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) 8.56 kB
/*! n2words/es-ES 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 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 t(e){const t=e.toString();return i(t)?n(t):t}function i(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 i=e.slice(0,t)||"0",r=e.slice(t+1);return{isNegative:n,integerPart:BigInt(i),decimalPart:r}}function o(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);i(t)&&(t=n(t));const r="-"===t[0];r&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:r,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",c=(t.slice(o+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(s),cents:BigInt(c)}}function s(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 c(e){return 10n**BigInt(6*(e+1))}function u(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 a=["","uno","dos","tres","cuatro","cinco","seis","siete","ocho","nueve"],f=["","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"],m=["veinte","veintiuna","veintidós","veintitrés","veinticuatro","veinticinco","veintiséis","veintisiete","veintiocho","veintinueve"],d=["","","","treinta","cuarenta","cincuenta","sesenta","setenta","ochenta","noventa"],p=["","ciento","doscientos","trescientos","cuatrocientos","quinientos","seiscientos","setecientos","ochocientos","novecientos"],v=["","cienta","doscientas","trescientas","cuatrocientas","quinientas","seiscientas","setecientas","ochocientas","novecientas"],b=["millón","billón","trillón","cuatrillón"],h=["millones","billones","trillones","cuatrillones"],w=c(b.length),N=10n**BigInt(9),y=c(b.length),I="mil",E="cero",O="menos",R=["","primero","segundo","tercero","cuarto","quinto","sexto","séptimo","octavo","noveno"],B=["","primera","segunda","tercera","cuarta","quinta","sexta","séptima","octava","novena"],x=["","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 j(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:p)[r]),0===o||s.push(o<10?(n?f:a)[o]:o<20?l[t]:o<30?(n?m:g)[t]:0===t?d[i]:d[i]+" y "+(n?f:a)[t]),s.join(" ")}function P(e,n){if(1===e)return I;if(e%2==0){const t=e/2-1,i=b[t];return i?n>1n?h[t]:i:""}{const n=h[(e-1)/2-1];return n?I+" "+n:I}}function q(e,n){if(0n===e)return E;if(e<1000n)return j(Number(e),n);if(e<1000000n){const t=Number(e/1000n),i=Number(e%1000n);let r;if(1===t)r=I;else{const e=j(t,!1);r="uno"===e||"una"===e?I:e+" "+I}return i>0&&(r+=" "+j(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?P(e,i):"";r&&(r+=" "),r+=0===e?j(Number(i),n):1===e?1n===i?I:j(Number(i),!1)+" "+o:e%2==1?1n===i?o:j(Number(i),!1)+" "+o:1n===i?"un "+o:j(Number(i),!1)+" "+o}return r}(e,n)}const S={gender:"masculine"},T={gender:["masculine","feminine"]};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?B:R,s=n?$:x,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"]},z={and:!0},J={esES:function(e,t){const r=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(i(t)){const e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const i=BigInt(e);if(i<=0n)throw new RangeError("Ordinals must be positive integers");return i}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);s(r,N);const{gender:o}=u(t,k,C);return F(r,"feminine"===o)}};e.currency={esES:function(e,n){const{isNegative:i,dollars:r,cents:c}=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(r,y);const{and:a}=u(n,z);let f="";return i&&(f=O+" "),(r>0n||0n===c)&&(f+=1n===r?"un euro":q(r,!1)+" euros"),c>0n&&(r>0n&&(f+=a?" con ":" "),f+=1n===c?"un céntimo":q(c,!1)+" céntimos"),f}},e.esES=function(e,o){const{isNegative:c,integerPart:a,decimalPart:f}=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)}:r(t(e))}if("string"==typeof e)return r(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return i(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(a,w,f);const{gender:l}=u(o,S,T),g="feminine"===l;let m="";return c&&(m=O+" "),m+=q(a,g),f&&(m+=" punto "+function(e,n){let t="",i=0;for(;i<e.length&&"0"===e[i];)t&&(t+=" "),t+=E,i++;const r=e.slice(i);return r&&(t&&(t+=" "),t+=q(BigInt(r),n)),t}(f,g)),m},e.ordinal=J},"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||{});