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.06 kB
/*! n2words/pt-PT v5.1.2 | MIT License | github.com/forzagreen/n2words */ function e(e){let[t,n]=e.toLowerCase().split("e");const r=parseInt(n,10);let i="";"-"===t[0]&&(i="-",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)+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 t(t){const r=t.toString();return n(r)?e(r):r}function n(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 r=e.slice(0,n)||"0",i=e.slice(n+1);return{isNegative:t,integerPart:BigInt(r),decimalPart:i}}function i(t){let r=t.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid currency format: "${t}"`);n(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,t,n){if(null===t)return;if(e<t&&!(n&&BigInt(n)>=t))return;const r=t.toString().length-1,i=t===10n**BigInt(r)?`10^${r} - 1`:""+(t-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=["","um","dois","três","quatro","cinco","seis","sete","oito","nove"],c=["dez","onze","doze","treze","catorze","quinze","dezasseis","dezassete","dezoito","dezanove"],a=["","","vinte","trinta","quarenta","cinquenta","sessenta","setenta","oitenta","noventa"],l=["","cento","duzentos","trezentos","quatrocentos","quinhentos","seiscentos","setecentos","oitocentos","novecentos"],f="mil",m="zero",g="menos",d=["","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"],p=["","","vigésimo","trigésimo","quadragésimo","quinquagésimo","sexagésimo","septuagésimo","octogésimo","nonagésimo"],h=["","centésimo","ducentésimo","tricentésimo","quadringentésimo","quingentésimo","sexcentésimo","septingentésimo","octingentésimo","nongentésimo"],w="euros";function v(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,r=Math.trunc(e/100),i=[];return r>0&&i.push(l[r]),1===n?i.push(c[t]):n>=2?i.push(t>0?a[n]+" e "+u[t]:a[n]):t>0&&i.push(u[t]),{word:i.join(" e "),isExactHundred:r>0&&0===n&&0===t,startsWithHundreds:e>=100}}const N=["",f,"milhão","mil milhões","bilião","mil biliões","trilião","mil triliões","quatrilião"],y=["",f,"milhões","mil milhões","biliões","mil biliões","triliões","mil triliões","quatriliões"],I=["","milésimo","milionésimo","mil milionésimo","bilionésimo","mil bilionésimo","trilionésimo"],E=s(N.length-1),O=s(I.length-1),q=s(N.length-1);function R(e){if(0n===e)return m;if(e<1000n)return v(Number(e)).word;if(e<1000000n){const t=Number(e/1000n),n=Number(e%1000n);let r;if(r=1===t?f:v(t).word+" "+f,n>0){const e=v(n);r+=e.startsWithHundreds?" "+e.word:" e "+e.word}return r}return function(e){const t=[];let n=e;for(;n>0n;)t.push(Number(n%1000n)),n/=1000n;let r=0;for(let e=0;e<t.length;e++)if(0!==t[e]){r=e;break}let i="",o=!1;for(let e=t.length-1;e>=0;e--){const n=t[e];if(0===n)continue;const s=v(n);if(i&&e===r&&o&&!s.startsWithHundreds&&(i+=" e"),i&&(i+=" "),0===e)i+=s.word,o=!1;else if(1===e)i+=1===n?f:s.word+" "+f,o=!0;else{const t=1===n?N[e]:y[e];i+=1===n?"um "+t:s.word+" "+t,o=!0}}return i}(e)}function x(i){const{isNegative:s,integerPart:u,decimalPart:c}=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(t(i))}if("string"==typeof i)return r(function(t){const r=t.trim();if(0===r.length||Number.isNaN(Number(r)))throw new RangeError(`Invalid number format: "${t}"`);return n(r)?e(r):r}(i));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(i);o(u,E,c);let a="";return s&&(a=g+" "),a+=R(u),c&&(a+=" vírgula "+function(e){let t="",n=0;for(;n<e.length&&"0"===e[n];)t&&(t+=" "),t+=m,n++;const r=e.slice(n);return r&&(t&&(t+=" "),t+=R(BigInt(r))),t}(c)),a}function B(e){if(0===e)return"";const t=e%10,n=Math.trunc(e/10)%10,r=Math.trunc(e/100),i=[];return r>0&&i.push(h[r]),1===n?i.push(b[t]):n>=2?(i.push(p[n]),t>0&&i.push(d[t])):t>0&&i.push(d[t]),i.join(" ")}function z(t){const r=function(t){const r=typeof t;if("bigint"==typeof t){if(t<=0n)throw new RangeError("Ordinals must be positive integers");return t}if("number"==typeof t){if(!Number.isFinite(t))throw new RangeError("Ordinals must be finite numbers");if(!Number.isInteger(t))throw new RangeError("Ordinals must be whole numbers");if(t<=0)throw new RangeError("Ordinals must be positive integers");return BigInt(t)}if("string"==typeof t)return function(t){const r=t.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(n(r)){const t=e(r);if(t.includes("."))throw new RangeError("Ordinals must be whole numbers");const n=BigInt(t);if(n<=0n)throw new RangeError("Ordinals must be positive integers");return n}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: "${t}"`,{cause:e})}}(t);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${r}`)}(t);if(o(r,O),r<10n)return d[Number(r)];if(r<20n)return b[Number(r)-10];if(r<100n){const e=Number(r%10n),t=Number(r/10n);return 0===e?p[t]:p[t]+" "+d[e]}return r<1000n?B(Number(r)):function(e){const t=[];let n=e;for(;n>0n;)t.push(Number(n%1000n)),n/=1000n;let r=0;for(let e=0;e<t.length;e++)if(0!==t[e]){r=e;break}let i="";for(let e=t.length-1;e>=0;e--){const n=t[e];if(0!==n)if(i&&(i+=" "),e===r)i+=0===e?B(n):1===n&&e>0?I[e]:B(n)+" "+I[e];else if(0===e)i+=v(n).word;else if(1===e)i+=1===n?f:v(n).word+" "+f;else{const t=1===n?N[e]:y[e];i+=1===n?"um "+t:v(n).word+" "+t}}return i}(r)}const $={and:!0};function j(e,n){const{isNegative:r,dollars:s,cents:u}=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}:i(t(e))}if("string"==typeof e)return i(e);throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${n}`)}(e);o(s,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 r=Object.keys(t);for(const[i,o]of Object.entries(e)){if(!Object.hasOwn(t,i))throw new TypeError(`Unknown option "${i}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof n[i])throw new TypeError(`Option "${i}" must be a ${typeof n[i]}, got ${typeof o}`);n[i]=o}}return n}(n,$);let a="";r&&(a=g+" ");const l=s>0n,f=u>0n;return l||f?(l&&(a+=R(s)+" "+(1n===s?"euro":w)),f&&(l&&(a+=c?" e ":" "),a+=R(u)+" "+(1n===u?"cêntimo":"cêntimos")),a):m+" "+w}export{E as cardinalMax,$ as currencyDefaults,q as currencyMax,O as ordinalMax,x as toCardinal,j as toCurrency,z as toOrdinal};