From 84ee203527fcd8394636d636e3093741f8282a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20R=C3=A4ntil=C3=A4?= Date: Sun, 25 Aug 2024 12:08:18 +0200 Subject: [PATCH] fix: reverted #98 to align with libphonenumber closes #103 --- index-esm.mjs | 8 ++++---- lib/index.js | 8 ++++---- src/index.js | 12 ++++++------ test.in/awesome-phonenumber/new-api.ts | 20 ++++++++++++++++---- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/index-esm.mjs b/index-esm.mjs index f1e3257..5267e4b 100644 --- a/index-esm.mjs +++ b/index-esm.mjs @@ -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{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;bb;++b){d=void 0;if(a.length{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 } ); // 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' ); } ); @@ -327,8 +330,17 @@ describe( 'errors', ( ) => 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' ); } ); } );