UNPKG

n2words

Version:

Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.

3 lines (2 loc) 7.92 kB
/*! n2words/en-US v5.1.2 | MIT License | github.com/forzagreen/n2words */ var e,n;e=this,n=function(e){"use strict";function n(e){let[n,t]=e.toLowerCase().split("e");const r=parseInt(t,10);let i="";"-"===n[0]&&(i="-",n=n.slice(1));const o=n.indexOf("."),s=-1===o?n:n.slice(0,o)+n.slice(o+1),u=(-1===o?n.length:o)+r;return u>=s.length?i+s+"0".repeat(u-s.length):u<=0?i+"0."+"0".repeat(-u)+s:i+s.slice(0,u)+"."+s.slice(u)}function t(e){const t=e.toString();return r(t)?n(t):t}function r(e){return e.includes("e")||e.includes("E")}function i(e){const n="-"===e[0];n&&(e=e.slice(1));const t=e.indexOf(".");if(-1===t)return{isNegative:n,integerPart:BigInt(e)};const r=e.slice(0,t)||"0",i=e.slice(t+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function o(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);r(t)&&(t=n(t));const i="-"===t[0];i&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",u=(t.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function s(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=n))return;const r=n.toString().length-1,i=n===10n**BigInt(r)?`10^${r} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}function u(e){return 10n**BigInt(3*(e+1))}function l(e,n,t){const r={...n};if(void 0===e)return r;if(!function(e){if(null===e||"object"!=typeof e)return!1;const n=Object.getPrototypeOf(e);return null===n||n===Object.prototype}(e))throw new TypeError("Invalid options: expected plain object or undefined, got "+typeof e);const i=Object.keys(n);for(const[t,o]of Object.entries(e)){if(!Object.hasOwn(n,t))throw new TypeError(`Unknown option "${t}" — expected one of: ${i.join(", ")}`);if(void 0!==o){if(typeof o!=typeof r[t])throw new TypeError(`Option "${t}" must be a ${typeof r[t]}, got ${typeof o}`);r[t]=o}}return r}const f=["","one","two","three","four","five","six","seven","eight","nine"],c=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"],a=["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"],d=["thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","quattuordecillion","quindecillion","sexdecillion","septendecillion","octodecillion","novemdecillion","vigintillion"],g=u(d.length),h=u(d.length),w=u(d.length),b="hundred",p="zero",m="minus",v=["","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth"],y=["tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth"],N=["","","twentieth","thirtieth","fortieth","fiftieth","sixtieth","seventieth","eightieth","ninetieth"],I={word:"",hasHundred:!1};function E(e,n){if(0===e)return I.word="",I.hasHundred=!1,I;const t=e%10,r=Math.trunc(e/10)%10,i=Math.trunc(e/100);let o="";return 1===r?o=c[t]:r>=2?o=t>0?a[r]+"-"+f[t]:a[r]:t>0&&(o=f[t]),i>0?(I.word=o?f[i]+" "+b+(n?" and ":" ")+o:f[i]+" "+b,I.hasHundred=!0):(I.word=o,I.hasHundred=!1),I}function O(e,n,t){if(0n===e)return p;if(e<1000n)return E(Number(e),t).word;if(n&&e>=1100n&&e<=9999n){const n=Number(e),r=Math.trunc(n/100),i=n%100,{word:o}=E(r,t);let s=o+" "+b;if(i>0){const{word:e}=E(i,t);t&&(s+=" and"),s+=" "+e}return s}if(e<1000000n){const n=Number(e/1000n),r=Number(e%1000n),{word:i}=E(n,t);let o=i+" "+d[0];if(r>0){const{word:e,hasHundred:n}=E(r,t);t&&!n&&(o+=" and"),o+=" "+e}return o}return function(e,n){const t=[];let r=e;for(;r>0n;)t.push(Number(r%1000n)),r/=1000n;let i=-1;if(n)for(let e=0;e<t.length;e++)if(0!==t[e]){i=e;break}let o="",s=!1;for(let e=t.length-1;e>=0;e--){const r=t[e];if(0===r)continue;const{word:u,hasHundred:l}=E(r,n);n&&o&&e===i&&s&&!l&&(o+=" and"),o&&(o+=" "),o+=u,e>0?(o+=" "+d[e-1],s=!0):s=!1}return o}(e,t)}const x={hundredPairing:!1,and:!1};function R(e){const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100);let i="";return 1===t?i=y[n]:t>=2?i=n>0?a[t]+"-"+v[n]:N[t]:n>0&&(i=v[n]),r>0?i?f[r]+" "+b+" "+i:f[r]+" hundredth":i}const B={and:!0},$={enUS:function(e){const t=function(e){const t=typeof e;if("bigint"==typeof e){if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(e))throw new RangeError("Ordinals must be whole numbers");if(e<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(e)}if("string"==typeof e)return function(e){const t=e.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 e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(e);if(r<=0n)throw new RangeError("Ordinals must be positive integers");return r}try{const e=BigInt(t);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}catch(n){if(n instanceof RangeError)throw n;throw new RangeError(`Invalid ordinal format: "${e}"`,{cause:n})}}(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);return s(t,h),function(e){if(e<1000n)return R(Number(e));if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);if(0===t)return E(n,!1).word+" "+d[0]+"th";const{word:r}=E(n,!1);return r+" "+d[0]+" "+R(t)}return function(e){const n=[];let t=e;for(;t>0n;)n.push(Number(t%1000n)),t/=1000n;let r=0;for(let e=0;e<n.length;e++)if(0!==n[e]){r=e;break}let i="";for(let e=n.length-1;e>=0;e--){const t=n[e];0!==t&&(i&&(i+=" "),e===r?i+=0===e?R(t):E(t,!1).word+" "+d[e-1]+"th":(i+=E(t,!1).word,e>0&&(i+=" "+d[e-1])))}return i}(e)}(t)}};e.currency={enUS:function(e,n){const{isNegative:r,dollars:i,cents:u}=function(e){const n=typeof e;if("bigint"==typeof e)return e<0n?{isNegative:!0,dollars:-e,cents:0n}:{isNegative:!1,dollars:e,cents:0n};if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Currency must be a finite number");return Number.isSafeInteger(e)?e<0?{isNegative:!0,dollars:BigInt(-e),cents:0n}:{isNegative:!1,dollars:BigInt(e),cents:0n}:o(t(e))}if("string"==typeof e)return o(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);s(i,w);const{and:f}=l(n,B);let c="";return r&&(c=m+" "),(i>0n||0n===u)&&(c+=O(i,!1,!1),c+=" "+(1n===i?"dollar":"dollars")),u>0n&&(i>0n&&(c+=f?" and ":" "),c+=O(u,!1,!1),c+=" "+(1n===u?"cent":"cents")),c}},e.enUS=function(e,o){const{isNegative:u,integerPart:f,decimalPart:c}=function(e){const o=typeof e;if("bigint"==typeof e)return e<0n?{isNegative:!0,integerPart:-e}:{isNegative:!1,integerPart:e};if("number"==typeof e){if(!Number.isFinite(e))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(e)?e<0?{isNegative:!0,integerPart:BigInt(-e)}:{isNegative:!1,integerPart:BigInt(e)}:i(t(e))}if("string"==typeof e)return i(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return r(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(f,g,c);const{hundredPairing:a,and:d}=l(o,x);let h="";return u&&(h=m+" "),h+=O(f,a,d),c&&(h+=" point "+function(e,n){let t="",r=0;for(;r<e.length&&"0"===e[r];)t&&(t+=" "),t+=p,r++;const i=e.slice(r);return i&&(t&&(t+=" "),t+=O(BigInt(i),!1,n)),t}(c,d)),h},e.ordinal=$},"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).n2words=e.n2words||{});