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.22 kB
/*! n2words/pt-PT v5.1.2 | MIT License | github.com/forzagreen/n2words */ var e,t;e=this,t=function(e){"use strict";function t(e){let[t,n]=e.toLowerCase().split("e");const i=parseInt(n,10);let r="";"-"===t[0]&&(r="-",t=t.slice(1));const o=t.indexOf("."),s=-1===o?t:t.slice(0,o)+t.slice(o+1),u=(-1===o?t.length:o)+i;return u>=s.length?r+s+"0".repeat(u-s.length):u<=0?r+"0."+"0".repeat(-u)+s:r+s.slice(0,u)+"."+s.slice(u)}function n(e){const n=e.toString();return i(n)?t(n):n}function i(e){return e.includes("e")||e.includes("E")}function r(e){const t="-"===e[0];t&&(e=e.slice(1));const n=e.indexOf(".");if(-1===n)return{isNegative:t,integerPart:BigInt(e)};const i=e.slice(0,n)||"0",r=e.slice(n+1);return{isNegative:t,integerPart:BigInt(i),decimalPart:r}}function o(e){let n=e.trim();if(0===n.length||Number.isNaN(Number(n)))throw new RangeError(`Invalid currency format: "${e}"`);i(n)&&(n=t(n));const r="-"===n[0];r&&(n=n.slice(1));const o=n.indexOf(".");if(-1===o)return{isNegative:r,dollars:BigInt(n),cents:0n};const s=n.slice(0,o)||"0",u=(n.slice(o+1)+"00").slice(0,2);return{isNegative:r,dollars:BigInt(s),cents:BigInt(u)}}function s(e,t,n){if(null===t)return;if(e<t&&!(n&&BigInt(n)>=t))return;const i=t.toString().length-1,r=t===10n**BigInt(i)?`10^${i} - 1`:""+(t-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${r}`)}function u(e){return 10n**BigInt(3*(e+1))}const c=["","um","dois","três","quatro","cinco","seis","sete","oito","nove"],a=["dez","onze","doze","treze","catorze","quinze","dezasseis","dezassete","dezoito","dezanove"],l=["","","vinte","trinta","quarenta","cinquenta","sessenta","setenta","oitenta","noventa"],f=["","cento","duzentos","trezentos","quatrocentos","quinhentos","seiscentos","setecentos","oitocentos","novecentos"],m="mil",g="zero",d="menos",p=["","primeiro","segundo","terceiro","quarto","quinto","sexto","sétimo","oitavo","nono"],b=["décimo","décimo primeiro","décimo segundo","décimo terceiro","décimo quarto","décimo quinto","décimo sexto","décimo sétimo","décimo oitavo","décimo nono"],h=["","","vigésimo","trigésimo","quadragésimo","quinquagésimo","sexagésimo","septuagésimo","octogésimo","nonagésimo"],w=["","centésimo","ducentésimo","tricentésimo","quadringentésimo","quingentésimo","sexcentésimo","septingentésimo","octingentésimo","nongentésimo"],v="euros";function N(e){if(0===e)return{word:"",isExactHundred:!1};if(100===e)return{word:"cem",isExactHundred:!0};const t=e%10,n=Math.trunc(e/10)%10,i=Math.trunc(e/100),r=[];return i>0&&r.push(f[i]),1===n?r.push(a[t]):n>=2?r.push(t>0?l[n]+" e "+c[t]:l[n]):t>0&&r.push(c[t]),{word:r.join(" e "),isExactHundred:i>0&&0===n&&0===t,startsWithHundreds:e>=100}}const y=["",m,"milhão","mil milhões","bilião","mil biliões","trilião","mil triliões","quatrilião"],I=["",m,"milhões","mil milhões","biliões","mil biliões","triliões","mil triliões","quatriliões"],E=["","milésimo","milionésimo","mil milionésimo","bilionésimo","mil bilionésimo","trilionésimo"],O=u(y.length-1),x=u(E.length-1),q=u(y.length-1);function R(e){if(0n===e)return g;if(e<1000n)return N(Number(e)).word;if(e<1000000n){const t=Number(e/1000n),n=Number(e%1000n);let i;if(i=1===t?m:N(t).word+" "+m,n>0){const e=N(n);i+=e.startsWithHundreds?" "+e.word:" e "+e.word}return i}return function(e){const t=[];let n=e;for(;n>0n;)t.push(Number(n%1000n)),n/=1000n;let i=0;for(let e=0;e<t.length;e++)if(0!==t[e]){i=e;break}let r="",o=!1;for(let e=t.length-1;e>=0;e--){const n=t[e];if(0===n)continue;const s=N(n);if(r&&e===i&&o&&!s.startsWithHundreds&&(r+=" e"),r&&(r+=" "),0===e)r+=s.word,o=!1;else if(1===e)r+=1===n?m:s.word+" "+m,o=!0;else{const t=1===n?y[e]:I[e];r+=1===n?"um "+t:s.word+" "+t,o=!0}}return r}(e)}function B(e){if(0===e)return"";const t=e%10,n=Math.trunc(e/10)%10,i=Math.trunc(e/100),r=[];return i>0&&r.push(w[i]),1===n?r.push(b[t]):n>=2?(r.push(h[n]),t>0&&r.push(p[t])):t>0&&r.push(p[t]),r.join(" ")}const z={and:!0},P={ptPT:function(e){const n=function(e){const n=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 n=e.trim();if(0===n.length)throw new RangeError("Ordinals cannot be empty strings");if("-"===n[0])throw new RangeError("Ordinals cannot be negative");if(n.includes("."))throw new RangeError("Ordinals must be whole numbers");if(i(n)){const e=t(n);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(n);if(e<=0n)throw new RangeError("Ordinals must be positive integers");return e}catch(t){if(t instanceof RangeError)throw t;throw new RangeError(`Invalid ordinal format: "${e}"`,{cause:t})}}(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);if(s(n,x),n<10n)return p[Number(n)];if(n<20n)return b[Number(n)-10];if(n<100n){const e=Number(n%10n),t=Number(n/10n);return 0===e?h[t]:h[t]+" "+p[e]}return n<1000n?B(Number(n)):function(e){const t=[];let n=e;for(;n>0n;)t.push(Number(n%1000n)),n/=1000n;let i=0;for(let e=0;e<t.length;e++)if(0!==t[e]){i=e;break}let r="";for(let e=t.length-1;e>=0;e--){const n=t[e];if(0!==n)if(r&&(r+=" "),e===i)r+=0===e?B(n):1===n&&e>0?E[e]:B(n)+" "+E[e];else if(0===e)r+=N(n).word;else if(1===e)r+=1===n?m:N(n).word+" "+m;else{const t=1===n?y[e]:I[e];r+=1===n?"um "+t:N(n).word+" "+t}}return r}(n)}};e.currency={ptPT:function(e,t){const{isNegative:i,dollars:r,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}:o(n(e))}if("string"==typeof e)return o(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${t}`)}(e);s(r,q);const{and:c}=function(e,t){const n={...t};if(void 0===e)return n;if(!function(e){if(null===e||"object"!=typeof e)return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype}(e))throw new TypeError("Invalid options: expected plain object or undefined, got "+typeof e);const i=Object.keys(t);for(const[r,o]of Object.entries(e)){if(!Object.hasOwn(t,r))throw new TypeError(`Unknown option "${r}" — expected one of: ${i.join(", ")}`);if(void 0!==o){if(typeof o!=typeof n[r])throw new TypeError(`Option "${r}" must be a ${typeof n[r]}, got ${typeof o}`);n[r]=o}}return n}(t,z);let a="";i&&(a=d+" ");const l=r>0n,f=u>0n;return l||f?(l&&(a+=R(r)+" "+(1n===r?"euro":v)),f&&(l&&(a+=c?" e ":" "),a+=R(u)+" "+(1n===u?"cêntimo":"cêntimos")),a):g+" "+v}},e.ordinal=P,e.ptPT=function(e){const{isNegative:o,integerPart:u,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)}:r(n(e))}if("string"==typeof e)return r(function(e){const n=e.trim();if(0===n.length||Number.isNaN(Number(n)))throw new RangeError(`Invalid number format: "${e}"`);return i(n)?t(n):n}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(u,O,c);let a="";return o&&(a=d+" "),a+=R(u),c&&(a+=" vírgula "+function(e){let t="",n=0;for(;n<e.length&&"0"===e[n];)t&&(t+=" "),t+=g,n++;const i=e.slice(n);return i&&(t&&(t+=" "),t+=R(BigInt(i))),t}(c)),a}},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).n2words=e.n2words||{});