Skip to content

Commit

Permalink
fix(parser): handle leading 00 same as libphonenumber
Browse files Browse the repository at this point in the history
This will make leading '00' be handled similar to + but still depending on region code.

fix #78
  • Loading branch information
grantila committed May 22, 2022
1 parent f9a37f3 commit 57e6baa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
25 changes: 16 additions & 9 deletions index-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,16 @@ function ub(a){var b=a.g.toString(),c=0;if(1!=a.l.ha())var d=!1;else d=a.g.toStr
function rb(a){var b=a.aa.toString(),c=new RegExp("^(?:\\+|"+n(a.l,11)+")");c=b.match(c);return null!=c&&null!=c[0]&&0<c[0].length?(a.ba=!0,c=c[0].length,A(a.g),a.g.g(b.substring(c)),A(a.h),a.h.g(b.substring(0,c)),"+"!=b.charAt(0)&&a.h.g(" "),!0):!1}function sb(a){if(0==a.g.h.length)return !1;var b=new z,c=gb(a.g,b);if(0==c)return !1;A(a.g);a.g.g(b.toString());b=R(c);"001"==b?a.l=S(a.oa,""+c):b!=a.la&&(a.l=kb(a,b));a.h.g(""+c).g(" ");a.o="";return !0}
function wb(a,b){var c=a.v.toString();if(0<=c.substring(a.$).search(a.ja)){var d=c.search(a.ja);b=c.replace(a.ja,b);A(a.v);a.v.g(b);a.$=d;return b.substring(0,a.$+1)}1==a.j.length&&(a.u=!1);a.ca="";return a.s.toString()}var T=H.h();function Ab(a){var b=T,c=cb(b,a);b=P(b,u(a,1),c);null==b?a=-1:(a=O(a),a=ab(a,b));switch(a){case 0:return "fixed-line";case 2:return "fixed-line-or-mobile";case 1:return "mobile";case 8:return "pager";case 7:return "personal-number";case 4:return "premium-rate";case 5:return "shared-cost";case 3:return "toll-free";case 9:return "uan";case 6:return "voip";default:case -1:return "unknown"}}
function Bb(a){switch(a){case "fixed-line":return 0;case "fixed-line-or-mobile":return 2;case "mobile":return 1;case "pager":return 8;case "personal-number":return 7;case "premium-rate":return 4;case "shared-cost":return 5;case "toll-free":return 3;case "uan":return 9;case "voip":return 6;default:case "unknown":return -1}}
function Cb(a){try{switch(eb(a)){case 0:return "is-possible";case 1:return "invalid-country-code";case 3:return "too-long";case 2:return "too-short"}if(db(a))return "is-possible"}catch(b){}return "unknown"}function Db(a){if("+"!==a.charAt(0))return {parsed:b,ea:c};try{var b=$a(T,a);}catch(e){}if(b){var c=cb(T,b);if(null!=c&&"ZZ"!==c)return {parsed:b,ea:c}}for(var d=1;4>d;++d){c=void 0;if(a.length<d+1)return {parsed:b,ea:c};c=R(a.substring(1,d+1));if("ZZ"!==c)return {ea:c}}return {parsed:b,ea:void 0}}
function V(a,b){if(!(this instanceof V))return new V(a,b);if("string"===typeof a)var c=!1;else try{bb(a),c=!0;}catch(e){c=!1;}let d;if(!c&&"string"!==typeof a)throw Error("Invalid phone number, expected a string");if(!c&&null!=b&&"string"!==typeof b)throw Error("Invalid region code, expected a string");c||(a=a.trim(),b&&"+"===a.charAt(0)&&(b=null),b||({ea:b=null,parsed:d}=Db(a)));this.g={number:{},regionCode:b,valid:!1,possible:!1};if(c)this.h=a;else {this.h=null;this.g.number.input=a;if(!b){this.g.possibility=
"invalid-country-code";return}if(0===Eb(b)){this.g.possibility="invalid-country-code";return}try{this.h=d?d:$a(T,a,b);}catch(e){this.g.possibility=Cb(a);return}}this.g.number.international=N(T,this.h,1);this.g.number.national=N(T,this.h,2);this.g.number.e164=N(T,this.h,0);this.g.number.rfc3966=N(T,this.h,3);this.g.number.significant=O(this.h);this.g.canBeInternationallyDialled=T.ia(this.h);this.g.possible=db(this.h);this.g.valid=bb(this.h);this.g.type=Ab(this.h);this.g.possibility=Cb(this.h);}
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 Eb(a){return M(a)?Xa(T,a):0}V.getCountryCodeForRegionCode=Eb;V.getRegionCodeForCountryCode=function(a){return R(a)};function Fb(a){var b={};return a.filter(c=>{if(b.hasOwnProperty(c))return !1;b[c]=1;return !0})}V.getSupportedRegionCodes=function(){return Fb(Oa())};
V.getSupportedCallingCodes=function(){return Fb(Qa())};V.getExample=function(a,b){var c;b?c=Ya(a,Bb(b)):c=Ya(a,0);return new V(c,a)};V.getAsYouType=function(a){return new Z(a)};V.prototype.toJSON=function(){return this.g};V.prototype.toJSON=V.prototype.toJSON;V.prototype.ia=function(){return this.g.canBeInternationallyDialled};V.prototype.canBeInternationallyDialled=V.prototype.ia;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.wa=function(){return "mobile"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isMobile=V.prototype.wa;V.prototype.va=function(){return "fixed-line"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isFixedLine=V.prototype.va;V.prototype.qa=function(a){return this.g.number[null==a?"e164":a]};V.prototype.getNumber=V.prototype.qa;
V.prototype.ra=function(a){return Wa(this.h,a)};V.prototype.getNumberFrom=V.prototype.ra;V.prototype.ta=function(){return this.g.regionCode};V.prototype.getRegionCode=V.prototype.ta;V.prototype.ha=function(){return Eb(this.g.regionCode)};V.prototype.getCountryCode=V.prototype.ha;function Z(a){this.l=a;this.j=new jb(a);this.g=this.h="";}Z.prototype.pa=function(a){this.h+=a;var b=this.j;b.ka=qb(b,a);return this.g=b.ka};Z.prototype.addChar=Z.prototype.pa;Z.prototype.za=function(){return this.g};
Z.prototype.number=Z.prototype.za;Z.prototype.Aa=function(){return ""===this.h?this.g:this.reset(this.h.slice(0,this.h.length-1))};Z.prototype.removeChar=Z.prototype.Aa;Z.prototype.reset=function(a){var b=this.j;b.ka="";A(b.s);A(b.aa);A(b.v);b.$=0;b.ca="";A(b.h);b.o="";A(b.g);b.u=!0;b.na=!1;b.ba=!1;b.da=!1;b.j=[];b.ga=!1;b.l!=b.ua&&(b.l=kb(b,b.la));this.g=this.h="";if(a){b=0;for(var c=a.length;b<c;++b)this.pa(a.charAt(b));}return this.g};Z.prototype.reset=Z.prototype.reset;
Z.prototype.sa=function(){return new V(this.g,this.l)};Z.prototype.getPhoneNumber=Z.prototype.sa;}).call(
function Cb(a){try{switch(eb(a)){case 0:return "is-possible";case 1:return "invalid-country-code";case 3:return "too-long";case 2:return "too-short"}if(db(a))return "is-possible"}catch(b){}return "unknown"}
function Db(a,b){if("+"!==a.charAt(0)&&"00"!==a.slice(0,2))return {parsed:c,ea:d};try{var c=$a(T,a,b);}catch(e){}if(c){var d=cb(T,c);if(null!=d&&"ZZ"!==d)return {parsed:c,ea:d}}for(b=1;4>b;++b){d=void 0;if(a.length<b+1)return {parsed:c,ea:d};d=R(a.substring(1,b+1));if("ZZ"!==d)return {ea:d}}return {parsed:c,ea:void 0}}
function V(a,b){if(!(this instanceof V))return new V(a,b);if("string"===typeof a)var c=!1;else try{bb(a),c=!0;}catch(e){c=!1;}let d;if(!c&&"string"!==typeof a)throw Error("Invalid phone number, expected a string");if(!c&&null!=b&&"string"!==typeof b)throw Error("Invalid region code, expected a string");c||(a=a.trim(),b&&"+"===a.charAt(0)&&(b=null),b&&"+"!==a.charAt(0)&&"00"!==a.slice(0,2)||({ea:b=null,parsed:d}=Db(a,b)));this.g={number:{},regionCode:b,valid:!1,possible:!1};if(c)this.h=a;else {this.h=
null;this.g.number.input=a;if(!b){this.g.possibility="invalid-country-code";return}if(0===Eb(b)){this.g.possibility="invalid-country-code";return}try{this.h=d?d:$a(T,a,b);}catch(e){this.g.possibility=Cb(a);return}}this.g.number.international=N(T,this.h,1);this.g.number.national=N(T,this.h,2);this.g.number.e164=N(T,this.h,0);this.g.number.rfc3966=N(T,this.h,3);this.g.number.significant=O(this.h);this.g.canBeInternationallyDialled=T.ia(this.h);this.g.possible=db(this.h);this.g.valid=bb(this.h);this.g.type=
Ab(this.h);this.g.possibility=Cb(this.h);}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 Eb(a){return M(a)?Xa(T,a):0}V.getCountryCodeForRegionCode=Eb;V.getRegionCodeForCountryCode=function(a){return R(a)};function Fb(a){var b={};return a.filter(c=>{if(b.hasOwnProperty(c))return !1;b[c]=1;return !0})}
V.getSupportedRegionCodes=function(){return Fb(Oa())};V.getSupportedCallingCodes=function(){return Fb(Qa())};V.getExample=function(a,b){var c;b?c=Ya(a,Bb(b)):c=Ya(a,0);return new V(c,a)};V.getAsYouType=function(a){return new Z(a)};V.prototype.toJSON=function(){return this.g};V.prototype.toJSON=V.prototype.toJSON;V.prototype.ia=function(){return this.g.canBeInternationallyDialled};V.prototype.canBeInternationallyDialled=V.prototype.ia;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.wa=function(){return "mobile"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isMobile=V.prototype.wa;V.prototype.va=function(){return "fixed-line"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isFixedLine=V.prototype.va;
V.prototype.qa=function(a){return this.g.number[null==a?"e164":a]};V.prototype.getNumber=V.prototype.qa;V.prototype.ra=function(a){return Wa(this.h,a)};V.prototype.getNumberFrom=V.prototype.ra;V.prototype.ta=function(){return this.g.regionCode};V.prototype.getRegionCode=V.prototype.ta;V.prototype.ha=function(){return Eb(this.g.regionCode)};V.prototype.getCountryCode=V.prototype.ha;function Z(a){this.l=a;this.j=new jb(a);this.g=this.h="";}
Z.prototype.pa=function(a){this.h+=a;var b=this.j;b.ka=qb(b,a);return this.g=b.ka};Z.prototype.addChar=Z.prototype.pa;Z.prototype.za=function(){return this.g};Z.prototype.number=Z.prototype.za;Z.prototype.Aa=function(){return ""===this.h?this.g:this.reset(this.h.slice(0,this.h.length-1))};Z.prototype.removeChar=Z.prototype.Aa;
Z.prototype.reset=function(a){var b=this.j;b.ka="";A(b.s);A(b.aa);A(b.v);b.$=0;b.ca="";A(b.h);b.o="";A(b.g);b.u=!0;b.na=!1;b.ba=!1;b.da=!1;b.j=[];b.ga=!1;b.l!=b.ua&&(b.l=kb(b,b.la));this.g=this.h="";if(a){b=0;for(var c=a.length;b<c;++b)this.pa(a.charAt(b));}return this.g};Z.prototype.reset=Z.prototype.reset;Z.prototype.sa=function(){return new V(this.g,this.l)};Z.prototype.getPhoneNumber=Z.prototype.sa;}).call(
(module.exports)
|| typeof globalThis !== 'undefined' && globalThis
|| typeof commonjsGlobal !== 'undefined' && commonjsGlobal
Expand Down Expand Up @@ -564,6 +565,12 @@ Object.defineProperty(
module.exports.default = module.exports;

module.exports.parsePhoneNumber = module.exports;
module.exports.getCountryCodeForRegionCode = module.exports.getCountryCodeForRegionCode;
module.exports.getRegionCodeForCountryCode = module.exports.getRegionCodeForCountryCode;
module.exports.getSupportedCallingCodes = module.exports.getSupportedCallingCodes;
module.exports.getSupportedRegionCodes = module.exports.getSupportedRegionCodes;
module.exports.getExample = module.exports.getExample;
module.exports.getAsYouType = module.exports.getAsYouType;
}(awesomePhonenumber));

var index = /*@__PURE__*/getDefaultExportFromCjs(awesomePhonenumber.exports);
Expand Down
19 changes: 10 additions & 9 deletions lib/index.js

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

Loading

0 comments on commit 57e6baa

Please sign in to comment.