Skip to content

Commit

Permalink
fix: reverted #98 to align with libphonenumber
Browse files Browse the repository at this point in the history
closes #103
  • Loading branch information
grantila committed Aug 25, 2024
1 parent 94fcb58 commit 84ee203
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
8 changes: 4 additions & 4 deletions index-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,10 @@ var lib = {exports: {}};
function Mb(a,b){if("+"!==a.charAt(0)&&"00"!==a.slice(0,2))return {parsed:c,fa:d};try{var c=ib(S,a,b);}catch(e){}if(c){var d=lb(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 Nb;a:{try{Nb=new WeakMap;break a}catch(a){}Nb=void 0;}var Ob=Nb;
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{kb(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}=Mb(a,c)));this.g={number:{},regionCode:c,valid:!1,possible:!1};Ob&&Ob.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===Pb(c)){this.g.possibility="invalid-country-code";return}try{this.l=e?e:ib(S,a,c);}catch(f){this.g.possibility=Lb(a);return}}this.g.number.international=S.format(this.l,
1);this.g.number.national=S.format(this.l,2);this.g.number.e164=S.format(this.l,0);this.g.number.rfc3966=S.format(this.l,3);this.g.number.significant=N(this.l);this.g.canBeInternationallyDialled=S.ja(this.l);this.g.valid=kb(this.l);this.g.possible=mb(this.l);this.g.possibility=Lb(this.l);!this.g.valid&&this.g.possible&&(this.g.possibility="invalid",this.g.possible=!1);this.g.type=Jb(this.l);this.g.typeIsMobile=this.sa();this.g.typeIsFixedLine=this.ra();this.g.countryCode=M(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 Pb(a){return M(a)?T(S,a):0}V.getCountryCodeForRegionCode=Pb;V.getRegionCodeForCountryCode=function(a){return P(a)};function Qb(a){var b={};return a.filter(c=>{if(b.hasOwnProperty(c))return !1;b[c]=1;return !0})}V.getSupportedRegionCodes=function(){return Qb(Ya())};
V.getSupportedCallingCodes=function(){return Qb($a())};V.getExample=function(a,b){var c;b?c=gb(a,Kb(b)):c=gb(a,0);return (new V(c,a)).toJSON()};V.getAsYouType=function(a){return new Z(a)};V.getNumberFrom=function(a,b){try{a:{if(Ob){var e=Ob.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:fb(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;
1);this.g.number.national=S.format(this.l,2);this.g.number.e164=S.format(this.l,0);this.g.number.rfc3966=S.format(this.l,3);this.g.number.significant=N(this.l);this.g.canBeInternationallyDialled=S.ja(this.l);this.g.valid=kb(this.l);this.g.possible=mb(this.l);this.g.possibility=Lb(this.l);this.g.type=Jb(this.l);this.g.typeIsMobile=this.sa();this.g.typeIsFixedLine=this.ra();this.g.countryCode=M(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 Pb(a){return M(a)?T(S,a):0}V.getCountryCodeForRegionCode=Pb;V.getRegionCodeForCountryCode=function(a){return P(a)};function Qb(a){var b={};return a.filter(c=>{if(b.hasOwnProperty(c))return !1;b[c]=1;return !0})}V.getSupportedRegionCodes=function(){return Qb(Ya())};V.getSupportedCallingCodes=function(){return Qb($a())};
V.getExample=function(a,b){var c;b?c=gb(a,Kb(b)):c=gb(a,0);return (new V(c,a)).toJSON()};V.getAsYouType=function(a){return new Z(a)};V.getNumberFrom=function(a,b){try{a:{if(Ob){var e=Ob.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:fb(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 Pb(this.g.regionCode)};V.prototype.getCountryCode=V.prototype.ia;function Z(a){this.j=a;this.h=new sb(a);this.l=this.g="";}
Z.prototype.qa=function(a){this.g+=a;var b=this.h;b.la=zb(b,a);return this.l=b.la};Z.prototype.addChar=Z.prototype.qa;Z.prototype.za=function(){return this.l};Z.prototype.number=Z.prototype.za;Z.prototype.Aa=function(){return ""===this.g?this.l:this.reset(this.g.slice(0,this.g.length-1))};Z.prototype.removeChar=Z.prototype.Aa;
Z.prototype.reset=function(a){var b=this.h;b.la="";A(b.u);A(b.ba);A(b.$);b.aa=0;b.da="";A(b.h);b.s="";A(b.g);b.v=!0;b.oa=!1;b.ca=!1;b.ea=!1;b.j=[];b.ha=!1;b.m!=b.wa&&(b.m=tb(b,b.ma));this.l=this.g="";if(a){b=0;for(var c=a.length;b<c;++b)this.qa(a.charAt(b));}return this.l};Z.prototype.reset=Z.prototype.reset;Z.prototype.ua=function(){return (new V(this.l,{regionCode:this.j})).toJSON()};Z.prototype.getPhoneNumber=Z.prototype.ua;}).call(
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js

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

12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ export function PhoneNumber( phoneNumber, options )
this._json[ 'possible' ] = phoneUtil.isPossibleNumber( this._number );
this._json[ 'possibility' ] = getValidationResult( this._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;
}
// 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[ 'type' ] = getNumberType( this._number );

Expand Down
20 changes: 16 additions & 4 deletions test.in/awesome-phonenumber/new-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,22 +313,34 @@ describe( 'errors', ( ) =>
} );

// https://github.com/grantila/awesome-phonenumber/issues/98
// This is now reverted to map to libphonenumber more closely
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' );
// expect( pn.possible ).toBe( false );
// expect( pn.possibility ).toBe( 'invalid' );
expect( pn.possible ).toBe( true );
expect( pn.possibility ).toBe( 'is-possible' );
} );


it( 'invalid phone number should still set type', ( ) => {
const pn = parsePhoneNumber( "0740521234", { regionCode: "US" } );

expect( pn.valid ).toBe( false );
expect( pn.possible ).toBe( false );
expect( pn.possibility ).toBe( 'invalid' );
expect( pn.possible ).toBe( true );
expect( pn.possibility ).toBe( 'is-possible' );
expect( pn.type ).toBe( 'unknown' );
} );

it( 'should separate possible and valid', ( ) => {
const pn = parsePhoneNumber( "+11111111111" );

expect( pn.valid ).toBe( false );
expect( pn.possible ).toBe( true );
expect( pn.possibility ).toBe( 'is-possible' );
expect( pn.type ).toBe( 'unknown' );
} );
} );
Expand Down

0 comments on commit 84ee203

Please sign in to comment.