Skip to content

Commit

Permalink
Fixed a bug when country couldn't be parsed from a phone number in mo…
Browse files Browse the repository at this point in the history
…st cases. Closes #4
  • Loading branch information
Nikolay committed Dec 14, 2016
1 parent 7536583 commit 9babfae
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.2.2 / 14.12.2016
===================

* Fixed a bug when country couldn't be parsed from a phone number in most cases

0.2.1 / 10.12.2016
===================

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libphonenumber-js",
"version": "0.2.1",
"version": "0.2.2",
"description": "A simpler (and smaller) rewrite of Google Android's famous libphonenumber library",
"main": "index.common.js",
"jsnext:main": "index.es6.js",
Expand Down
7 changes: 7 additions & 0 deletions source/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@ export function find_country_code(country_phone_code, national_phone_number)
// Is always non-empty, because `country_phone_code` is always valid
const possible_countries = metadata.country_phone_code_to_countries[country_phone_code]

// If there's just one country corresponding to the country code,
// then just return it, without further phone number digits validation.
if (possible_countries.length === 1)
{
return possible_countries[0]
}

for (let country_code of possible_countries)
{
const country = metadata.countries[country_code]
Expand Down
3 changes: 3 additions & 0 deletions test/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ describe('format', () =>
format({ country: 'CH', phone: '446681800' }, 'International').should.equal('+41 44 668 18 00')
format({ country: 'CH', phone: '446681800' }, 'International_plaintext').should.equal('+41446681800')
format({ country: 'CH', phone: '446681800' }, 'National').should.equal('044 668 18 00')

// France
format({ country: 'FR', phone: '169454850' }, 'National').should.equal('01 69 45 48 50')
})

it('should work in edge cases', function()
Expand Down
3 changes: 3 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ describe('parse', () =>

// China, Beijing
parse('010-852644821', 'CN').should.deep.equal({ country: 'CN', phone: '10852644821' })

// France
parse('+33169454850').should.deep.equal({ country: 'FR', phone: '169454850' })
})

it('should work in edge cases', function()
Expand Down

0 comments on commit 9babfae

Please sign in to comment.