n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
3 lines (2 loc) • 5.73 kB
JavaScript
/*! n2words/ha-NG v5.1.2 | MIT License | github.com/forzagreen/n2words */
var n,e;n=this,e=function(n){"use strict";function e(n){let[e,t]=n.toLowerCase().split("e");const r=parseInt(t,10);let i="";"-"===e[0]&&(i="-",e=e.slice(1));const s=e.indexOf("."),o=-1===s?e:e.slice(0,s)+e.slice(s+1),a=(-1===s?e.length:s)+r;return a>=o.length?i+o+"0".repeat(a-o.length):a<=0?i+"0."+"0".repeat(-a)+o:i+o.slice(0,a)+"."+o.slice(a)}function t(n){const t=n.toString();return r(t)?e(t):t}function r(n){return n.includes("e")||n.includes("E")}function i(n){const e="-"===n[0];e&&(n=n.slice(1));const t=n.indexOf(".");if(-1===t)return{isNegative:e,integerPart:BigInt(n)};const r=n.slice(0,t)||"0",i=n.slice(t+1);return{isNegative:e,integerPart:BigInt(r),decimalPart:i}}function s(n){let t=n.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${n}"`);r(t)&&(t=e(t));const i="-"===t[0];i&&(t=t.slice(1));const s=t.indexOf(".");if(-1===s)return{isNegative:i,dollars:BigInt(t),cents:0n};const o=t.slice(0,s)||"0",a=(t.slice(s+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(o),cents:BigInt(a)}}function o(n,e,t){if(null===e)return;if(n<e)return;const r=e.toString().length-1,i=e===10n**BigInt(r)?`10^${r} - 1`:""+(e-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function a(n){return 10n**BigInt(3*(n+1))}const u=["","ɗaya","biyu","uku","huɗu","biyar","shida","bakwai","takwas","tara"],c=["goma","sha ɗaya","sha biyu","sha uku","sha huɗu","sha biyar","sha shida","sha bakwai","sha takwas","sha tara"],l=["","","ashirin","talatin","arba'in","hamsin","sittin","saba'in","tamanin","tis'in"],g="ɗari",f="dubu",h="sifiri",d="babu",b=["",f,"miliyan","biliyan"],p=a(b.length-1),m=a(b.length-1),w=a(b.length-1);function N(n){if(0===n)return"";const e=n%10,t=Math.trunc(n/10)%10,r=Math.trunc(n/100),i=[];r>0&&i.push(1===r?g:u[r]+" "+g);const s=n%100;return 0===s||i.push(s<10?r>0?"da "+u[e]:u[e]:s<20?c[e]:0===e?l[t]:l[t]+" da "+u[e]),i.join(" ")}function y(n){return 0n===n?h:n<1000n?N(Number(n)):function(n){const e=n.toString(),t=e.length,r=[],i=t%3;let s=0;for(i>0&&(r.push(Number(e.slice(0,i))),s=i);s<t;)r.push(Number(e.slice(s,s+3))),s+=3;const o=[];let a=r.length-1;for(let n=0;n<r.length;n++){const e=r[n];if(0!==e){const n=b[a]||"";0===a?o.push(N(e)):(o.push(N(e)),o.push(n))}a--}const u=[];for(let n=0;n<o.length;n++){const e=o[n],t=o[n+1];("ɗaya"!==e||!t||t!==g&&t!==f)&&u.push(e)}const c=[];for(let n=0;n<u.length;n++){const e=u[n],t=n>0?u[n-1]:null;t&&v(e)&&(t===f||t===g||b.includes(t))?c.push(" da "):n>0&&c.push(" "),c.push(e)}return c.join("")}(n)}function v(n){return u.slice(1).includes(n)}const I={haNG:function(n){const t=function(n){const t=typeof n;if("bigint"==typeof n){if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(n))throw new RangeError("Ordinals must be whole numbers");if(n<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(n)}if("string"==typeof n)return function(n){const t=n.trim();if(0===t.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===t[0])throw new RangeError("Ordinals cannot be negative");if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");if(r(t)){const n=e(t);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(n);if(r<=0n)throw new RangeError("Ordinals must be positive integers");return r}try{const n=BigInt(t);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}catch(e){if(e instanceof RangeError)throw e;throw new RangeError(`Invalid ordinal format: "${n}"`,{cause:e})}}(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(n);return o(t,m),1n===(i=t)?"na farko":"na "+y(i);var i}};n.currency={haNG:function(n){const{isNegative:e,dollars:r,cents:i}=function(n){const e=typeof n;if("bigint"==typeof n)return n<0n?{isNegative:!0,dollars:-n,cents:0n}:{isNegative:!1,dollars:n,cents:0n};if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(n)?n<0?{isNegative:!0,dollars:BigInt(-n),cents:0n}:{isNegative:!1,dollars:BigInt(n),cents:0n}:s(t(n))}if("string"==typeof n)return s(n);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${e}`)}(n);o(r,w);let a="";return e&&(a=d+" "),(r>0n||0n===i)&&(a+=y(r)+" naira"),i>0n&&(r>0n&&(a+=" da "),a+=y(i)+" kobo"),a}},n.haNG=function(n){const{isNegative:s,integerPart:a,decimalPart:c}=function(n){const s=typeof n;if("bigint"==typeof n)return n<0n?{isNegative:!0,integerPart:-n}:{isNegative:!1,integerPart:n};if("number"==typeof n){if(!Number.isFinite(n))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(n)?n<0?{isNegative:!0,integerPart:BigInt(-n)}:{isNegative:!1,integerPart:BigInt(n)}:i(t(n))}if("string"==typeof n)return i(function(n){const t=n.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${n}"`);return r(t)?e(t):t}(n));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${s}`)}(n);o(a,p);let l="";return s&&(l=d+" "),l+=y(a),c&&(l+=" digo "+function(n){const e=[];for(const t of n){const n=parseInt(t,10);e.push(0===n?h:u[n])}return e.join(" ")}(c)),l},n.ordinal=I},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).n2words=n.n2words||{});