Skip to content

Commit

Permalink
fix(core): Fix handling invalid numbers
Browse files Browse the repository at this point in the history
fix #98
  • Loading branch information
grantila committed Apr 4, 2023
1 parent b312e85 commit 77f0374
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions index-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ function Eb(a){try{switch(gb(a)){case 0:return "is-possible";case 1:return "inva
function Fb(a,b){if("+"!==a.charAt(0)&&"00"!==a.slice(0,2))return {parsed:c,fa:d};try{var c=bb(S,a,b);}catch(e){}if(c){var d=eb(S,c);if(null!=d&&"ZZ"!==d)return {parsed:c,fa:d}}for(b=1;4>b;++b){d=void 0;if(a.length<b+1)return {parsed:c,fa:d};d=P(a.substring(1,b+1));if("ZZ"!==d)return {fa:d}}return {parsed:c,fa:void 0}}var Gb;a:{try{Gb=new WeakMap;break a}catch(a){}Gb=void 0;}var Hb=Gb;
function V(a,b){if(!(this instanceof V))return new V(a,b);var c=null==b?void 0:b.regionCode;if("string"===typeof a)var d=!1;else try{db(a),d=!0;}catch(f){d=!1;}let e;if(!d&&"string"!==typeof a)throw Error("Invalid phone number, expected a string");if(!d&&"object"!==typeof b&&"undefined"!==typeof b)throw Error(`Invalid options, expected object, got ${typeof b}. `+"This may be because of not yet upgraded code.");if(!d&&null!=c&&"string"!==typeof c)throw Error("Invalid region code, expected a string, got "+
`${typeof c} ${c}`);d||(a=a.trim(),c&&"+"===a.charAt(0)&&(c=null),c&&"+"!==a.charAt(0)&&"00"!==a.slice(0,2)||({fa:c=null,parsed:e}=Fb(a,c)));this.g={number:{},regionCode:c,valid:!1,possible:!1};Hb&&Hb.set(this.g,this);if(d)this.l=a;else {this.l=null;this.g.number.input=a;if(!c){this.g.possibility="invalid-country-code";return}if(0===Ib(c)){this.g.possibility="invalid-country-code";return}try{this.l=e?e:bb(S,a,c);}catch(f){this.g.possibility=Eb(a);return}}this.g.number.international=M(S,this.l,1);this.g.number.national=
M(S,this.l,2);this.g.number.e164=M(S,this.l,0);this.g.number.rfc3966=M(S,this.l,3);this.g.number.significant=N(this.l);this.g.canBeInternationallyDialled=S.ja(this.l);this.g.possible=fb(this.l);this.g.valid=db(this.l);this.g.type=Cb(this.l);this.g.possibility=Eb(this.l);this.g.typeIsMobile=this.sa();this.g.typeIsFixedLine=this.ra();this.g.countryCode=L(c)?T(S,c):0;}var W=["PhoneNumber$$module$src$index"],X=aa;W[0]in X||"undefined"==typeof X.execScript||X.execScript("var "+W[0]);
for(var Y;W.length&&(Y=W.shift());)W.length||void 0===V?X[Y]&&X[Y]!==Object.prototype[Y]?X=X[Y]:X=X[Y]={}:X[Y]=V;function Ib(a){return L(a)?T(S,a):0}V.getCountryCodeForRegionCode=Ib;V.getRegionCodeForCountryCode=function(a){return P(a)};function Jb(a){var b={};return a.filter(c=>{if(b.hasOwnProperty(c))return !1;b[c]=1;return !0})}V.getSupportedRegionCodes=function(){return Jb(Ra())};V.getSupportedCallingCodes=function(){return Jb(Ta())};
M(S,this.l,2);this.g.number.e164=M(S,this.l,0);this.g.number.rfc3966=M(S,this.l,3);this.g.number.significant=N(this.l);this.g.canBeInternationallyDialled=S.ja(this.l);this.g.valid=db(this.l);this.g.possible=fb(this.l);this.g.possibility=Eb(this.l);!this.g.valid&&this.g.possible&&(this.g.possibility="invalid",this.g.possible=!1);this.g.type=Cb(this.l);this.g.typeIsMobile=this.sa();this.g.typeIsFixedLine=this.ra();this.g.countryCode=L(c)?T(S,c):0;}var W=["PhoneNumber$$module$src$index"],X=aa;
W[0]in X||"undefined"==typeof X.execScript||X.execScript("var "+W[0]);for(var Y;W.length&&(Y=W.shift());)W.length||void 0===V?X[Y]&&X[Y]!==Object.prototype[Y]?X=X[Y]:X=X[Y]={}:X[Y]=V;function Ib(a){return L(a)?T(S,a):0}V.getCountryCodeForRegionCode=Ib;V.getRegionCodeForCountryCode=function(a){return P(a)};function Jb(a){var b={};return a.filter(c=>{if(b.hasOwnProperty(c))return !1;b[c]=1;return !0})}V.getSupportedRegionCodes=function(){return Jb(Ra())};V.getSupportedCallingCodes=function(){return Jb(Ta())};
V.getExample=function(a,b){var c;b?c=$a(a,Db(b)):c=$a(a,0);return (new V(c,a)).toJSON()};V.getAsYouType=function(a){return new Z(a)};V.getNumberFrom=function(a,b){try{a:{if(Hb){var e=Hb.get(a);if(e){var c=e;break a}}let d;c=new V(null==a?void 0:null==(d=a.number)?void 0:d.e164,{});}return {valid:!0,number:Za(c.l,b)}}catch(d){return {valid:!1,error:d}}};V.prototype.toJSON=function(){return this.g};V.prototype.toJSON=V.prototype.toJSON;V.prototype.ja=function(){return this.g.canBeInternationallyDialled};
V.prototype.canBeInternationallyDialled=V.prototype.ja;V.prototype.ya=function(){return this.g.valid};V.prototype.isValid=V.prototype.ya;V.prototype.xa=function(){return this.g.possible};V.prototype.isPossible=V.prototype.xa;V.prototype.getType=function(){return this.g.type};V.prototype.getType=V.prototype.getType;V.prototype.sa=function(){return "mobile"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isMobile=V.prototype.sa;
V.prototype.ra=function(){return "fixed-line"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isFixedLine=V.prototype.ra;V.prototype.ta=function(a){return this.g.number[null==a?"e164":a]};V.prototype.getNumber=V.prototype.ta;V.prototype.va=function(){return this.g.regionCode};V.prototype.getRegionCode=V.prototype.va;V.prototype.ia=function(){return Ib(this.g.regionCode)};V.prototype.getCountryCode=V.prototype.ia;function Z(a){this.j=a;this.h=new lb(a);this.l=this.g="";}
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js

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

14 changes: 9 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ export function PhoneNumber( phoneNumber, options )
if ( !( this instanceof PhoneNumber ) )
return new PhoneNumber( phoneNumber, options );

var self = this;

var regionCode = options?.['regionCode'];

var isInternal =
Expand Down Expand Up @@ -273,12 +271,18 @@ export function PhoneNumber( phoneNumber, options )
this._json[ 'canBeInternationallyDialled' ] =
phoneUtil.canBeInternationallyDialled( this._number );

this._json[ 'possible' ] = phoneUtil.isPossibleNumber( this._number );
this._json[ 'valid' ] = phoneUtil.isValidNumber( this._number );
this._json[ 'possible' ] = phoneUtil.isPossibleNumber( this._number );
this._json[ 'possibility' ] = getValidationResult( this._number );

this._json[ 'type' ] = getNumberType( self._number );
if ( !this._json[ 'valid' ] && this._json[ 'possible' ] )
{
// Sometimes libphonenumber says a number is possible but invalid...
this._json[ 'possibility' ] = 'invalid';
this._json[ 'possible' ] = false;
}

this._json[ 'possibility' ] = getValidationResult( self._number );
this._json[ 'type' ] = getNumberType( this._number );

this._json[ 'typeIsMobile' ] = this.isMobile( );
this._json[ 'typeIsFixedLine' ] = this.isFixedLine( );
Expand Down
10 changes: 10 additions & 0 deletions test.in/awesome-phonenumber/new-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ describe( 'errors', ( ) =>
expect( failure( true ) )
.toMatchObject( failureResult( "Invalid options" ) );
} );

// https://github.com/grantila/awesome-phonenumber/issues/98
it( 'should handle invalid phone number #98', ( ) =>
{
const pn = parsePhoneNumber( "0740521234", { regionCode: "US" } );

expect( pn.valid ).toBe( false );
expect( pn.possible ).toBe( false );
expect( pn.possibility ).toBe( 'invalid' );
} );
} );

describe( 'getNumberFrom', ( ) =>
Expand Down

0 comments on commit 77f0374

Please sign in to comment.