mozambique-utils
Version:
Easy to use parsers and validators for mozambican (🇲🇿) data-type formatted strings.
26 lines (24 loc) • 4.08 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.
*/
import e from"lodash/isString";import a from"lodash/isEmpty";import t from"lodash/get";var n="1.1.1",r=function(a){if(!e(a))throw new Error("Expected a string")},o={all:"234567",tmcel:"23",movitel:"67",vodacom:"45"},i={tete:"252",pemba:"272",beira:"23",chokwe:"281",manica:"251",xaixai:"282",maputo:"21",nampula:"26",lichinga:"271",inhambane:"293",quelimane:"24",vilanculos:"258"},l=/^((\+|00)?258)?((8[234567])\d{7})$/,u=/^((\+|00)?258)?((2(5[0128]|7[12]|8[12]|93))(\d{5})|(2[1346])(\d{6}))$/,c=/^(2(5[0128]|7[12]|8[12]|93)|2[1346])$/,m=/^8[45]$/,s=/^8[23]$/,d=/^8[67]$/,f=/^\d{9}$/,p=/^(MZ[0-9]{2})\d{21}$/i,v=function(e){return r(e),m.test(e)?{name:"Vodacom Moçambique",shortName:"Vodacom"}:d.test(e)?{name:"Movitel, S.A.",shortName:"Movitel"}:s.test(e)||c.test(e)?{name:"Moçambique Telecom, S.A.",shortName:"Tmcel"}:null},h=function(e){return e&&(e=e.replace(/[\s-]/g,"")),e},b=function(e){r(e);var a=!1,t=null,n=(e=h(e)).match(l);if(n){a=!0;var o=e,i=void 0!==n[1],u=n[3],c="+258"+u,m=n[4];t={number:o,localFormat:u,internationalFormat:c,includesCountryCode:i,nationalDestinationCode:m,operator:v(m),lineType:"mobile"}}return{valid:a,data:t}},g={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"},w=function(e){r(e);var a,t=!1,n=null,o=(e=h(e)).match(u);if(o){t=!0;var i=e,l=void 0!==o[1],m=o[3],s="+258"+m,d=o[4]?o[4]:o[7],f=(r(a=d),c.test(a)?g[a]:null);n={number:i,localFormat:m,internationalFormat:s,includesCountryCode:l,nationalDestinationCode:d,operator:v(d),region:f,lineType:"landline"}}return{valid:t,data:n}},$=function(e,n){r(e),e=h(e);var i=(n=n||{}).allowedOperators||[];return function(e,n){var r,i;switch(e){case"required":r="((\\+|00)?258)";break;case"off":r="()";break;case"optional":default:r="((\\+|00)?258)?"}return a(n)?i=o.all:n.forEach((function(e){var a=t(o,e);a&&(i+=a)})),new RegExp("^"+r+"((8["+i+"])\\d{7})$")}(n.countryCode||"optional",i).test(e)},C=function(e){return r(e),!!(e=h(e))&&f.test(e)},x=function(e){if(r(e),e=(e=h(e)).toLowerCase()){if(!p.test(e))return!1;var a=(e.substring(4,e.length)+e.substring(0,4)).replace("mz","2235");return 1===Array.from(a).map((function(e){return parseInt(e)})).reduce((function(e,a){return(10*e+a)%97}),0)}return!1},y=function(e,n){r(e),e=h(e);var o=(n=n||{}).allowedRegions||[];return function(e,n){var r;switch(e){case"required":r="((\\+|00)?258)";break;case"off":r="()";break;case"optional":default:r="((\\+|00)?258)?"}var o="";return a(n)?o="((2(5[0128]|7[12]|8[12]|93))(\\d{5})|(2[1346])(\\d{6}))":(o+="(",n.forEach((function(e,a){var r=t(i,e),l=8-r.length;o+="("+r+")(\\d{"+l+"})",n.length-1===a||(o+="|")})),o+=")"),new RegExp("^"+r+o+"$")}(n.countryCode||"optional",o).test(e)};export{x as isIBANValid,y as isLandlineNumberValid,$ as isMobileNumberValid,C as isNUITValid,w as parseLandlineNumber,b as parseMobileNumber,n as version};