mozambique-utils
Version:
Easy to use parsers and validators for mozambican (🇲🇿) data-type formatted strings.
26 lines (24 loc) • 12.9 kB
JavaScript
/**
* The MIT License (MIT)
*
* Copyright (c) 2021 Kishan Jadav
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).mozambiqueUtils={})}(this,(function(t){"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function r(t){var e={exports:{}};return t(e,e.exports),e.exports}var n="object"==typeof e&&e&&e.Object===Object&&e,o="object"==typeof self&&self&&self.Object===Object&&self,a=n||o||Function("return this")(),i=a.Symbol,u=Object.prototype,c=u.hasOwnProperty,l=u.toString,s=i?i.toStringTag:void 0;var f=function(t){var e=c.call(t,s),r=t[s];try{t[s]=void 0;var n=!0}catch(t){}var o=l.call(t);return n&&(e?t[s]=r:delete t[s]),o},p=Object.prototype.toString;var v=function(t){return p.call(t)},d=i?i.toStringTag:void 0;var h=function(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":d&&d in Object(t)?f(t):v(t)},b=Array.isArray;var y=function(t){return null!=t&&"object"==typeof t};var _=function(t){return"string"==typeof t||!b(t)&&y(t)&&"[object String]"==h(t)},j=function(t){if(!_(t))throw new Error("Expected a string")},m={all:"234567",tmcel:"23",movitel:"67",vodacom:"45"},g={tete:"252",pemba:"272",beira:"23",chokwe:"281",manica:"251",xaixai:"282",maputo:"21",nampula:"26",lichinga:"271",inhambane:"293",quelimane:"24",vilanculos:"258"},w=/^((\+|00)?258)?((8[234567])\d{7})$/,O=/^((\+|00)?258)?((2(5[0128]|7[12]|8[12]|93))(\d{5})|(2[1346])(\d{6}))$/,A=/^(2(5[0128]|7[12]|8[12]|93)|2[1346])$/,x=/^8[45]$/,$=/^8[23]$/,S=/^8[67]$/,T=/^\d{9}$/,z=/^(MZ[0-9]{2})\d{21}$/i,M=function(t){return j(t),x.test(t)?{name:"Vodacom Moçambique",shortName:"Vodacom"}:S.test(t)?{name:"Movitel, S.A.",shortName:"Movitel"}:$.test(t)||A.test(t)?{name:"Moçambique Telecom, S.A.",shortName:"Tmcel"}:null},C=function(t){return t&&(t=t.replace(/[\s-]/g,"")),t},E={21:"maputo",23:"beira",24:"quelimane",26:"nampula",258:"vilanculos",251:"manica",252:"tete",271:"lichinga",272:"pemba",281:"chokwe",282:"xai-xai",293:"inhambane"},F=Object.prototype;var P=function(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||F)};var k=function(t,e){return function(r){return t(e(r))}}(Object.keys,Object),N=Object.prototype.hasOwnProperty;var I=function(t){if(!P(t))return k(t);var e=[];for(var r in Object(t))N.call(t,r)&&"constructor"!=r&&e.push(r);return e};var q=function(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)};var V,U=function(t){if(!q(t))return!1;var e=h(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e},B=a["__core-js_shared__"],D=(V=/[^.]+$/.exec(B&&B.keys&&B.keys.IE_PROTO||""))?"Symbol(src)_1."+V:"";var R=function(t){return!!D&&D in t},L=Function.prototype.toString;var W=function(t){if(null!=t){try{return L.call(t)}catch(t){}try{return t+""}catch(t){}}return""},G=/^\[object .+?Constructor\]$/,Z=Function.prototype,H=Object.prototype,J=Z.toString,K=H.hasOwnProperty,Q=RegExp("^"+J.call(K).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");var X=function(t){return!(!q(t)||R(t))&&(U(t)?Q:G).test(W(t))};var Y=function(t,e){return null==t?void 0:t[e]};var tt=function(t,e){var r=Y(t,e);return X(r)?r:void 0},et=tt(a,"DataView"),rt=tt(a,"Map"),nt=tt(a,"Promise"),ot=tt(a,"Set"),at=tt(a,"WeakMap"),it="[object Map]",ut="[object Promise]",ct="[object Set]",lt="[object WeakMap]",st="[object DataView]",ft=W(et),pt=W(rt),vt=W(nt),dt=W(ot),ht=W(at),bt=h;(et&&bt(new et(new ArrayBuffer(1)))!=st||rt&&bt(new rt)!=it||nt&&bt(nt.resolve())!=ut||ot&&bt(new ot)!=ct||at&&bt(new at)!=lt)&&(bt=function(t){var e=h(t),r="[object Object]"==e?t.constructor:void 0,n=r?W(r):"";if(n)switch(n){case ft:return st;case pt:return it;case vt:return ut;case dt:return ct;case ht:return lt}return e});var yt=bt;var _t=function(t){return y(t)&&"[object Arguments]"==h(t)},jt=Object.prototype,mt=jt.hasOwnProperty,gt=jt.propertyIsEnumerable,wt=_t(function(){return arguments}())?_t:function(t){return y(t)&&mt.call(t,"callee")&&!gt.call(t,"callee")};var Ot=function(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=9007199254740991};var At=function(t){return null!=t&&Ot(t.length)&&!U(t)};var xt=function(){return!1},$t=r((function(t,e){var r=e&&!e.nodeType&&e,n=r&&t&&!t.nodeType&&t,o=n&&n.exports===r?a.Buffer:void 0,i=(o?o.isBuffer:void 0)||xt;t.exports=i})),St={};St["[object Float32Array]"]=St["[object Float64Array]"]=St["[object Int8Array]"]=St["[object Int16Array]"]=St["[object Int32Array]"]=St["[object Uint8Array]"]=St["[object Uint8ClampedArray]"]=St["[object Uint16Array]"]=St["[object Uint32Array]"]=!0,St["[object Arguments]"]=St["[object Array]"]=St["[object ArrayBuffer]"]=St["[object Boolean]"]=St["[object DataView]"]=St["[object Date]"]=St["[object Error]"]=St["[object Function]"]=St["[object Map]"]=St["[object Number]"]=St["[object Object]"]=St["[object RegExp]"]=St["[object Set]"]=St["[object String]"]=St["[object WeakMap]"]=!1;var Tt=function(t){return y(t)&&Ot(t.length)&&!!St[h(t)]};var zt=function(t){return function(e){return t(e)}},Mt=r((function(t,e){var r=e&&!e.nodeType&&e,o=r&&t&&!t.nodeType&&t,a=o&&o.exports===r&&n.process,i=function(){try{var t=o&&o.require&&o.require("util").types;return t||a&&a.binding&&a.binding("util")}catch(t){}}();t.exports=i})),Ct=Mt&&Mt.isTypedArray,Et=Ct?zt(Ct):Tt,Ft=Object.prototype.hasOwnProperty;var Pt=function(t){if(null==t)return!0;if(At(t)&&(b(t)||"string"==typeof t||"function"==typeof t.splice||$t(t)||Et(t)||wt(t)))return!t.length;var e=yt(t);if("[object Map]"==e||"[object Set]"==e)return!t.size;if(P(t))return!I(t).length;for(var r in t)if(Ft.call(t,r))return!1;return!0};var kt=function(t){return"symbol"==typeof t||y(t)&&"[object Symbol]"==h(t)},Nt=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,It=/^\w*$/;var qt=function(t,e){if(b(t))return!1;var r=typeof t;return!("number"!=r&&"symbol"!=r&&"boolean"!=r&&null!=t&&!kt(t))||(It.test(t)||!Nt.test(t)||null!=e&&t in Object(e))},Vt=tt(Object,"create");var Ut=function(){this.__data__=Vt?Vt(null):{},this.size=0};var Bt=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},Dt=Object.prototype.hasOwnProperty;var Rt=function(t){var e=this.__data__;if(Vt){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return Dt.call(e,t)?e[t]:void 0},Lt=Object.prototype.hasOwnProperty;var Wt=function(t){var e=this.__data__;return Vt?void 0!==e[t]:Lt.call(e,t)};var Gt=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=Vt&&void 0===e?"__lodash_hash_undefined__":e,this};function Zt(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}Zt.prototype.clear=Ut,Zt.prototype.delete=Bt,Zt.prototype.get=Rt,Zt.prototype.has=Wt,Zt.prototype.set=Gt;var Ht=Zt;var Jt=function(){this.__data__=[],this.size=0};var Kt=function(t,e){return t===e||t!=t&&e!=e};var Qt=function(t,e){for(var r=t.length;r--;)if(Kt(t[r][0],e))return r;return-1},Xt=Array.prototype.splice;var Yt=function(t){var e=this.__data__,r=Qt(e,t);return!(r<0)&&(r==e.length-1?e.pop():Xt.call(e,r,1),--this.size,!0)};var te=function(t){var e=this.__data__,r=Qt(e,t);return r<0?void 0:e[r][1]};var ee=function(t){return Qt(this.__data__,t)>-1};var re=function(t,e){var r=this.__data__,n=Qt(r,t);return n<0?(++this.size,r.push([t,e])):r[n][1]=e,this};function ne(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}ne.prototype.clear=Jt,ne.prototype.delete=Yt,ne.prototype.get=te,ne.prototype.has=ee,ne.prototype.set=re;var oe=ne;var ae=function(){this.size=0,this.__data__={hash:new Ht,map:new(rt||oe),string:new Ht}};var ie=function(t){var e=typeof t;return"string"==e||"number"==e||"symbol"==e||"boolean"==e?"__proto__"!==t:null===t};var ue=function(t,e){var r=t.__data__;return ie(e)?r["string"==typeof e?"string":"hash"]:r.map};var ce=function(t){var e=ue(this,t).delete(t);return this.size-=e?1:0,e};var le=function(t){return ue(this,t).get(t)};var se=function(t){return ue(this,t).has(t)};var fe=function(t,e){var r=ue(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};function pe(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}pe.prototype.clear=ae,pe.prototype.delete=ce,pe.prototype.get=le,pe.prototype.has=se,pe.prototype.set=fe;var ve=pe;function de(t,e){if("function"!=typeof t||null!=e&&"function"!=typeof e)throw new TypeError("Expected a function");var r=function(){var n=arguments,o=e?e.apply(this,n):n[0],a=r.cache;if(a.has(o))return a.get(o);var i=t.apply(this,n);return r.cache=a.set(o,i)||a,i};return r.cache=new(de.Cache||ve),r}de.Cache=ve;var he=de;var be=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,ye=/\\(\\)?/g,_e=function(t){var e=he(t,(function(t){return 500===r.size&&r.clear(),t})),r=e.cache;return e}((function(t){var e=[];return 46===t.charCodeAt(0)&&e.push(""),t.replace(be,(function(t,r,n,o){e.push(n?o.replace(ye,"$1"):r||t)})),e}));var je=function(t,e){for(var r=-1,n=null==t?0:t.length,o=Array(n);++r<n;)o[r]=e(t[r],r,t);return o},me=i?i.prototype:void 0,ge=me?me.toString:void 0;var we=function t(e){if("string"==typeof e)return e;if(b(e))return je(e,t)+"";if(kt(e))return ge?ge.call(e):"";var r=e+"";return"0"==r&&1/e==-Infinity?"-0":r};var Oe=function(t){return null==t?"":we(t)};var Ae=function(t,e){return b(t)?t:qt(t,e)?[t]:_e(Oe(t))};var xe=function(t){if("string"==typeof t||kt(t))return t;var e=t+"";return"0"==e&&1/t==-Infinity?"-0":e};var $e=function(t,e){for(var r=0,n=(e=Ae(e,t)).length;null!=t&&r<n;)t=t[xe(e[r++])];return r&&r==n?t:void 0};var Se=function(t,e,r){var n=null==t?void 0:$e(t,e);return void 0===n?r:n};t.isIBANValid=function(t){if(j(t),t=(t=C(t)).toLowerCase()){if(!z.test(t))return!1;var e=(t.substring(4,t.length)+t.substring(0,4)).replace("mz","2235");return 1===Array.from(e).map((function(t){return parseInt(t)})).reduce((function(t,e){return(10*t+e)%97}),0)}return!1},t.isLandlineNumberValid=function(t,e){j(t),t=C(t);var r=(e=e||{}).allowedRegions||[];return function(t,e){var r;switch(t){case"required":r="((\\+|00)?258)";break;case"off":r="()";break;case"optional":default:r="((\\+|00)?258)?"}var n="";return Pt(e)?n="((2(5[0128]|7[12]|8[12]|93))(\\d{5})|(2[1346])(\\d{6}))":(n+="(",e.forEach((function(t,r){var o=Se(g,t),a=8-o.length;n+="("+o+")(\\d{"+a+"})",e.length-1===r||(n+="|")})),n+=")"),new RegExp("^"+r+n+"$")}(e.countryCode||"optional",r).test(t)},t.isMobileNumberValid=function(t,e){j(t),t=C(t);var r=(e=e||{}).allowedOperators||[];return function(t,e){var r,n;switch(t){case"required":r="((\\+|00)?258)";break;case"off":r="()";break;case"optional":default:r="((\\+|00)?258)?"}return Pt(e)?n=m.all:e.forEach((function(t){var e=Se(m,t);e&&(n+=e)})),new RegExp("^"+r+"((8["+n+"])\\d{7})$")}(e.countryCode||"optional",r).test(t)},t.isNUITValid=function(t){return j(t),!!(t=C(t))&&T.test(t)},t.parseLandlineNumber=function(t){j(t);var e,r=!1,n=null,o=(t=C(t)).match(O);if(o){r=!0;var a=t,i=void 0!==o[1],u=o[3],c="+258"+u,l=o[4]?o[4]:o[7],s=(j(e=l),A.test(e)?E[e]:null);n={number:a,localFormat:u,internationalFormat:c,includesCountryCode:i,nationalDestinationCode:l,operator:M(l),region:s,lineType:"landline"}}return{valid:r,data:n}},t.parseMobileNumber=function(t){j(t);var e=!1,r=null,n=(t=C(t)).match(w);if(n){e=!0;var o=t,a=void 0!==n[1],i=n[3],u="+258"+i,c=n[4];r={number:o,localFormat:i,internationalFormat:u,includesCountryCode:a,nationalDestinationCode:c,operator:M(c),lineType:"mobile"}}return{valid:e,data:r}},t.version="1.1.1",Object.defineProperty(t,"__esModule",{value:!0})}));