Skip to content

Commit

Permalink
Fix RFC3966 parse bug. Closes #217
Browse files Browse the repository at this point in the history
  • Loading branch information
purecatamphetamine committed May 29, 2018
1 parent 0ba7ba0 commit f6c979c
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 79 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.2.13 / 30.05.2018
===================

* Fixed a previously unnoticed [bug](https://github.com/catamphetamine/libphonenumber-js/issues/217) regarding parsing RFC3966 phone URIs: previously `:` was mistakenly being considered a key-value separator instead of `=`. E.g. it was parsing RFC3966 phone numbers as `tel:+78005553535;ext:123` instead of `tel:+78005553535;ext=123`. The bug was found and reported by @cdunn.

1.2.6 / 12.05.2018
===================

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ parseNumber('(213) 373-4253', 'US') === { country: 'US', phone: '2133734253' }
parseNumber('(213) 373-4253 ext. 123', 'US') === { country: 'US', phone: '2133734253', ext: '123' }

// Parses RFC 3966 phone number URIs.
parseNumber('tel:+78005553535;ext:123') === { country: 'RU', phone: '8005553535', ext: '123' }
parseNumber('tel:+78005553535;ext=123') === { country: 'RU', phone: '8005553535', ext: '123' }
```

Available `options`:
Expand Down
5 changes: 4 additions & 1 deletion source/RFC3966.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export function parseRFC3966(text)
let number
let ext

// Replace "tel:" with "tel=" for parsing convenience.
text = text.replace(/^tel:/, 'tel=')

for (const part of text.split(';'))
{
const [name, value] = part.split(':')
const [name, value] = part.split('=')
switch (name)
{
case 'tel':
Expand Down
6 changes: 3 additions & 3 deletions source/RFC3966.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ describe('RFC3966', () =>
number : '+78005553535'
})

parseRFC3966('tel:+78005553535;ext:123').should.deep.equal
parseRFC3966('tel:+78005553535;ext=123').should.deep.equal
({
number : '+78005553535',
ext : '123'
})

// With `phone-context`
parseRFC3966('tel:8005553535;ext:123;phone-context:+7').should.deep.equal
parseRFC3966('tel:8005553535;ext=123;phone-context=+7').should.deep.equal
({
number : '+78005553535',
ext : '123'
})

// "Domain contexts" are ignored
parseRFC3966('tel:8005553535;ext:123;phone-context:www.leningrad.spb.ru').should.deep.equal
parseRFC3966('tel:8005553535;ext=123;phone-context=www.leningrad.spb.ru').should.deep.equal
({
number : '8005553535',
ext : '123'
Expand Down
6 changes: 3 additions & 3 deletions source/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,23 +275,23 @@ describe('parse', () =>

it('should parse RFC 3966 phone numbers', function()
{
parse('tel:+78005553535;ext:123').should.deep.equal
parse('tel:+78005553535;ext=123').should.deep.equal
({
country : 'RU',
phone : '8005553535',
ext : '123'
})

// Should parse "visual separators".
parse('tel:+7(800)555-35.35;ext:123').should.deep.equal
parse('tel:+7(800)555-35.35;ext=123').should.deep.equal
({
country : 'RU',
phone : '8005553535',
ext : '123'
})

// Invalid number.
parse('tel:+7x8005553535;ext:123').should.deep.equal({})
parse('tel:+7x8005553535;ext=123').should.deep.equal({})
})

it('should parse invalid international numbers even if they are invalid', () =>
Expand Down
10 changes: 0 additions & 10 deletions test/metadata/1.0.0/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,4 @@ describe('parse', () =>
// Not a valid extension
parse('2134567890 ext. 1234567890', 'US').should.deep.equal({})
})

it('should parse RFC 3966 phone numbers', function()
{
parse('tel:+78005553535;ext:123').should.deep.equal
({
country : 'RU',
phone : '8005553535',
ext : '123'
})
})
})
40 changes: 0 additions & 40 deletions test/metadata/1.1.11/test/RFC3966.test.js

This file was deleted.

21 changes: 0 additions & 21 deletions test/metadata/1.1.11/test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,27 +259,6 @@ describe('parse', () =>
parse('2134567890 ext. 1234567890', 'US').should.deep.equal({})
})

it('should parse RFC 3966 phone numbers', function()
{
parse('tel:+78005553535;ext:123').should.deep.equal
({
country : 'RU',
phone : '8005553535',
ext : '123'
})

// Should parse "visual separators".
parse('tel:+7(800)555-35.35;ext:123').should.deep.equal
({
country : 'RU',
phone : '8005553535',
ext : '123'
})

// Invalid number.
parse('tel:+7x8005553535;ext:123').should.deep.equal({})
})

it('should parse invalid international numbers even if they are invalid', () =>
{
parse('+49(0)15123020522', 'DE').should.deep.equal
Expand Down

0 comments on commit f6c979c

Please sign in to comment.