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.41 kB
JavaScript
/*! n2words/en-IE v5.1.2 | MIT License | github.com/forzagreen/n2words */
function e(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 n(n){const r=n.toString();return t(r)?e(r):r}function t(e){return e.includes("e")||e.includes("E")}function r(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 i(n){let r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${n}"`);t(r)&&(r=e(r));const i="-"===r[0];i&&(r=r.slice(1));const o=r.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(r),cents:0n};const s=r.slice(0,o)||"0",u=(r.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function o(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 s(e){return 10n**BigInt(3*(e+1))}const u=["","one","two","three","four","five","six","seven","eight","nine"],l=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"],c=["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"],f=["thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","quattuordecillion","quindecillion","sexdecillion","septendecillion","octodecillion","novemdecillion","vigintillion"],a=s(f.length),g=s(f.length),d=s(f.length),h="hundred",w="zero",b="minus",p=["","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth"],m=["tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth"],v=["","","twentieth","thirtieth","fortieth","fiftieth","sixtieth","seventieth","eightieth","ninetieth"],y={word:"",hasHundred:!1};function N(e){if(0===e)return y.word="",y.hasHundred=!1,y;const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100);let i="";return 1===t?i=l[n]:t>=2?i=n>0?c[t]+"-"+u[n]:c[t]:n>0&&(i=u[n]),r>0?(y.word=i?u[r]+" "+h+" and "+i:u[r]+" "+h,y.hasHundred=!0):(y.word=i,y.hasHundred=!1),y}function I(e){if(0n===e)return w;if(e<1000n)return N(Number(e)).word;if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n),{word:r}=N(n);let i=r+" "+f[0];if(t>0){const{word:e,hasHundred:n}=N(t);i+=n?" "+e:" and "+e}return i}return function(e){const n=[];let t=e;for(;t>0n;)n.push(Number(t%1000n)),t/=1000n;let r=-1;for(let e=0;e<n.length;e++)if(0!==n[e]){r=e;break}let i="",o=!1;for(let e=n.length-1;e>=0;e--){const t=n[e];if(0===t)continue;const{word:s,hasHundred:u}=N(t);i&&e===r&&o&&!u&&(i+=" and"),i&&(i+=" "),i+=s,e>0?(i+=" "+f[e-1],o=!0):o=!1}return i}(e)}function E(i){const{isNegative:s,integerPart:u,decimalPart:l}=function(i){const o=typeof i;if("bigint"==typeof i)return i<0n?{isNegative:!0,integerPart:-i}:{isNegative:!1,integerPart:i};if("number"==typeof i){if(!Number.isFinite(i))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(i)?i<0?{isNegative:!0,integerPart:BigInt(-i)}:{isNegative:!1,integerPart:BigInt(i)}:r(n(i))}if("string"==typeof i)return r(function(n){const r=n.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${n}"`);return t(r)?e(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(i);o(u,a,l);let c="";return s&&(c=b+" "),c+=I(u),l&&(c+=" point "+function(e){let n="",t=0;for(;t<e.length&&"0"===e[t];)n&&(n+=" "),n+=w,t++;const r=e.slice(t);return r&&(n&&(n+=" "),n+=I(BigInt(r))),n}(l)),c}function O(e){const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100);let i="";return 1===t?i=m[n]:t>=2?i=n>0?c[t]+"-"+p[n]:v[t]:n>0&&(i=p[n]),r>0?i?u[r]+" "+h+" "+i:u[r]+" hundredth":i}function x(n){const r=function(n){const r=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 r=n.trim();if(0===r.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===r[0])throw new RangeError("Ordinals cannot be negative");if(r.includes("."))throw new RangeError("Ordinals must be whole numbers");if(t(r)){const n=e(r);if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");const t=BigInt(n);if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}try{const e=BigInt(r);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}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 ${r}`)}(n);return o(r,g),function(e){if(e<1000n)return O(Number(e));if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);if(0===t)return N(n).word+" "+f[0]+"th";const{word:r}=N(n);return r+" "+f[0]+" "+O(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?O(t):N(t).word+" "+f[e-1]+"th":(i+=N(t).word,e>0&&(i+=" "+f[e-1])))}return i}(e)}(r)}const R={and:!0};function B(e,t){const{isNegative:r,dollars:s,cents:u}=function(e){const t=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}:i(n(e))}if("string"==typeof e)return i(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);o(s,d);const{and:l}=function(e,n){const t={...n};if(void 0===e)return t;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 r=Object.keys(n);for(const[i,o]of Object.entries(e)){if(!Object.hasOwn(n,i))throw new TypeError(`Unknown option "${i}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof t[i])throw new TypeError(`Option "${i}" must be a ${typeof t[i]}, got ${typeof o}`);t[i]=o}}return t}(t,R);let c="";return r&&(c=b+" "),(s>0n||0n===u)&&(c+=I(s),c+=" euro"),u>0n&&(s>0n&&(c+=l?" and ":" "),c+=I(u),c+=" "+(1n===u?"cent":"cents")),c}export{a as cardinalMax,R as currencyDefaults,d as currencyMax,g as ordinalMax,E as toCardinal,B as toCurrency,x as toOrdinal};