nig-utils
Version:
A fully-typed, production-grade utility library for Nigerian developers
29 lines • 5.28 kB
JavaScript
;var d={MTN:{provider:"MTN",prefixes:["0703","0704","0706","07025","07026","0803","0806","0810","0813","0814","0816","0903","0906","0913","0916"],description:"MTN Nigeria"},GLO:{provider:"GLO",prefixes:["0705","0805","0807","0811","0815","0905","0915"],description:"Globacom Nigeria"},AIRTEL:{provider:"AIRTEL",prefixes:["0701","0708","0802","0808","0812","0901","0902","0907","0912"],description:"Airtel Nigeria"},"9MOBILE":{provider:"9MOBILE",prefixes:["0809","0817","0818","0908","0909"],description:"9mobile Nigeria"}},a={CLEANUP:/[^\d+]/g,E164_VALIDATION:/^\+234\d{10}$/,LOCAL_VALIDATION:/^0\d{10}$/,INTERNATIONAL_VALIDATION:/^\+234\d{10}$/},i=new Map;(()=>{for(let[e,t]of Object.entries(d))for(let r of t.prefixes)i.set(r,e);})();var h=class{constructor(t){this.cache=new Map;this.maxSize=t;}get(t){if(this.cache.has(t)){let r=this.cache.get(t);return this.cache.delete(t),this.cache.set(t,r),r}}set(t,r){if(this.cache.has(t))this.cache.delete(t);else if(this.cache.size>=this.maxSize){let o=this.cache.keys().next().value;o!==void 0&&this.cache.delete(o);}this.cache.set(t,r);}clear(){this.cache.clear();}get size(){return this.cache.size}has(t){return this.cache.has(t)}delete(t){return this.cache.delete(t)}},f=new h(1e3);var p=class extends Error{constructor(t,r,o){super(t),this.name="PhoneValidationError",this.code=r,this.input=o;}},z=e=>a.E164_VALIDATION.test(e.replace(a.CLEANUP,"").replace(/^(?:234|0)/,"+234")),V=e=>a.E164_VALIDATION.test(e),O=()=>Object.keys(d),P=e=>d[e],R=e=>i.get(e)??null;function l(e,t){if(typeof e!="string"||e.length===0)throw new TypeError("Phone number must be a non-empty string");let r=e.replace(a.CLEANUP,"");if(r.length<10||r.length>14)throw new Error(`Invalid phone number length: ${e}`);let o,n=r[0];r.slice(0,4);switch(n){case "+":if(r.startsWith("+234"))o=r;else throw new Error(`Invalid country code: ${e}`);break;case "2":if(r.startsWith("234"))o="+"+r;else throw new Error(`Invalid country code: ${e}`);break;case "0":if(r.length===11)o="+234"+r.slice(1);else throw r.length===10?new Error(`Invalid local number: ${e}`):new Error(`Invalid phone number length: ${e}`);break;default:if(r.length===10)o="+234"+r;else throw new Error(`Invalid phone number format: ${e}`)}if(!a.E164_VALIDATION.test(o))throw new Error(`Invalid Nigerian phone number: ${e}`);switch(t??"e164"){case "local":return "0"+o.slice(4);case "international":return o;case "e164":default:return o}}function u(e,t){try{return {success:!0,data:l(e,t),error:null}}catch(r){return {success:false,data:null,error:r instanceof Error?r.message:"Unknown error"}}}function g(e){if(typeof e!="string"||e.length===0)return false;try{return l(e),!0}catch{return false}}function m(e){try{let r="0"+l(e).slice(4),o=r.slice(0,5);if(i.has(o))return i.get(o);let n=r.slice(0,4);return i.get(n)??null}catch{return null}}function _(e){let t=f.get(e);if(t)return t;let r=e,o;try{let n=l(e),c=m(e),s=T(e),x=e[0]==="+"?"international":e[0]==="0"?"local":"e164";o={original:r,normalized:n,telco:c,isValid:!0,format:x,prefix:s.prefix,number:s.number};}catch{o={original:r,normalized:"",telco:null,isValid:false,format:"e164",prefix:"",number:""};}return f.set(e,o),o}function T(e){let t=l(e),r="0"+t.slice(4),o=r.slice(0,5),n=i.get(o);if(n)return {prefix:o,telco:n,number:t.slice(4)};let c=r.slice(0,4),s=i.get(c);return {prefix:s?c:"",telco:s??null,number:t.slice(4)}}function M(e){let t=e?[e]:["MTN","GLO","AIRTEL","9MOBILE"],r=t[Math.floor(Math.random()*t.length)],o=P(r).prefixes,n=o[Math.floor(Math.random()*o.length)],c=11-n.length,s=Array.from({length:c},()=>Math.floor(Math.random()*10).toString()).join("");return l(n+s)}function w(e,t){return e.map(r=>u(r,t))}function F(e){return e.map(m)}function C(e){return e.map(g)}var D=e=>l(e,"international"),S=e=>l(e,"local");function $(e,t){let r=u(e,t);return r.success?(t==="local"?a.LOCAL_VALIDATION:t==="international"?a.INTERNATIONAL_VALIDATION:a.E164_VALIDATION).test(r.data):false}/**
* Type definitions for Nigerian telecommunications utilities
*
* @fileoverview Core types and interfaces for phone number handling
* @author Ademuyiwa Johnson
* @license MIT
*/
/**
* Constants and data for Nigerian telecommunications utilities
*
* @fileoverview Telco data, patterns, and lookup structures
* @author Ademuyiwa Johnson
* @license MIT
*/
/**
* Cache management for Nigerian telecommunications utilities
*
* @fileoverview LRU cache implementation and cache management
* @author Ademuyiwa Johnson
* @license MIT
*/
/**
* Nigerian Phone Number Library - Ultra Optimized Edition
* Performance-first implementation with advanced TypeScript features
*
* @fileoverview Main entry point for Nigerian phone number utilities
* @author Ademuyiwa Johnson
* @license MIT
*/exports.PhoneValidationError=p;exports.batchGetTelcos=F;exports.batchNormalizePhones=w;exports.batchValidatePhones=C;exports.formatPhoneToInternational=D;exports.formatPhoneToLocal=S;exports.generateRandomPhone=M;exports.getAllTelcos=O;exports.getPhoneInfo=_;exports.getTelco=m;exports.getTelcoByPrefix=R;exports.getTelcoInfo=P;exports.isNormalizedPhone=V;exports.isValidNigerianNumber=g;exports.isValidatedPhone=z;exports.normalizePhone=l;exports.safeNormalizePhone=u;exports.splitPhoneParts=T;exports.validatePhoneFormat=$;