Skip to content

Commit

Permalink
fix(core): ignore region code if E164 is provided
Browse files Browse the repository at this point in the history
fix #51
  • Loading branch information
grantila committed Aug 18, 2020
1 parent 86f7262 commit 32e2bb7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,12 @@ function PhoneNumber( phoneNumber, regionCode )

if ( !isInternal )
{
phoneNumber = phoneNumber.trim( );

if ( regionCode && ( phoneNumber.charAt( 0 ) === '+' ) )
{
// Ensure region code is valid
var cc = PhoneNumber.getCountryCodeForRegionCode( regionCode );
if ( phoneNumber.substr( 1, ( '' + cc ).length ) !== '' + cc )
// Wrong region code, let's fix it
regionCode = null;
}
// Ignore region code if we have an international phonenumber,
// it'll be extracted properly by libphonenumber.
regionCode = null;

if ( !regionCode )
// Guess region code
Expand Down
6 changes: 6 additions & 0 deletions test.in/awesome-phonenumber/should-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ describe( 'general', function( ) {
expect( pn.getRegionCode( ) ).to.equal( 'CA' );
} );

it( 'should not use US for known CA numbers', function( ) {
// Issue #51
const pn = new PhoneNumber( '+1613 734.6759', 'US' );
expect( pn.getRegionCode( ) ).to.equal( 'CA' );
} );

it( 'should extract region by prefix as early as possible', function( ) {
const pn1 = new PhoneNumber( '+1' );
const pn1x = new PhoneNumber( '+12' );
Expand Down

0 comments on commit 32e2bb7

Please sign in to comment.