Skip to content

Commit

Permalink
fix(cc): Handle invalid country codes
Browse files Browse the repository at this point in the history
fix #26
  • Loading branch information
grantila committed Feb 14, 2019
1 parent dff9f44 commit c323fe6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
16 changes: 8 additions & 8 deletions lib/index.js

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

38 changes: 30 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,20 @@ function toNumberType( exportedName )

function getValidationResult( number )
{
switch( phoneUtil.isPossibleNumberWithReason( number ) )
try
{
case ValidationResult.IS_POSSIBLE: return 'is-possible';
case ValidationResult.INVALID_COUNTRY_CODE: return 'invalid-country-code';
case ValidationResult.TOO_LONG: return 'too-long';
case ValidationResult.TOO_SHORT: return 'too-short';
}
switch( phoneUtil.isPossibleNumberWithReason( number ) )
{
case ValidationResult.IS_POSSIBLE: return 'is-possible';
case ValidationResult.INVALID_COUNTRY_CODE: return 'invalid-country-code';
case ValidationResult.TOO_LONG: return 'too-long';
case ValidationResult.TOO_SHORT: return 'too-short';
}

if ( phoneUtil.isPossibleNumber( number ) )
return 'is-possible';
} catch ( err ) { }

if ( phoneUtil.isPossibleNumber( number ) )
return 'is-possible';
return 'unknown';
}

Expand All @@ -87,6 +91,8 @@ function extractRegionCode( phoneNumber )
regionCode = PhoneNumber.getRegionCodeForCountryCode( firstThree );
if ( regionCode !== 'ZZ' )
return regionCode;

return null;
}

/**
Expand Down Expand Up @@ -148,12 +154,28 @@ function PhoneNumber( phoneNumber, regionCode )
this._number = null;
this._json[ 'number' ][ 'input' ] = phoneNumber;

if ( !regionCode )
{
this._json[ 'possibility' ] = 'invalid-country-code';
return;
}
else
{
var cc = PhoneNumber.getCountryCodeForRegionCode( regionCode );
if ( cc === 0 )
{
this._json[ 'possibility' ] = 'invalid-country-code';
return;
}
}

try
{
this._number = phoneUtil.parse( phoneNumber, regionCode );
}
catch ( e )
{
this._json[ 'possibility' ] = getValidationResult( phoneNumber );
return;
}
}
Expand Down
37 changes: 37 additions & 0 deletions test.in/awesome-phonenumber/should-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,40 @@ describe( 'general', function( ) {
expect( pn.getRegionCode( ) ).to.equal( 'CA' );
} );
} );

describe( 'errors', function( ) {
it( 'should handle invalid country code', function( ) {
var pn = new PhoneNumber( '+0123' );
expect( pn.isValid( ) ).to.be.false;
expect( pn.isPossible( ) ).to.be.false;
expect( pn.toJSON( ).possibility ).to.equal( 'invalid-country-code' );
} );

it( 'should handle invalid country code (and valid region code)', function( ) {
var pn = new PhoneNumber( '+0123', 'SE' );
expect( pn.isValid( ) ).to.be.false;
expect( pn.isPossible( ) ).to.be.false;
expect( pn.toJSON( ).possibility ).to.equal( 'invalid-country-code' );
} );

it( 'should handle invalid country code and region code', function( ) {
var pn = new PhoneNumber( '0123', 'XX' );
expect( pn.isValid( ) ).to.be.false;
expect( pn.isPossible( ) ).to.be.false;
expect( pn.toJSON( ).possibility ).to.equal( 'invalid-country-code' );
} );

it( 'should handle missing country code', function( ) {
var pn = new PhoneNumber( '0123' );
expect( pn.isValid( ) ).to.be.false;
expect( pn.isPossible( ) ).to.be.false;
expect( pn.toJSON( ).possibility ).to.equal( 'invalid-country-code' );
} );

it( 'should handle TOO_SHORT', function( ) {
var pn = new PhoneNumber( '0123', 'SE' );
expect( pn.isValid( ) ).to.be.false;
expect( pn.isPossible( ) ).to.be.false;
expect( pn.toJSON( ).possibility ).to.equal( 'too-short' );
} );
} );

0 comments on commit c323fe6

Please sign in to comment.