UNPKG

mozambique-utils

Version:

Easy to use parsers and validators for mozambican (🇲🇿) data-type formatted strings.

26 lines (24 loc) • 4.28 kB
/** * 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. */ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("lodash/isString"),a=require("lodash/isEmpty"),t=require("lodash/get");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(e),o=n(a),i=n(t),l=function(e){if(!r.default(e))throw new Error("Expected a string")},u={all:"234567",tmcel:"23",movitel:"67",vodacom:"45"},s={tete:"252",pemba:"272",beira:"23",chokwe:"281",manica:"251",xaixai:"282",maputo:"21",nampula:"26",lichinga:"271",inhambane:"293",quelimane:"24",vilanculos:"258"},c=/^((\+|00)?258)?((8[234567])\d{7})$/,d=/^((\+|00)?258)?((2(5[0128]|7[12]|8[12]|93))(\d{5})|(2[1346])(\d{6}))$/,m=/^(2(5[0128]|7[12]|8[12]|93)|2[1346])$/,f=/^8[45]$/,p=/^8[23]$/,v=/^8[67]$/,b=/^\d{9}$/,h=/^(MZ[0-9]{2})\d{21}$/i,g=function(e){return l(e),f.test(e)?{name:"Vodacom Moçambique",shortName:"Vodacom"}:v.test(e)?{name:"Movitel, S.A.",shortName:"Movitel"}:p.test(e)||m.test(e)?{name:"Moçambique Telecom, S.A.",shortName:"Tmcel"}:null},x=function(e){return e&&(e=e.replace(/[\s-]/g,"")),e},w={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"};exports.isIBANValid=function(e){if(l(e),e=(e=x(e)).toLowerCase()){if(!h.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},exports.isLandlineNumberValid=function(e,a){l(e),e=x(e);var t=(a=a||{}).allowedRegions||[];return function(e,a){var t;switch(e){case"required":t="((\\+|00)?258)";break;case"off":t="()";break;case"optional":default:t="((\\+|00)?258)?"}var n="";return o.default(a)?n="((2(5[0128]|7[12]|8[12]|93))(\\d{5})|(2[1346])(\\d{6}))":(n+="(",a.forEach((function(e,t){var r=i.default(s,e),o=8-r.length;n+="("+r+")(\\d{"+o+"})",a.length-1===t||(n+="|")})),n+=")"),new RegExp("^"+t+n+"$")}(a.countryCode||"optional",t).test(e)},exports.isMobileNumberValid=function(e,a){l(e),e=x(e);var t=(a=a||{}).allowedOperators||[];return function(e,a){var t,n;switch(e){case"required":t="((\\+|00)?258)";break;case"off":t="()";break;case"optional":default:t="((\\+|00)?258)?"}return o.default(a)?n=u.all:a.forEach((function(e){var a=i.default(u,e);a&&(n+=a)})),new RegExp("^"+t+"((8["+n+"])\\d{7})$")}(a.countryCode||"optional",t).test(e)},exports.isNUITValid=function(e){return l(e),!!(e=x(e))&&b.test(e)},exports.parseLandlineNumber=function(e){l(e);var a,t=!1,n=null,r=(e=x(e)).match(d);if(r){t=!0;var o=e,i=void 0!==r[1],u=r[3],s="+258"+u,c=r[4]?r[4]:r[7],f=(l(a=c),m.test(a)?w[a]:null);n={number:o,localFormat:u,internationalFormat:s,includesCountryCode:i,nationalDestinationCode:c,operator:g(c),region:f,lineType:"landline"}}return{valid:t,data:n}},exports.parseMobileNumber=function(e){l(e);var a=!1,t=null,n=(e=x(e)).match(c);if(n){a=!0;var r=e,o=void 0!==n[1],i=n[3],u="+258"+i,s=n[4];t={number:r,localFormat:i,internationalFormat:u,includesCountryCode:o,nationalDestinationCode:s,operator:g(s),lineType:"mobile"}}return{valid:a,data:t}},exports.version="1.1.1";