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.81 kB
JavaScript
/*! n2words/pt-BR 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 r=parseInt(t,10);let i="";"-"===n[0]&&(i="-",n=n.slice(1));const o=n.indexOf("."),s=-1===o?n:n.slice(0,o)+n.slice(o+1),u=(-1===o?n.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(e){const t=e.toString();return r(t)?n(t):t}function r(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 r=e.slice(0,t)||"0",i=e.slice(t+1);return{isNegative:n,integerPart:BigInt(r),decimalPart:i}}function o(e){let t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid currency format: "${e}"`);r(t)&&(t=n(t));const i="-"===t[0];i&&(t=t.slice(1));const o=t.indexOf(".");if(-1===o)return{isNegative:i,dollars:BigInt(t),cents:0n};const s=t.slice(0,o)||"0",u=(t.slice(o+1)+"00").slice(0,2);return{isNegative:i,dollars:BigInt(s),cents:BigInt(u)}}function s(e,n,t){if(null===n)return;if(e<n&&!(t&&BigInt(t)>=n))return;const r=n.toString().length-1,i=n===10n**BigInt(r)?`10^${r} - 1`:""+(n-1n);throw new RangeError(`Number too large to convert: the largest supported value is ${i}`)}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","quatorze","quinze","dezesseis","dezessete","dezoito","dezenove"],l=["","","vinte","trinta","quarenta","cinquenta","sessenta","setenta","oitenta","noventa"],f=["","cento","duzentos","trezentos","quatrocentos","quinhentos","seiscentos","setecentos","oitocentos","novecentos"],m="mil",d="zero",g="menos",p=["","primeiro","segundo","terceiro","quarto","quinto","sexto","sétimo","oitavo","nono"],h=["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"],b=["","","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={BRL:{major:["real","reais"],minor:["centavo","centavos"]},USD:{major:["dólar","dólares"],minor:["centavo","centavos"]},EUR:{major:["euro","euros"],minor:["centavo","centavos"]},GBP:{major:["libra","libras"],minor:["pêni","pence"]},JPY:{major:["iene","ienes"],minor:["sen","sen"]}},y={minor:["centavo","centavos"]};function N(e){if(0===e)return{word:"",isExactHundred:!1};if(100===e)return{word:"cem",isExactHundred:!0};const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100),i=[];return r>0&&i.push(f[r]),1===t?i.push(a[n]):t>=2?i.push(n>0?l[t]+" e "+c[n]:l[t]):n>0&&i.push(c[n]),{word:i.join(" e "),isExactHundred:r>0&&0===t&&0===n,startsWithHundreds:e>=100}}const E=["",m,"milhão","bilhão","trilhão","quatrilhão","quintilhão","sextilhão","setilhão"],I=["",m,"milhões","bilhões","trilhões","quatrilhões","quintilhões","sextilhões","setilhões"],R=["","milésimo","milionésimo","bilionésimo","trilionésimo","quatrilionésimo","quintilionésimo","sextilionésimo"],x=u(E.length-1),B=u(R.length-1),q=u(E.length-1);function O(e){if(0n===e)return d;if(e<1000n)return N(Number(e)).word;if(e<1000000n){const n=Number(e/1000n),t=Number(e%1000n);let r;if(r=1===n?m:N(n).word+" "+m,t>0){const e=N(t);r+=!e.startsWithHundreds||e.isExactHundred?" e "+e.word:" "+e.word}return r}return function(e){const n=[];let t=e;for(;t>0n;)n.push(Number(t%1000n)),t/=1000n;let r=0;for(let e=0;e<n.length;e++)if(0!==n[e]){r=e;break}let i="",o=!1;for(let e=n.length-1;e>=0;e--){const t=n[e];if(0===t)continue;const s=N(t);if(i&&e===r&&o&&(!s.startsWithHundreds||s.isExactHundred)&&(i+=" e"),i&&(i+=" "),0===e)i+=s.word,o=!1;else if(1===e)i+=1===t?m:s.word+" "+m,o=!0;else{const n=1===t?E[e]:I[e];i+=1===t?"um "+n:s.word+" "+n,o=!0}}return i}(e)}function j(e){if(0===e)return"";const n=e%10,t=Math.trunc(e/10)%10,r=Math.trunc(e/100),i=[];return r>0&&i.push(w[r]),1===t?i.push(h[n]):t>=2?(i.push(b[t]),n>0&&i.push(p[n])):n>0&&i.push(p[n]),i.join(" ")}const z={and:!0,currency:""},$={ptBR:function(e){const t=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(r(t)){const e=n(t);if(e.includes("."))throw new RangeError("Ordinals must be whole numbers");const r=BigInt(e);if(r<=0n)throw new RangeError("Ordinals must be positive integers");return r}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);if(s(t,B),t<10n)return p[Number(t)];if(t<20n)return h[Number(t)-10];if(t<100n){const e=Number(t%10n),n=Number(t/10n);return 0===e?b[n]:b[n]+" "+p[e]}return t<1000n?j(Number(t)):function(e){const n=[];let t=e;for(;t>0n;)n.push(Number(t%1000n)),t/=1000n;let r=0;for(let e=0;e<n.length;e++)if(0!==n[e]){r=e;break}let i="";for(let e=n.length-1;e>=0;e--){const t=n[e];if(0!==t)if(i&&(i+=" "),e===r)i+=0===e?j(t):1===t&&e>0?R[e]:j(t)+" "+R[e];else if(0===e)i+=N(t).word;else if(1===e)i+=1===t?m:N(t).word+" "+m;else{const n=1===t?E[e]:I[e];i+=1===t?"um "+n:N(t).word+" "+n}}return i}(t)}};e.currency={ptBR:function(e,n){const{isNegative:r,dollars:i,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}: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(i,q);const{and:c,currency:a}=function(e,n){const t={...n};if(void 0===e)return t;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[i,o]of Object.entries(e)){if(!Object.hasOwn(n,i))throw new TypeError(`Unknown option "${i}" — expected one of: ${r.join(", ")}`);if(void 0!==o){if(typeof o!=typeof t[i])throw new TypeError(`Option "${i}" must be a ${typeof t[i]}, got ${typeof o}`);t[i]=o}}return t}(n,z);let l=a;if(!l){try{const e=new Intl.Locale("pt-BR");l=e.getCurrencies?.()[0]}catch{}l=l||"BRL"}l=l.toUpperCase();const f=v[l]||{major:[l,l],minor:y.minor};let m="";r&&(m=g+" ");const p=i>0n,h=u>0n;if(!p&&!h)return d+" "+f.major[1];if(p&&(m+=O(i)+" "+(1n===i?f.major[0]:f.major[1])),h){p&&(m+=c?" e ":" ");const e=O(u),n=1n===u?f.minor[0]:f.minor[1];m+=""===n?e:e+" "+n}return m}},e.ordinal=$,e.ptBR=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)}:i(t(e))}if("string"==typeof e)return i(function(e){const t=e.trim();if(0===t.length||Number.isNaN(Number(t)))throw new RangeError(`Invalid number format: "${e}"`);return r(t)?n(t):t}(e));throw new TypeError(`Invalid value type: expected number, string, or bigint, received ${o}`)}(e);s(u,x,c);let a="";return o&&(a=g+" "),a+=O(u),c&&(a+=" vírgula "+function(e){let n="",t=0;for(;t<e.length&&"0"===e[t];)n&&(n+=" "),n+=d,t++;const r=e.slice(t);return r&&(n&&(n+=" "),n+=O(BigInt(r))),n}(c)),a}},"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||{});