n2words
Version:
Convert numbers to words in 70+ languages with zero dependencies. Supports BigInt, decimals, and browser/Node.js environments.
3 lines (2 loc) • 8.18 kB
JavaScript
/*! n2words/es-US v5.1.2 | MIT License | github.com/forzagreen/n2words */
function e(e){let[n,t]=e.toLowerCase().split("e");const i=parseInt(t,10);let r="";"-"===n[0]&&(r="-",n=n.slice(1));const o=n.indexOf("."),s=-1===o?n:n.slice(0,o)+n.slice(o+1),c=(-1===o?n.length:o)+i;return c>=s.length?r+s+"0".repeat(c-s.length):c<=0?r+"0."+"0".repeat(-c)+s:r+s.slice(0,c)+"."+s.slice(c)}function n(n){const i=n.toString();return t(i)?e(i):i}function t(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 i=e.slice(0,t)||"0",r=e.slice(t+1);return{isNegative:n,integerPart:BigInt(i),decimalPart:r}}function r(n){let i=n.trim();if(0===i.length||Number.isNaN(Number(i)))throw new RangeError(`Invalid currency format: "${n}"`);t(i)&&(i=e(i));const r="-"===i[0];r&&(i=i.slice(1));const o=i.indexOf(".");if(-1===o)return{isNegative:r,dollars:BigInt(i),cents:0n};const s=i.slice(0,o)||"0",c=(i.slice(o+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(s),cents:BigInt(c)}}function o(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=n))return;const i=n.toString().length-1,r=n===10n**BigInt(i)?`10^${i} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${r}`)}function s(e){return 10n**BigInt(3*(e+1))}function c(e,n,t){const i={...n};if(void 0===e)return i;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[o,s]of Object.entries(e)){if(!Object.hasOwn(n,o))throw new TypeError(`Unknown option "${o}" — expected one of: ${r.join(", ")}`);if(void 0!==s){if(typeof s!=typeof i[o])throw new TypeError(`Option "${o}" must be a ${typeof i[o]}, got ${typeof s}`);if(void 0!==t&&Object.hasOwn(t,o)){const e=t[o];if(!e.includes(s))throw new RangeError(`Option "${o}" must be one of: ${e.join(", ")} — got ${JSON.stringify(s)}`)}i[o]=s}}return i}const u=["","uno","dos","tres","cuatro","cinco","seis","siete","ocho","nueve"],a=["","una","dos","tres","cuatro","cinco","seis","siete","ocho","nueve"],l=["diez","once","doce","trece","catorce","quince","dieciseis","diecisiete","dieciocho","diecinueve"],g=["veinte","veintiuno","veintidós","veintitrés","veinticuatro","veinticinco","veintiséis","veintisiete","veintiocho","veintinueve"],f=["veinte","veintiuna","veintidós","veintitrés","veinticuatro","veinticinco","veintiséis","veintisiete","veintiocho","veintinueve"],m=["","","","treinta","cuarenta","cincuenta","sesenta","setenta","ochenta","noventa"],d=["","ciento","doscientos","trescientos","cuatrocientos","quinientos","seiscientos","setecientos","ochocientos","novecientos"],v=["","cienta","doscientas","trescientas","cuatrocientas","quinientas","seiscientas","setecientas","ochocientas","novecientas"],p=["mil","millón","billón","trillón","cuatrillón","quintillón"],b=["mil","millones","billones","trillones","cuatrillones","quintillones"],h=s(p.length),w=10n**BigInt(9),N=s(p.length),y="cero",I="menos",E=["","primero","segundo","tercero","cuarto","quinto","sexto","séptimo","octavo","noveno"],O=["","primera","segunda","tercera","cuarta","quinta","sexta","séptima","octava","novena"],R=["","décimo","vigésimo","trigésimo","cuadragésimo","quincuagésimo","sexagésimo","septuagésimo","octogésimo","nonagésimo"],B=["","décima","vigésima","trigésima","cuadragésima","quincuagésima","sexagésima","septuagésima","octogésima","nonagésima"];function $(e,n){if(0===e)return"";if(100===e)return"cien";const t=e%10,i=Math.trunc(e/10)%10,r=Math.trunc(e/100),o=e%100,s=[];return r>0&&s.push((n?v:d)[r]),0===o||s.push(o<10?(n?a:u)[o]:o<20?l[t]:o<30?(n?f:g)[t]:0===t?m[i]:m[i]+" y "+(n?a:u)[t]),s.join(" ")}function x(e,n){if(0n===e)return y;if(e<1000n)return $(Number(e),n);const t=[];let i=e;for(;i>0n;)t.push(i%1000n),i/=1000n;let r="";for(let e=t.length-1;e>=0;e--){const i=t[e];if(0n!==i)if(r&&(r+=" "),0===e)r+=$(Number(i),n);else if(1===e)r+=1n===i?p[0]:$(Number(i),!1)+" "+p[0];else{const n=e-1;r+=1n===i?"un "+p[n]:$(Number(i),!1)+" "+b[n]}}return r}const j={gender:"masculine"},q={gender:["masculine","feminine"]};function P(r,s){const{isNegative:u,integerPart:a,decimalPart:l}=function(r){const o=typeof r;if("bigint"==typeof r)return r<0n?{isNegative:!0,integerPart:-r}:{isNegative:!1,integerPart:r};if("number"==typeof r){if(!Number.isFinite(r))throw new RangeError("Number must be finite (NaN and Infinity are not supported)");return Number.isSafeInteger(r)?r<0?{isNegative:!0,integerPart:BigInt(-r)}:{isNegative:!1,integerPart:BigInt(r)}:i(n(r))}if("string"==typeof r)return i(function(n){const i=n.trim();if(0===i.length||Number.isNaN(Number(i)))throw new RangeError(`Invalid number format: "${n}"`);return t(i)?e(i):i}(r));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(r);o(a,h,l);const{gender:g}=c(s,j,q),f="feminine"===g;let m="";return u&&(m=I+" "),m+=x(a,f),l&&(m+=" punto "+function(e,n){let t="",i=0;for(;i<e.length&&"0"===e[i];)t&&(t+=" "),t+=y,i++;const r=e.slice(i);return r&&(t&&(t+=" "),t+=x(BigInt(r),n)),t}(l,f)),m}function T(e,n){if(0===e)return"";const t=e%10,i=Math.trunc(e/10)%10,r=Math.trunc(e/100),o=n?O:E,s=n?B:R,c=n?"centésima":"centésimo",u=[];return r>0&&u.push(1===r?c:["","","du","tri","cuadri","quin","sex","septi","octi","noni"][r]+c),i>0&&u.push(s[i]),t>0&&u.push(o[t]),u.join(" ")}function S(e,n){const t=n?"milésima":"milésimo",i=n?"millonésima":"millonésimo";if(e<1000n)return T(Number(e),n);if(e<1000000n){const i=Number(e/1000n),r=Number(e%1000n);let o;return o=1===i?t:T(i,n)+" "+t,r>0&&(o+=" "+T(r,n)),o}const r=Number(e/1000000n),o=e%1000000n;let s;return s=1===r?i:T(r,n)+" "+i,o>0n&&(s+=" "+S(o,n)),s}const M={gender:"masculine"},F={gender:["masculine","feminine"]};function k(n,i){const r=function(n){const i=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 i=n.trim();if(0===i.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===i[0])throw new RangeError("Ordinals cannot be negative");if(i.includes("."))throw new RangeError("Ordinals must be whole numbers");if(t(i)){const n=e(i);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(i);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 ${i}`)}(n);o(r,w);const{gender:s}=c(i,M,F);return S(r,"feminine"===s)}const C={and:!0};function z(e,t){const{isNegative:i,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}:r(n(e))}if("string"==typeof e)return r(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);o(s,N);const{and:a}=c(t,C);let l="";return i&&(l=I+" "),(s>0n||0n===u)&&(l+=1n===s?"un dólar":x(s,!1)+" dólares"),u>0n&&(s>0n&&(l+=a?" con ":" "),l+=1n===u?"un centavo":x(u,!1)+" centavos"),l}export{j as cardinalDefaults,h as cardinalMax,q as cardinalValues,C as currencyDefaults,N as currencyMax,M as ordinalDefaults,w as ordinalMax,F as ordinalValues,P as toCardinal,z as toCurrency,k as toOrdinal};