UNPKG

nig-utils

Version:

A fully-typed, production-grade utility library for Nigerian developers

7 lines 3.27 kB
'use strict';function m(r,n={}){let{symbol:o="\u20A6",compact:a=false,decimals:t=2,showKobo:e=false,locale:c="en-NG"}=n;if(r===0)return `${o}0.00`;let f=r<0,u=Math.abs(r);if(a)return h(u,o,f);let l=new Intl.NumberFormat(c,{style:"currency",currency:"NGN",minimumFractionDigits:t,maximumFractionDigits:t}).format(u),i=l;return o==="NGN"?i=l.replace("\u20A6","NGN "):o==="N"&&(i=l.replace("\u20A6","N ")),f?`-${i}`:i}function h(r,n,o){let a=Math.abs(r);if(a>=1e9){let t=a/1e9,e=t===Math.floor(t)?t.toString():t.toFixed(1);return `${o?"-":""}${n}${e}B`}else if(a>=1e6){let t=a/1e6,e=t===Math.floor(t)?t.toString():t.toFixed(1);return `${o?"-":""}${n}${e}M`}else if(a>=1e3){let t=a/1e3,e=t===Math.floor(t)?t.toString():t.toFixed(1);return `${o?"-":""}${n}${e}K`}else return m(r,{symbol:n})}function N(r,n={}){let{allowNegative:o=true,defaultCurrency:a="\u20A6",strict:t=false}=n;if(!r||typeof r!="string")throw new Error("Invalid input: text must be a non-empty string");let e=r.trim(),c=e.startsWith("-");c&&(e=e.substring(1));let f=/[₦NGN]/.test(e);e=e.replace(/[₦NGN]/g,"").trim();let u=e.match(/^([\d,]+\.?\d*)([KMB])$/i);if(u){let[,i,b]=u,d=b.toUpperCase()==="K"?1e3:b.toUpperCase()==="M"?1e6:b.toUpperCase()==="B"?1e9:1,p=parseFloat(i.replace(/,/g,""));if(isNaN(p))throw new Error("Invalid number format");return (c?-1:1)*p*d}if(e.match(/^[\d,]+\.?\d*$/)&&f){let i=parseFloat(e.replace(/,/g,""));if(isNaN(i))throw new Error("Invalid number format");return (c?-1:1)*i}if(t)throw new Error("Invalid Naira format");return 0}function g(r){return Math.round(r*100)}function M(r){return r/100}function x(r){if(r===0)return "Zero Naira only";let n=r<0,o=Math.abs(r),a=Math.floor(o),t=Math.round((o-a)*100),e="";return n&&(e+="Negative "),a>0&&(e+=s(a)+" Naira"),t>0&&(a>0&&(e+=" and "),e+=s(t)+" Kobo"),a===0&&t===0&&(e="Zero Naira"),e+" only"}function s(r){let n=["","one","two","three","four","five","six","seven","eight","nine"],o=["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"],a=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"];return r===0?"":r<10?n[r]:r<20?a[r-10]:r<100?o[Math.floor(r/10)]+(r%10>0?"-"+n[r%10]:""):r<1e3?n[Math.floor(r/100)]+" hundred"+(r%100>0?" "+s(r%100):""):r<1e6?s(Math.floor(r/1e3))+" thousand"+(r%1e3>0?", "+s(r%1e3):""):r<1e9?s(Math.floor(r/1e6))+" million"+(r%1e6>0?", "+s(r%1e6):""):s(Math.floor(r/1e9))+" billion"+(r%1e9>0?", "+s(r%1e9):"")}function v(r){try{return N(r,{strict:!0}),!0}catch{return false}}function y(r,n){return r*n/100}function w(r,n=7.5){return r+y(r,n)}function $(r,n=7.5){let o=r/(1+n/100);return Math.round(o*100)/100}function F(r,n,o={}){let a=m(r,o),t=m(n,o);return `${a} - ${t}`}function C(r){return Math.round(r)}function K(r){return Math.round(r*100)/100}/** * Nigerian Money Utilities - Naira Formatting & Currency Helpers * * @fileoverview Comprehensive utilities for Nigerian currency operations * @author Ademuyiwa Johnson * @license MIT */exports.addVAT=w;exports.calculatePercentage=y;exports.formatNaira=m;exports.formatRange=F;exports.isValidNairaAmount=v;exports.koboToNaira=M;exports.nairaToKobo=g;exports.parseNaira=N;exports.removeVAT=$;exports.roundToKobo=K;exports.roundToNaira=C;exports.spellOutNaira=x;