From df36157e5d86e6c42baf329c204e3f0157dc0d52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20R=C3=A4ntil=C3=A4?= Date: Sun, 25 Aug 2024 12:24:08 +0200 Subject: [PATCH] feat: Support short number handling closes #112 --- README.md | 8 + index-esm.mjs | 265 ++++++++++++++++++------ index.d.ts | 2 + lib/index.js | 267 +++++++++++++++++++------ src/index.js | 19 +- test.in/awesome-phonenumber/new-api.ts | 38 +++- 6 files changed, 479 insertions(+), 120 deletions(-) diff --git a/README.md b/README.md index 0832724..7bad113 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,8 @@ const pn = { possibility: 'is-possible', regionCode: 'SE', possible: true, + shortPossible: false, + shortValid: false, canBeInternationallyDialled: true, type: 'mobile', countryCode: 46, @@ -105,6 +107,8 @@ interface ParsedPhoneNumberValid { possibility: PhoneNumberPossibility; // a string union, see below regionCode: string; possible: boolean; + shortPossible: boolean; + shortValid: boolean; canBeInternationallyDialled: boolean; type: PhoneNumberTypes; // a string union, see below countryCode: number; @@ -121,10 +125,14 @@ interface ParsedPhoneNumberInvalid { possible: false; possibility: 'invalid'; + shortPossible: boolean; + shortValid: boolean; error?: unknown; }; ``` +Note that an incorrect (invalid) phone number can still be a valid _short number_ for the given region. + ## API diff --git a/index-esm.mjs b/index-esm.mjs index 5267e4b..9f6510f 100644 --- a/index-esm.mjs +++ b/index-esm.mjs @@ -14,10 +14,10 @@ var lib = {exports: {}}; Copyright The Closure Library Authors. SPDX-License-Identifier: Apache-2.0 */ - var aa=this||self;function h(a,b){function c(){}c.prototype=b.prototype;a.Ba=b.prototype;a.prototype=new c;a.prototype.constructor=a;a.Ha=function(d,e,f){for(var g=Array(arguments.length-2),l=2;la.length?!1:K(Sa,a)}function Wa(a){return K(Na,a)?L(a,Ga):L(a,Ea)}function Xa(a){var b=Wa(a.toString());A(a);a.g(b);}function Ya(){return Object.keys(G).filter(function(a){return isNaN(a)})}function Za(){return Object.keys(G).filter(function(a){return !isNaN(a)}).map(function(a){return parseInt(a,10)})}function $a(){var a=Object.keys(F);return [...Za(),...a.map(function(b){return parseInt(b,10)})]} function L(a,b){for(var c=new z,d,e=a.length,f=0;fb?2:f[f.length-1]=e&&e<=d;++e)if(c=parseInt(a.substring(0,e),10),c in F)return b.g(a.substring(e)),c;return 0} - function qb(a,b,c,d,e){if(0==b.length)return 0;b=new z(b);var f;null!=c&&(f=n(c,11));null==f&&(f="NonMatch");var g=b.toString();if(0==g.length)f=20;else if(I.test(g))g=g.replace(I,""),A(b),b.g(Wa(g)),f=1;else {g=new RegExp(f);Xa(b);f=b.toString();if(0==f.search(g)){g=f.match(g)[0].length;var l=f.substring(g).match(Ja);l&&null!=l[1]&&0=b.h.length)throw Error("Phone number too short after IDD");a=pb(b, - d);if(0!=a)return p(e,1,a),a;throw Error("Invalid country calling code");}if(null!=c&&(f=u(c,10),g=""+f,l=b.toString(),0==l.lastIndexOf(g,0)&&(g=new z(l.substring(g.length)),l=n(c,1),l=new RegExp(u(l,2)),rb(g,c,null),g=g.toString(),!K(l,b.toString())&&K(l,g)||3==ob(a,b.toString(),c,-1))))return d.g(g),p(e,1,f),f;p(e,1,0);return 0} - function rb(a,b,c){var d=a.toString(),e=d.length,f=n(b,15);if(0!=e&&null!=f&&0!=f.length){var g=new RegExp("^(?:"+f+")");if(e=g.exec(d)){f=new RegExp(u(n(b,1),2));var l=K(f,d),q=e.length-1;b=n(b,16);if(null==b||0==b.length||null==e[q]||0==e[q].length){if(!l||K(f,d.substring(e[0].length)))null!=c&&0=b.length)e="";else {var f=b.indexOf(";",e);e=-1!==f?b.substring(e,f):b.substring(e);}var g=e;null==g?f=!0:0===g.length?f=!1:(f=Oa.exec(g),g=Pa.exec(g),f=null!==f||null!==g);if(!f)throw Error("The string supplied did not seem to be a phone number"); - null!=e?("+"===e.charAt(0)&&d.g(e),e=b.indexOf("tel:"),d.g(b.substring(0<=e?e+4:0,b.indexOf(";phone-context=")))):(e=d.g,b=null!=b?b:"",f=b.search(Ka),0<=f?(b=b.substring(f),b=b.replace(Ma,""),f=b.search(La),0<=f&&(b=b.substring(0,f))):b="",e.call(d,b));b=d.toString();e=b.indexOf(";isub=");0e.h.length)throw Error("The string supplied is too short to be a phone number"); - null!=f&&(c=new z(e.toString()),rb(c,f,new z),a=ob(a,c.toString(),f,-1),2!=a&&4!=a&&5!=a&&(e=c));a=e.toString();c=a.length;if(2>c)throw Error("The string supplied is too short to be a phone number");if(17b;++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; - 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;ba.length?!1:K(Ta,a)}function Xa(a){return K(Oa,a)?L(a,Ha):L(a,Fa)}function Ya(a){var b=Xa(a.toString());A(a);a.g(b);}function Za(){return Object.keys(G).filter(function(a){return isNaN(a)})}function $a(){return Object.keys(G).filter(function(a){return !isNaN(a)}).map(function(a){return parseInt(a,10)})}function ab(){var a=Object.keys(F);return [...$a(),...a.map(function(b){return parseInt(b,10)})]} function L(a,b){for(var c=new z,d,e=a.length,f=0;fb?2:f[f.length-1]=e&&e<=d;++e)if(c=parseInt(a.substring(0,e),10),c in F)return b.g(a.substring(e)),c;return 0} + function rb(a,b,c,d,e){if(0==b.length)return 0;b=new z(b);var f;null!=c&&(f=n(c,11));null==f&&(f="NonMatch");var g=b.toString();if(0==g.length)f=20;else if(I.test(g))g=g.replace(I,""),A(b),b.g(Xa(g)),f=1;else {g=new RegExp(f);Ya(b);f=b.toString();if(0==f.search(g)){g=f.match(g)[0].length;var l=f.substring(g).match(Ka);l&&null!=l[1]&&0=b.h.length)throw Error("Phone number too short after IDD");a=qb(b, + d);if(0!=a)return q(e,1,a),a;throw Error("Invalid country calling code");}if(null!=c&&(f=u(c,10),g=""+f,l=b.toString(),0==l.lastIndexOf(g,0)&&(g=new z(l.substring(g.length)),l=n(c,1),l=new RegExp(u(l,2)),sb(g,c,null),g=g.toString(),!K(l,b.toString())&&K(l,g)||3==pb(a,b.toString(),c,-1))))return d.g(g),q(e,1,f),f;q(e,1,0);return 0} + function sb(a,b,c){var d=a.toString(),e=d.length,f=n(b,15);if(0!=e&&null!=f&&0!=f.length){var g=new RegExp("^(?:"+f+")");if(e=g.exec(d)){f=new RegExp(u(n(b,1),2));var l=K(f,d),p=e.length-1;b=n(b,16);if(null==b||0==b.length||null==e[p]||0==e[p].length){if(!l||K(f,d.substring(e[0].length)))null!=c&&0=b.length)e="";else {var f=b.indexOf(";",e);e=-1!==f?b.substring(e,f):b.substring(e);}var g=e;null==g?f=!0:0===g.length?f=!1:(f=Pa.exec(g),g=Qa.exec(g),f=null!==f||null!==g);if(!f)throw Error("The string supplied did not seem to be a phone number"); + null!=e?("+"===e.charAt(0)&&d.g(e),e=b.indexOf("tel:"),d.g(b.substring(0<=e?e+4:0,b.indexOf(";phone-context=")))):(e=d.g,b=null!=b?b:"",f=b.search(La),0<=f?(b=b.substring(f),b=b.replace(Na,""),f=b.search(Ma),0<=f&&(b=b.substring(0,f))):b="",e.call(d,b));b=d.toString();e=b.indexOf(";isub=");0e.h.length)throw Error("The string supplied is too short to be a phone number"); + null!=f&&(c=new z(e.toString()),sb(c,f,new z),a=pb(a,c.toString(),f,-1),2!=a&&4!=a&&5!=a&&(e=c));a=e.toString();c=a.length;if(2>c)throw Error("The string supplied is too short to be a phone number");if(17b;++b){d=void 0;if(a.length{if(b.hasOwnProperty(c))return !1;b[c]=1;return !0})}V.getSupportedRegionCodes=function(){return Yb(Za())};V.getSupportedCallingCodes=function(){return Yb(ab())}; + V.getExample=function(a,b){var c;b?c=hb(a,Sb(b)):c=hb(a,0);return (new V(c,a)).toJSON()};V.getAsYouType=function(a){return new Z(a)};V.getNumberFrom=function(a,b){try{a:{if(Wb){var e=Wb.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:gb(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.Aa=function(){return this.g.valid};V.prototype.isValid=V.prototype.Aa;V.prototype.za=function(){return this.g.possible};V.prototype.isPossible=V.prototype.za;V.prototype.getType=function(){return this.g.type};V.prototype.getType=V.prototype.getType;V.prototype.ua=function(){return "mobile"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isMobile=V.prototype.ua; + V.prototype.ta=function(){return "fixed-line"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isFixedLine=V.prototype.ta;V.prototype.va=function(a){return this.g.number[null==a?"e164":a]};V.prototype.getNumber=V.prototype.va;V.prototype.xa=function(){return this.g.regionCode};V.prototype.getRegionCode=V.prototype.xa;V.prototype.ia=function(){return Xb(this.g.regionCode)};V.prototype.getCountryCode=V.prototype.ia;function Z(a){this.j=a;this.h=new tb(a);this.l=this.g="";} + Z.prototype.sa=function(a){this.g+=a;var b=this.h;b.la=Ab(b,a);return this.l=b.la};Z.prototype.addChar=Z.prototype.sa;Z.prototype.Ba=function(){return this.l};Z.prototype.number=Z.prototype.Ba;Z.prototype.Ca=function(){return ""===this.g?this.l:this.reset(this.g.slice(0,this.g.length-1))};Z.prototype.removeChar=Z.prototype.Ca; + 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.ya&&(b.m=ub(b,b.ma));this.l=this.g="";if(a){b=0;for(var c=a.length;ba.length?!1:K(Sa,a)}function Wa(a){return K(Na,a)?L(a,Ga):L(a,Ea)}function Xa(a){var b=Wa(a.toString());A(a);a.g(b)}function Ya(){return Object.keys(G).filter(function(a){return isNaN(a)})}function Za(){return Object.keys(G).filter(function(a){return!isNaN(a)}).map(function(a){return parseInt(a,10)})}function $a(){var a=Object.keys(F);return[...Za(),...a.map(function(b){return parseInt(b,10)})]}function ab(a){return null!=a&&(1!=v(a,9)||-1!=r(a,9)[0])} -function L(a,b){for(var c=new z,d,e=a.length,f=0;fb?2:f[f.length-1]=e&&e<=d;++e)if(c=parseInt(a.substring(0,e),10),c in F)return b.g(a.substring(e)),c;return 0} -function qb(a,b,c,d,e){if(0==b.length)return 0;b=new z(b);var f;null!=c&&(f=n(c,11));null==f&&(f="NonMatch");var g=b.toString();if(0==g.length)f=20;else if(I.test(g))g=g.replace(I,""),A(b),b.g(Wa(g)),f=1;else{g=new RegExp(f);Xa(b);f=b.toString();if(0==f.search(g)){g=f.match(g)[0].length;var l=f.substring(g).match(Ja);l&&null!=l[1]&&0=b.h.length)throw Error("Phone number too short after IDD");a=pb(b, -d);if(0!=a)return p(e,1,a),a;throw Error("Invalid country calling code");}if(null!=c&&(f=u(c,10),g=""+f,l=b.toString(),0==l.lastIndexOf(g,0)&&(g=new z(l.substring(g.length)),l=n(c,1),l=new RegExp(u(l,2)),rb(g,c,null),g=g.toString(),!K(l,b.toString())&&K(l,g)||3==ob(a,b.toString(),c,-1))))return d.g(g),p(e,1,f),f;p(e,1,0);return 0} -function rb(a,b,c){var d=a.toString(),e=d.length,f=n(b,15);if(0!=e&&null!=f&&0!=f.length){var g=new RegExp("^(?:"+f+")");if(e=g.exec(d)){f=new RegExp(u(n(b,1),2));var l=K(f,d),q=e.length-1;b=n(b,16);if(null==b||0==b.length||null==e[q]||0==e[q].length){if(!l||K(f,d.substring(e[0].length)))null!=c&&0=b.length)e="";else{var f=b.indexOf(";",e);e=-1!==f?b.substring(e,f):b.substring(e)}var g=e;null==g?f=!0:0===g.length?f=!1:(f=Oa.exec(g),g=Pa.exec(g),f=null!==f||null!==g);if(!f)throw Error("The string supplied did not seem to be a phone number"); -null!=e?("+"===e.charAt(0)&&d.g(e),e=b.indexOf("tel:"),d.g(b.substring(0<=e?e+4:0,b.indexOf(";phone-context=")))):(e=d.g,b=null!=b?b:"",f=b.search(Ka),0<=f?(b=b.substring(f),b=b.replace(Ma,""),f=b.search(La),0<=f&&(b=b.substring(0,f))):b="",e.call(d,b));b=d.toString();e=b.indexOf(";isub=");0e.h.length)throw Error("The string supplied is too short to be a phone number"); -null!=f&&(c=new z(e.toString()),rb(c,f,new z),a=ob(a,c.toString(),f,-1),2!=a&&4!=a&&5!=a&&(e=c));a=e.toString();c=a.length;if(2>c)throw Error("The string supplied is too short to be a phone number");if(17b;++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; -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;ba.length?!1:K(Ta,a)}function Xa(a){return K(Oa,a)?L(a,Ha):L(a,Fa)}function Ya(a){var b=Xa(a.toString());A(a);a.g(b)}function Za(){return Object.keys(G).filter(function(a){return isNaN(a)})}function $a(){return Object.keys(G).filter(function(a){return!isNaN(a)}).map(function(a){return parseInt(a,10)})}function ab(){var a=Object.keys(F);return[...$a(),...a.map(function(b){return parseInt(b,10)})]}function bb(a){return null!=a&&(1!=v(a,9)||-1!=r(a,9)[0])} +function L(a,b){for(var c=new z,d,e=a.length,f=0;fb?2:f[f.length-1]=e&&e<=d;++e)if(c=parseInt(a.substring(0,e),10),c in F)return b.g(a.substring(e)),c;return 0} +function rb(a,b,c,d,e){if(0==b.length)return 0;b=new z(b);var f;null!=c&&(f=n(c,11));null==f&&(f="NonMatch");var g=b.toString();if(0==g.length)f=20;else if(I.test(g))g=g.replace(I,""),A(b),b.g(Xa(g)),f=1;else{g=new RegExp(f);Ya(b);f=b.toString();if(0==f.search(g)){g=f.match(g)[0].length;var l=f.substring(g).match(Ka);l&&null!=l[1]&&0=b.h.length)throw Error("Phone number too short after IDD");a=qb(b, +d);if(0!=a)return q(e,1,a),a;throw Error("Invalid country calling code");}if(null!=c&&(f=u(c,10),g=""+f,l=b.toString(),0==l.lastIndexOf(g,0)&&(g=new z(l.substring(g.length)),l=n(c,1),l=new RegExp(u(l,2)),sb(g,c,null),g=g.toString(),!K(l,b.toString())&&K(l,g)||3==pb(a,b.toString(),c,-1))))return d.g(g),q(e,1,f),f;q(e,1,0);return 0} +function sb(a,b,c){var d=a.toString(),e=d.length,f=n(b,15);if(0!=e&&null!=f&&0!=f.length){var g=new RegExp("^(?:"+f+")");if(e=g.exec(d)){f=new RegExp(u(n(b,1),2));var l=K(f,d),p=e.length-1;b=n(b,16);if(null==b||0==b.length||null==e[p]||0==e[p].length){if(!l||K(f,d.substring(e[0].length)))null!=c&&0=b.length)e="";else{var f=b.indexOf(";",e);e=-1!==f?b.substring(e,f):b.substring(e)}var g=e;null==g?f=!0:0===g.length?f=!1:(f=Pa.exec(g),g=Qa.exec(g),f=null!==f||null!==g);if(!f)throw Error("The string supplied did not seem to be a phone number"); +null!=e?("+"===e.charAt(0)&&d.g(e),e=b.indexOf("tel:"),d.g(b.substring(0<=e?e+4:0,b.indexOf(";phone-context=")))):(e=d.g,b=null!=b?b:"",f=b.search(La),0<=f?(b=b.substring(f),b=b.replace(Na,""),f=b.search(Ma),0<=f&&(b=b.substring(0,f))):b="",e.call(d,b));b=d.toString();e=b.indexOf(";isub=");0e.h.length)throw Error("The string supplied is too short to be a phone number"); +null!=f&&(c=new z(e.toString()),sb(c,f,new z),a=pb(a,c.toString(),f,-1),2!=a&&4!=a&&5!=a&&(e=c));a=e.toString();c=a.length;if(2>c)throw Error("The string supplied is too short to be a phone number");if(17b;++b){d=void 0;if(a.length{if(b.hasOwnProperty(c))return!1;b[c]=1;return!0})}V.getSupportedRegionCodes=function(){return Yb(Za())};V.getSupportedCallingCodes=function(){return Yb(ab())}; +V.getExample=function(a,b){var c;b?c=hb(a,Sb(b)):c=hb(a,0);return(new V(c,a)).toJSON()};V.getAsYouType=function(a){return new Z(a)};V.getNumberFrom=function(a,b){try{a:{if(Wb){var e=Wb.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:gb(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.Aa=function(){return this.g.valid};V.prototype.isValid=V.prototype.Aa;V.prototype.za=function(){return this.g.possible};V.prototype.isPossible=V.prototype.za;V.prototype.getType=function(){return this.g.type};V.prototype.getType=V.prototype.getType;V.prototype.ua=function(){return"mobile"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isMobile=V.prototype.ua; +V.prototype.ta=function(){return"fixed-line"===this.g.type||"fixed-line-or-mobile"===this.g.type};V.prototype.isFixedLine=V.prototype.ta;V.prototype.va=function(a){return this.g.number[null==a?"e164":a]};V.prototype.getNumber=V.prototype.va;V.prototype.xa=function(){return this.g.regionCode};V.prototype.getRegionCode=V.prototype.xa;V.prototype.ia=function(){return Xb(this.g.regionCode)};V.prototype.getCountryCode=V.prototype.ia;function Z(a){this.j=a;this.h=new tb(a);this.l=this.g=""} +Z.prototype.sa=function(a){this.g+=a;var b=this.h;b.la=Ab(b,a);return this.l=b.la};Z.prototype.addChar=Z.prototype.sa;Z.prototype.Ba=function(){return this.l};Z.prototype.number=Z.prototype.Ba;Z.prototype.Ca=function(){return""===this.g?this.l:this.reset(this.g.slice(0,this.g.length-1))};Z.prototype.removeChar=Z.prototype.Ca; +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.ya&&(b.m=ub(b,b.ma));this.l=this.g="";if(a){b=0;for(var c=a.length;b } ); - it( 'invalid phone number should still set type', ( ) => { + it( 'invalid phone number should still set type', ( ) => + { const pn = parsePhoneNumber( "0740521234", { regionCode: "US" } ); expect( pn.valid ).toBe( false ); @@ -335,7 +336,8 @@ describe( 'errors', ( ) => expect( pn.type ).toBe( 'unknown' ); } ); - it( 'should separate possible and valid', ( ) => { + it( 'should separate possible and valid', ( ) => + { const pn = parsePhoneNumber( "+11111111111" ); expect( pn.valid ).toBe( false ); @@ -345,6 +347,38 @@ describe( 'errors', ( ) => } ); } ); + +describe( 'short numbers', ( ) => +{ + it( 'should handle short numbers that are real numbes', ( ) => + { + var pn = parsePhoneNumber( '0707123456', { regionCode: 'SE' } ); + expect( pn.valid ).toBe( true ); + expect( pn.possible ).toBe( true ); + expect( pn.shortValid ).toBe( false ); + expect( pn.shortPossible ).toBe( false ); + } ); + + + it( 'should handle short numbers that are invalid', ( ) => + { + var pn = parsePhoneNumber( '12', { regionCode: 'NO' } ); + expect( pn.valid ).toBe( false ); + expect( pn.possible ).toBe( false ); + expect( pn.shortValid ).toBe( false ); + expect( pn.shortPossible ).toBe( false ); + } ); + + it( 'should handle short numbers that are valid', ( ) => + { + var pn = parsePhoneNumber( '116117', { regionCode: 'NO' } ); + expect( pn.valid ).toBe( false ); + expect( pn.possible ).toBe( false ); + expect( pn.shortValid ).toBe( true ); + expect( pn.shortPossible ).toBe( true ); + } ); +} ); + describe( 'getNumberFrom', ( ) => { it( 'Using weakmap', ( ) =>