{"record":{"id":"4170185b0eec4952","repo":"nosir/cleave.js","slug":"invalid-region-code-417018","errorCode":null,"errorMessage":"Invalid region code: ","messagePattern":"Invalid region code: ","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/addons/phone-type-formatter.la.js","lineNumber":64,"sourceCode":" Copyright (C) 2010 The Libphonenumber Authors.\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n*/\nfunction M(){this.a={}}M.b=function(){return M.a?M.a:M.a=new M};\nvar oa={0:\"0\",1:\"1\",2:\"2\",3:\"3\",4:\"4\",5:\"5\",6:\"6\",7:\"7\",8:\"8\",9:\"9\",\"\\uff10\":\"0\",\"\\uff11\":\"1\",\"\\uff12\":\"2\",\"\\uff13\":\"3\",\"\\uff14\":\"4\",\"\\uff15\":\"5\",\"\\uff16\":\"6\",\"\\uff17\":\"7\",\"\\uff18\":\"8\",\"\\uff19\":\"9\",\"\\u0660\":\"0\",\"\\u0661\":\"1\",\"\\u0662\":\"2\",\"\\u0663\":\"3\",\"\\u0664\":\"4\",\"\\u0665\":\"5\",\"\\u0666\":\"6\",\"\\u0667\":\"7\",\"\\u0668\":\"8\",\"\\u0669\":\"9\",\"\\u06f0\":\"0\",\"\\u06f1\":\"1\",\"\\u06f2\":\"2\",\"\\u06f3\":\"3\",\"\\u06f4\":\"4\",\"\\u06f5\":\"5\",\"\\u06f6\":\"6\",\"\\u06f7\":\"7\",\"\\u06f8\":\"8\",\"\\u06f9\":\"9\"},pa=RegExp(\"[+\\uff0b]+\"),qa=RegExp(\"([0-9\\uff10-\\uff19\\u0660-\\u0669\\u06f0-\\u06f9])\"),\nra=/^\\(?\\$1\\)?$/;function N(a){return 0==a.length||ra.test(a)}function O(a,c){if(null==c)return null;c=c.toUpperCase();var b=a.a[c];if(null==b){b=L[c];if(null==b)return null;b=(new J).a(E.j(),b);a.a[c]=b}return b}function P(a){a=K[a];return null==a?\"ZZ\":a[0]};function Q(a){this.H=RegExp(\"\\u2008\");this.C=\"\";this.m=new n;this.w=\"\";this.i=new n;this.u=new n;this.l=!0;this.A=this.o=this.F=!1;this.G=M.b();this.s=0;this.b=new n;this.B=!1;this.h=\"\";this.a=new n;this.f=[];this.D=a;this.J=this.g=R(this,this.D)}var S=new E;r(S,11,\"NA\");\nvar sa=/\\[([^\\[\\]])*\\]/g,ua=/\\d(?=[^,}][^,}])/g,va=RegExp(\"^[-x\\u2010-\\u2015\\u2212\\u30fc\\uff0d-\\uff0f \\u00a0\\u00ad\\u200b\\u2060\\u3000()\\uff08\\uff09\\uff3b\\uff3d.\\\\[\\\\]/~\\u2053\\u223c\\uff5e]*(\\\\$\\\\d[-x\\u2010-\\u2015\\u2212\\u30fc\\uff0d-\\uff0f \\u00a0\\u00ad\\u200b\\u2060\\u3000()\\uff08\\uff09\\uff3b\\uff3d.\\\\[\\\\]/~\\u2053\\u223c\\uff5e]*)+$\"),T=/[- ]/;\nfunction R(a,c){var b;if(null!=c&&isNaN(c)&&c.toUpperCase()in L){b=O(a.G,c);if(null==b)throw Error(\"Invalid region code: \"+c);b=x(b,10)}else b=0;b=O(a.G,P(b));return null!=b?b:S}\nfunction U(a){for(var c=a.f.length,b=0;b<c;++b){var d=a.f[b],f=x(d,1);if(a.w==f)return!1;var e;e=a;var g=d,k=x(g,1);if(-1!=k.indexOf(\"|\"))e=!1;else{k=k.replace(sa,\"\\\\d\");k=k.replace(ua,\"\\\\d\");p(e.m);var m;m=e;var g=x(g,2),t=\"999999999999999\".match(k)[0];t.length<m.a.b.length?m=\"\":(m=t.replace(new RegExp(k,\"g\"),g),m=m.replace(RegExp(\"9\",\"g\"),\"\\u2008\"));0<m.length?(e.m.a(m),e=!0):e=!1}if(e)return a.w=f,a.B=T.test(w(d,4)),a.s=0,!0}return a.l=!1}\nfunction V(a,c){for(var b=[],d=c.length-3,f=a.f.length,e=0;e<f;++e){var g=a.f[e];0==y(g,3)?b.push(a.f[e]):(g=w(g,3,Math.min(d,y(g,3)-1)),0==c.search(g)&&b.push(a.f[e]))}a.f=b}Q.prototype.K=function(){this.C=\"\";p(this.i);p(this.u);p(this.m);this.s=0;this.w=\"\";p(this.b);this.h=\"\";p(this.a);this.l=!0;this.A=this.o=this.F=!1;this.f=[];this.B=!1;this.g!=this.J&&(this.g=R(this,this.D))};Q.prototype.L=function(a){return this.C=wa(this,a)};\nfunction wa(a,c){a.i.a(c);var b=c;if(qa.test(b)||1==a.i.b.length&&pa.test(b)){var b=c,d;\"+\"==b?(d=b,a.u.a(b)):(d=oa[b],a.u.a(d),a.a.a(d));c=d}else a.l=!1,a.F=!0;if(!a.l){if(!a.F)if(W(a)){if(X(a))return xa(a)}else if(0<a.h.length&&(b=a.a.toString(),p(a.a),a.a.a(a.h),a.a.a(b),b=a.b.toString(),d=b.lastIndexOf(a.h),p(a.b),a.b.a(b.substring(0,d))),a.h!=ya(a))return a.b.a(\" \"),xa(a);return a.i.toString()}switch(a.u.b.length){case 0:case 1:case 2:return a.i.toString();case 3:if(W(a))a.A=!0;else return a.h=\nya(a),Y(a);default:if(a.A)return X(a)&&(a.A=!1),a.b.toString()+a.a.toString();if(0<a.f.length){b=za(a,c);d=Aa(a);if(0<d.length)return d;V(a,a.a.toString());return U(a)?Ba(a):a.l?Z(a,b):a.i.toString()}return Y(a)}}function xa(a){a.l=!0;a.A=!1;a.f=[];a.s=0;p(a.m);a.w=\"\";return Y(a)}function Aa(a){for(var c=a.a.toString(),b=a.f.length,d=0;d<b;++d){var f=a.f[d],e=x(f,1);if((new RegExp(\"^(?:\"+e+\")$\")).test(c))return a.B=T.test(w(f,4)),c=c.replace(new RegExp(e,\"g\"),w(f,2)),Z(a,c)}return\"\"}\nfunction Z(a,c){var b=a.b.b.length;return a.B&&0<b&&\" \"!=a.b.toString().charAt(b-1)?a.b+\" \"+c:a.b+c}function Y(a){var c=a.a.toString();if(3<=c.length){for(var b=a.o&&0==a.h.length&&0<y(a.g,20)?v(a.g,20)||[]:v(a.g,19)||[],d=b.length,f=0;f<d;++f){var e=b[f];0<a.h.length&&N(x(e,4))&&!w(e,6)&&null==e.a[5]||(0!=a.h.length||a.o||N(x(e,4))||w(e,6))&&va.test(x(e,2))&&a.f.push(e)}V(a,c);c=Aa(a);return 0<c.length?c:U(a)?Ba(a):a.i.toString()}return Z(a,c)}\nfunction Ba(a){var c=a.a.toString(),b=c.length;if(0<b){for(var d=\"\",f=0;f<b;f++)d=za(a,c.charAt(f));return a.l?Z(a,d):a.i.toString()}return a.b.toString()}\nfunction ya(a){var c=a.a.toString(),b=0,d;1!=w(a.g,10)?d=!1:(d=a.a.toString(),d=\"1\"==d.charAt(0)&&\"0\"!=d.charAt(1)&&\"1\"!=d.charAt(1));d?(b=1,a.b.a(\"1\").a(\" \"),a.o=!0):null!=a.g.a[15]&&(d=new RegExp(\"^(?:\"+w(a.g,15)+\")\"),d=c.match(d),null!=d&&null!=d[0]&&0<d[0].length&&(a.o=!0,b=d[0].length,a.b.a(c.substring(0,b))));p(a.a);a.a.a(c.substring(b));return c.substring(0,b)}\nfunction W(a){var c=a.u.toString(),b=new RegExp(\"^(?:\\\\+|\"+w(a.g,11)+\")\"),b=c.match(b);return null!=b&&null!=b[0]&&0<b[0].length?(a.o=!0,b=b[0].length,p(a.a),a.a.a(c.substring(b)),p(a.b),a.b.a(c.substring(0,b)),\"+\"!=c.charAt(0)&&a.b.a(\" \"),!0):!1}\nfunction X(a){if(0==a.a.b.length)return!1;var c=new n,b;a:{b=a.a.toString();if(0!=b.length&&\"0\"!=b.charAt(0))for(var d,f=b.length,e=1;3>=e&&e<=f;++e)if(d=parseInt(b.substring(0,e),10),d in K){c.a(b.substring(e));b=d;break a}b=0}if(0==b)return!1;p(a.a);a.a.a(c.toString());c=P(b);\"001\"==c?a.g=O(a.G,\"\"+b):c!=a.D&&(a.g=R(a,c));a.b.a(\"\"+b).a(\" \");a.h=\"\";return!0}\nfunction za(a,c){var b=a.m.toString();if(0<=b.substring(a.s).search(a.H)){var d=b.search(a.H),b=b.replace(a.H,c);p(a.m);a.m.a(b);a.s=d;return b.substring(0,a.s+1)}1==a.f.length&&(a.l=!1);a.w=\"\";return a.i.toString()};h(\"Cleave.AsYouTypeFormatter\",Q);h(\"Cleave.AsYouTypeFormatter.prototype.inputDigit\",Q.prototype.L);h(\"Cleave.AsYouTypeFormatter.prototype.clear\",Q.prototype.K);}.call((typeof global===\"object\"&&global)?global:window);\n","sourceCodeStart":46,"sourceCodeEnd":75,"githubUrl":"https://github.com/nosir/cleave.js/blob/a966c95cf5193f223485a10eec8405bb32b8f6dd/src/addons/phone-type-formatter.la.js#L46-L75","documentation":"The Laos (la) phone-type-formatter addon bundles the same libphonenumber formatter; its region-validation function R throws 'Invalid region code: <code>' when the supplied region is empty/undefined or not present in the bundled metadata map L. Thrown at formatter construction (Q constructor invokes R) or on reset when the region changes to an invalid value.","triggerScenarios":"Calling the la addon's formatter API with an empty string region (e.g. new formatter('') or formatter('LA-NOT-A-CODE')), or re-calling it after a reset with a region that never resolved.","commonSituations":"Country dropdown submitting '' as its initial value; i18n locale string passed instead of a region code (e.g. 'lo' vs 'LA'); tests instantiating the formatter with no args; build/codegen producing an addon wired with a blank region constant.","solutions":["Pass the ISO 3166-1 alpha-2 code 'LA' (uppercase, trimmed) to the formatter.","Validate the region against the addon's supported set before calling; return early if empty.","Fix the caller that supplies the region (select box default, locale mapper) so it never yields ''.","Instantiate lazily and cache, retrying only once a valid region is available."],"exampleFix":"// before\nfunction formatLa(phone, region) {\n  return new phoneFormatter(region).format(phone); // region may be ''\n}\n// after\nfunction formatLa(phone, region) {\n  const r = (region || '').trim().toUpperCase();\n  if (!/^[A-Z]{2}$/.test(r)) return phone;\n  return new phoneFormatter(r).format(phone);\n}","handlingStrategy":"validation","validationCode":"const normalized = (region || '').trim().toUpperCase();\nif (!/^[A-Z]{2}$/.test(normalized)) throw new TypeError('region must be a 2-letter ISO code, got: ' + JSON.stringify(region));","typeGuard":"function isIsoRegion(v) {\n  return typeof v === 'string' && /^[A-Z]{2}$/.test(v.trim().toUpperCase());\n}","tryCatchPattern":"let out;\ntry {\n  out = new phoneFormatter(region).format(phone);\n} catch (e) {\n  if (/Invalid region code/.test(e.message)) out = phone; // pass through unformatted\n  else throw e;\n}","preventionTips":["Map locale strings to region codes explicitly ('lo' -> 'LA'); never pass a locale as a region.","Avoid empty initial <option> values feeding the formatter; gate on a 'country chosen' flag.","Verify the addon file matches the region (phone-type-formatter.la.js is for LA only).","Sanitize region input in one shared helper so all call sites are protected."],"tags":["libphonenumber","phone-formatting","region-code","invalid-argument"],"backgroundTag":"invalid-region-code","analyzedSha":"a966c95cf5193f223485a10eec8405bb32b8f6dd","analyzedAt":"2026-09-02T23:44:35.488Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}