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.48 kB
JavaScript
/*! n2words/es-ES 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(6*(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"],g=["diez","once","doce","trece","catorce","quince","dieciseis","diecisiete","dieciocho","diecinueve"],l=["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=["millón","billón","trillón","cuatrillón"],b=["millones","billones","trillones","cuatrillones"],h=s(p.length),w=10n**BigInt(9),N=s(p.length),y="mil",I="cero",E="menos",O=["","primero","segundo","tercero","cuarto","quinto","sexto","séptimo","octavo","noveno"],R=["","primera","segunda","tercera","cuarta","quinta","sexta","séptima","octava","novena"],B=["","décimo","vigésimo","trigésimo","cuadragésimo","quincuagésimo","sexagésimo","septuagésimo","octogésimo","nonagésimo"],$=["","décima","vigésima","trigésima","cuadragésima","quincuagésima","sexagésima","septuagésima","octogésima","nonagésima"];function x(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?g[t]:o<30?(n?f:l)[t]:0===t?m[i]:m[i]+" y "+(n?a:u)[t]),s.join(" ")}function j(e,n){if(1===e)return y;if(e%2==0){const t=e/2-1,i=p[t];return i?n>1n?b[t]:i:""}{const n=b[(e-1)/2-1];return n?y+" "+n:y}}function P(e,n){if(0n===e)return I;if(e<1000n)return x(Number(e),n);if(e<1000000n){const t=Number(e/1000n),i=Number(e%1000n);let r;if(1===t)r=y;else{const e=x(t,!1);r="uno"===e||"una"===e?y:e+" "+y}return i>0&&(r+=" "+x(i,n)),r}return function(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)continue;const o=e>0?j(e,i):"";r&&(r+=" "),r+=0===e?x(Number(i),n):1===e?1n===i?y:x(Number(i),!1)+" "+o:e%2==1?1n===i?o:x(Number(i),!1)+" "+o:1n===i?"un "+o:x(Number(i),!1)+" "+o}return r}(e,n)}const q={gender:"masculine"},T={gender:["masculine","feminine"]};function S(r,s){const{isNegative:u,integerPart:a,decimalPart:g}=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,g);const{gender:l}=c(s,q,T),f="feminine"===l;let m="";return u&&(m=E+" "),m+=P(a,f),g&&(m+=" punto "+function(e,n){let t="",i=0;for(;i<e.length&&"0"===e[i];)t&&(t+=" "),t+=I,i++;const r=e.slice(i);return r&&(t&&(t+=" "),t+=P(BigInt(r),n)),t}(g,f)),m}function M(e,n){if(0===e)return"";const t=e%10,i=Math.trunc(e/10)%10,r=Math.trunc(e/100),o=n?R:O,s=n?$:B,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 F(e,n){const t=n?"milésima":"milésimo",i=n?"millonésima":"millonésimo";if(e<1000n)return M(Number(e),n);if(e<1000000n){const i=Number(e/1000n),r=Number(e%1000n);let o;return o=1===i?t:M(i,n)+" "+t,r>0&&(o+=" "+M(r,n)),o}const r=Number(e/1000000n),o=e%1000000n;let s;return s=1===r?i:M(r,n)+" "+i,o>0n&&(s+=" "+F(o,n)),s}const k={gender:"masculine"},C={gender:["masculine","feminine"]};function z(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,k,C);return F(r,"feminine"===s)}const J={and:!0};function L(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,J);let g="";return i&&(g=E+" "),(s>0n||0n===u)&&(g+=1n===s?"un euro":P(s,!1)+" euros"),u>0n&&(s>0n&&(g+=a?" con ":" "),g+=1n===u?"un céntimo":P(u,!1)+" céntimos"),g}export{q as cardinalDefaults,h as cardinalMax,T as cardinalValues,J as currencyDefaults,N as currencyMax,k as ordinalDefaults,w as ordinalMax,C as ordinalValues,S as toCardinal,L as toCurrency,z as toOrdinal};