extenso
Version:
Uma biblioteca avançada para escrever números por extenso (em português).
6 lines (4 loc) • 14.2 kB
JavaScript
/*! extenso v3.0.0 | MIT (c) 2015-2026 by Matheus Alves */
;var extenso=(()=>{var T=Object.defineProperty;var Ae=Object.getOwnPropertyDescriptor;var Le=Object.getOwnPropertyNames;var Ce=Object.prototype.hasOwnProperty;var Ne=(e,r)=>{for(var t in r)T(e,t,{get:r[t],enumerable:!0})},Me=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of Le(r))!Ce.call(e,o)&&o!==t&&T(e,o,{get:()=>r[o],enumerable:!(n=Ae(r,o))||n.enumerable});return e};var Se=e=>Me(T({},"__esModule",{value:!0}),e);var wr={};Ne(wr,{default:()=>xr});var D=(u=>(u.AOA="AOA",u.CVE="CVE",u.BRL="BRL",u.XOF="XOF",u.MZN="MZN",u.EUR="EUR",u.STN="STN",u.USD="USD",u.MOP="MOP",u))(D||{}),c=D;var G={MOP$:c.MOP,R$:c.BRL,"\u20AC":c.EUR,$:c.USD,Kz:c.AOA,Esc:c.CVE,CFA:c.XOF,MT:c.MZN,Db:c.STN},Te=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),ve=Object.keys(G).sort((e,r)=>r.length-e.length).map(Te).join("|"),Oe=Object.values(c).join("|"),z=new RegExp(`${ve}|(?<![A-Za-z])(?:${Oe})(?![A-Za-z])`,"g"),P=e=>Array.from(e.matchAll(z),r=>G[r[0]]??r[0]),q=e=>e.replace(z," ");var U=1e3,Re=e=>{let r=/^(-?)(\d+)(?:\.(\d+))?[eE]([+-]?\d+)$/.exec(e);if(!r)return e;let[,t,n,o="",s]=r,a=Number(s),m=`${n}${o}`;return a<0?`${t}0.${"0".repeat(Math.abs(a)-1)}${m}`:`${t}${m}${"0".repeat(a-o.length)}`},De=e=>{if(typeof e=="bigint"&&(e=e.toString()),typeof e!="string"&&typeof e!="number")throw new TypeError("Input must be a string, number or bigint");if(typeof e=="number"&&!Number.isFinite(e))throw new RangeError("Input number must be finite");let r=typeof e=="number"?Re(e.toString()):e.toString();if(r.length>U)throw new Error(`Input exceeds maximum length of ${U} characters`);if(/^[+-]?(?:NaN|Infinity)$/.test(r.trim()))throw new RangeError("Input number must be finite");let t=q(r).trim().replace(/^-\s+/,"-");if(/\s/.test(t))throw new Error("Invalid number format: whitespace is not allowed within the number");return t},F=De;var B=(t=>(t.COMMA="comma",t.POINT="point",t))(B||{}),g=B;var f=class extends Error{constructor(t,n){super(t);this.code=n;this.name="ParseError"}},Ge=(e,r=g.POINT,t=!1)=>{e=e.trim();let n={decimal:r===g.POINT?".":",",thousands:r===g.POINT?",":"."};if(e==="")throw new f("Input cannot be empty","EMPTY_INPUT");if(e==="-")throw new f("A negative sign must be followed by a number","INVALID_NUMBER");if(!/^-?[^-]+$/.test(e))throw new f("Invalid number format: the negative sign is only allowed at the beginning.","INVALID_NUMBER");let s=(e.startsWith("-")?e.slice(1):e).split(n.decimal);if(s.length>2)throw new f(`Invalid number format: multiple decimal separators found. Use only one '${n.decimal}' as decimal separator.`,"MULTIPLE_DECIMALS");if(s.length===2&&s[1]==="")throw new f("Invalid number format: decimal separator must be followed by digits.","INCOMPLETE_DECIMAL");let[a,m]=s,d=a;if(d.includes(n.thousands)){let u=d.split(n.thousands);if(!(/^\d{1,3}$/.test(u[0])&&u.slice(1).every(p=>/^\d{3}$/.test(p))))throw new f(`Invalid thousands grouping for separator '${n.thousands}'.`,"INVALID_GROUPING");d=u.join("")}if(d||(d="0"),!/^\d+$/.test(d))throw new f(`Invalid integer part: "${d}". Only digits are allowed.`,"INVALID_INTEGER");if(m&&!/^\d+$/.test(m))throw new f(`Invalid decimal part: "${m}". Only digits are allowed.`,"INVALID_DECIMAL");return{integer:t?d:d.replace(/^0+(?=\d)/,""),decimal:m||"0"}},_=Ge;var V=(t=>(t.BR="br",t.PT="pt",t))(V||{}),x=V;var ze=(e,r=x.BR)=>{switch(r){case x.PT:return e=e.replace(/\bquatorze\b/g,"catorze").replace(/\bdezesseis\b/g,"dezasseis").replace(/\bdezessete\b/g,"dezassete").replace(/\bdezenove\b/g,"dezanove"),e.replace(/[^m]ilh(ão|ões)/g,t=>t.replace(/h/g,"i"));case x.BR:default:return e}},H=ze;var j=(o=>(o.ABBREVIATED="abbreviated",o.CURRENCY="currency",o.DIGIT="digit",o.NUMBER="number",o))(j||{}),y=j;var k=(t=>(t.MALE="male",t.FEMALE="female",t))(k||{}),i=k;var Z=(t=>(t.SHORT="short",t.LONG="long",t))(Z||{}),l=Z;var A=e=>e!==null&&typeof e=="object"&&!Array.isArray(e),E=(e,r,t)=>{if(r!==void 0&&!Object.values(t).includes(r))throw new TypeError(`Invalid ${e}: ${String(r)}`)},$=(e,r)=>{if(typeof r!="string"||r.trim()==="")throw new TypeError(`Invalid ${e}: expected a non-empty string`)},Pe=e=>{if(e.code!==void 0)throw new TypeError("Invalid currency options: code cannot be combined with a custom currency");if($("currency.singular",e.singular),$("currency.plural",e.plural),E("currency.gender",e.gender,i),!A(e.subunit))throw new TypeError("Invalid currency.subunit: expected an object");let r=e.subunit;$("currency.subunit.singular",r.singular),$("currency.subunit.plural",r.plural),E("currency.subunit.gender",r.gender,i)},qe=e=>{if(!A(e))throw new TypeError("Options must be an object");if(E("mode",e.mode,y),E("locale",e.locale,x),E("scale",e.scale,l),E("decimalSeparator",e.decimalSeparator,g),e.removeAccents!==void 0&&typeof e.removeAccents!="boolean")throw new TypeError(`Invalid removeAccents: ${String(e.removeAccents)}`);if(e.currency!==void 0&&!A(e.currency))throw new TypeError("Invalid currency options: expected an object");if(e.number!==void 0&&!A(e.number))throw new TypeError("Invalid number options: expected an object");let r=e.currency;r&&("singular"in r||"plural"in r||"gender"in r||"subunit"in r)?Pe(r):E("currency.code",r?.code,c);let t=e.number;if(E("number.gender",t?.gender,i),t?.ordinal!==void 0&&typeof t.ordinal!="boolean")throw new TypeError(`Invalid number.ordinal: ${String(t.ordinal)}`)},X=qe;var Ue=e=>e.normalize("NFD").replace(new RegExp("\\p{Diacritic}","gu"),""),Y=Ue;var Fe={[l.SHORT]:["mil","mi","bi","tri","qua","qui","sex","sep","oct","non","dec","undec","duodec"],[l.LONG]:["mil","mi","mil mi","bi","mil bi","tri","mil tri","qua","mil qua","qui","mil qui","sex","mil sex","sep","mil sep","oct","mil oct","non","mil non","dec","mil dec","undec","mil undec","duodec"]},Be=(e,r)=>{let t=r.replace(/0+$/,"");return t===""?e:`${e},${t}`},_e=(e,r,t)=>{let n=r==="0"?"":r,o=BigInt(`${e}${n}`),s=10n**BigInt(n.length),a=10n**BigInt(t),m=s*a;return(o*10n+m/2n)/m},Ve=(e,r="0",t=l.SHORT)=>{if(e.length<4)return Be(e,r);let n=Math.floor((e.length-1)/3)-1,o=_e(e,r,(n+1)*3);o===10000n&&(n+=1,o=10n);let s=Fe[t][n];if(s===void 0)throw new Error(`Number exceeds ${t} scale limit`);let a=o/10n,m=o%10n;return`${m===0n?a.toString():`${a},${m}`} ${s}`},W=Ve;var He={[c.AOA]:{singular:"kwanza",plural:"kwanzas",gender:i.MALE,subunit:{singular:"c\xEAntimo",plural:"c\xEAntimos",gender:i.MALE}},[c.CVE]:{singular:"escudo",plural:"escudos",gender:i.MALE,subunit:{singular:"centavo",plural:"centavos",gender:i.MALE}},[c.BRL]:{singular:"real",plural:"reais",gender:i.MALE,subunit:{singular:"centavo",plural:"centavos",gender:i.MALE}},[c.XOF]:{singular:"franco",plural:"francos",gender:i.MALE,subunit:{singular:"centavo",plural:"centavos",gender:i.MALE}},[c.MZN]:{singular:"metical",plural:"meticais",gender:i.MALE,subunit:{singular:"centavo",plural:"centavos",gender:i.MALE}},[c.EUR]:{singular:"euro",plural:"euros",gender:i.MALE,subunit:{singular:"c\xEAntimo",plural:"c\xEAntimos",gender:i.MALE}},[c.STN]:{singular:"dobra",plural:"dobras",gender:i.FEMALE,subunit:{singular:"centavo",plural:"centavos",gender:i.MALE}},[c.USD]:{singular:"d\xF3lar",plural:"d\xF3lares",gender:i.MALE,subunit:{singular:"centavo",plural:"centavos",gender:i.MALE}},[c.MOP]:{singular:"pataca",plural:"patacas",gender:i.FEMALE,subunit:{singular:"avo",plural:"avos",gender:i.MALE}}},v=He;var je=e=>{let r=[];for(let t=e.length;t>0;t-=3){let n=Math.max(0,t-3);r.unshift(e.slice(n,t))}return r.map(Number)},L=je;var C="mil",ke={[l.SHORT]:["mil","milh\xE3o","bilh\xE3o","trilh\xE3o","quatrilh\xE3o","quintilh\xE3o","sextilh\xE3o","septilh\xE3o","octilh\xE3o","nonilh\xE3o","decilh\xE3o","undecilh\xE3o","duodecilh\xE3o"],[l.LONG]:["mil","milh\xE3o","mil milh\xE3o","bilh\xE3o","mil bilh\xE3o","trilh\xE3o","mil trilh\xE3o","quatrilh\xE3o","mil quatrilh\xE3o","quintilh\xE3o","mil quintilh\xE3o","sextilh\xE3o","mil sextilh\xE3o","septilh\xE3o","mil septilh\xE3o","octilh\xE3o","mil octilh\xE3o","nonilh\xE3o","mil nonilh\xE3o","decilh\xE3o","mil decilh\xE3o","undecilh\xE3o","mil undecilh\xE3o","duodecilh\xE3o"]},N=ke;var Ze=["zero","um","dois","tr\xEAs","quatro","cinco","seis","sete","oito","nove"],K=["zero","uma","duas","tr\xEAs","quatro","cinco","seis","sete","oito","nove"],J=Ze;var Xe=["dez","onze","doze","treze","quatorze","quinze","dezesseis","dezessete","dezoito","dezenove"],Q=Xe;var Ye=["vinte","trinta","quarenta","cinquenta","sessenta","setenta","oitenta","noventa"],ee=Ye;var We=["cento","duzentos","trezentos","quatrocentos","quinhentos","seiscentos","setecentos","oitocentos","novecentos"],re=["cento","duzentas","trezentas","quatrocentas","quinhentas","seiscentas","setecentas","oitocentas","novecentas"],te=We;var I=(e,r=i.MALE)=>r===i.FEMALE?K[e]:J[e],Je=(e,r=i.MALE)=>e<10?I(e,r):Q[e-10],ne=(e,r=i.MALE)=>{if(e<20)return Je(e,r);let t=ee[(e-e%10)/10-2];return e%10===0?t:`${t} e ${I(e%10,r)}`},Qe=(e,r=i.MALE)=>{if(e<100)return ne(e,r);let n=(r===i.FEMALE?re:te)[(e-e%100)/100-1];return e%100===0?e===100?"cem":n:`${n} e ${ne(e%100,r)}`},M=Qe;var oe=e=>e<100||e%100===0,er=(e,r)=>r===1?e===C?"":e:e.replace("\xE3o","\xF5es"),rr=(e,r,t,n,o)=>{if(e===0)return"";let s=N[n][r-1],m=r===0||s===C?o:i.MALE,d=M(e,m);if(r===0)return oe(e)?`e ${d}`:d;if(s===void 0)throw new Error(`Number exceeds ${n} scale limit`);let u=er(s,e),w=`${u?`${d} `:""}${u||C}`,p=t.slice(r+1).some(S=>S>0),$e=t.slice(0,r).every(S=>S===0);return p&&$e&&oe(e)?`e ${w}`:w},tr=e=>e.map((r,t)=>t<e.length-2?`${r},`:r),nr=(e,r=l.SHORT,t=i.MALE)=>{let n=L(e).reverse(),o=n.map((s,a)=>rr(s,a,n,r,t)).reverse().filter(Boolean);return tr(o).join(" ")},ie=nr;var or=(e,r=l.SHORT,t=i.MALE)=>{let n=e.replace(/^0+(?=\d)/,"");return n.length<4?M(Number(n),t):ie(n,r,t)},h=or;var se=1000000n,ae=(e,r,t=l.SHORT)=>{let n=h(e,t,r.gender),o=BigInt(e);return o===1n?`${n} ${r.singular}`:o>=se&&o%se===0n?`${n} de ${r.plural}`:`${n} ${r.plural}`},ce=(e,r)=>{let t=h(e.slice(0,2),l.SHORT,r.subunit.gender);return BigInt(e)===1n?`${t} ${r.subunit.singular}`:`${t} ${r.subunit.plural}`},ir=(e,r="0",t=c.BRL,n=l.SHORT)=>{if(!/^\d+$/.test(e)||!/^\d{1,2}$/.test(r))throw new Error("Currency values must have zero, one, or two decimal places");r=r.padEnd(2,"0");let o;if(typeof t=="string"){if(!Object.keys(v).includes(t))throw new Error("Invalid currency");o=v[t]}else o=t;let s=BigInt(e)>0n,a=BigInt(r)>0n;return!s&&!a?`zero ${o.plural}`:s?a?`${ae(e,o,n)} e ${ce(r,o)}`:ae(e,o,n):ce(r,o)},le=ir;var sr=e=>{let r=e.split(/[.,]/),t=r[0].split("").map(o=>I(Number(o))).join(" ");if(r.length===1||!r[1]||r[1].trim()==="")return t;let n=r[1].split("").map(o=>I(Number(o))).join(" ");return`${t} v\xEDrgula ${n}`},O=sr;var ar=(e,r)=>r===1?e:`${e}s`,b=ar;var cr=["mil\xE9simo","milion\xE9simo","bilion\xE9simo","trilion\xE9simo","quatrilion\xE9simo","quintilion\xE9simo","sextilion\xE9simo","septilion\xE9simo","octilion\xE9simo","nonilion\xE9simo","decilion\xE9simo","undecilion\xE9simo","duodecilion\xE9simo"],me=cr;var lr=e=>{let r=h(e),t=Number(e);if(e.length===1)return`${r} ${b("d\xE9cimo",t)}`;if(e.length===2)return`${r} ${b("cent\xE9simo",t)}`;let n=me[Math.floor(e.length/3)-1];if(n===void 0)throw new Error("Number exceeds limit");switch(e.length%3){case 1:return`${r} ${b("d\xE9cimo",t)} de ${n}`;case 2:return`${r} ${b("cent\xE9simo",t)} de ${n}`;default:return`${r} ${b(n,t)}`}},R=lr;var ue=["","primeiro","segundo","terceiro","quarto","quinto","sexto","s\xE9timo","oitavo","nono"],de=["d\xE9cimo","vig\xE9simo","trig\xE9simo","quadrag\xE9simo","quinquag\xE9simo","sexag\xE9simo","septuag\xE9simo","octog\xE9simo","nonag\xE9simo"],pe=["cent\xE9simo","ducent\xE9simo","trecent\xE9simo","quadringent\xE9simo","quingent\xE9simo","sexcent\xE9simo","septingent\xE9simo","octingent\xE9simo","nongent\xE9simo"],fe=["mil\xE9simo","milion\xE9simo","bilion\xE9simo","trilion\xE9simo","quadrilion\xE9simo","quintilion\xE9simo","sextilion\xE9simo","septilion\xE9simo","octilion\xE9simo","nonilion\xE9simo","decilion\xE9simo","undecilion\xE9simo","duodecilion\xE9simo"],mr=fe.flatMap((e,r)=>r===0?[e]:[e,`mil\xE9simo ${e}`]).slice(0,N[l.LONG].length),ur={[l.SHORT]:fe,[l.LONG]:mr},ge=ur;var he=(e,r)=>r===i.MALE?e:e.replace(/o\b/g,"a"),Ee=(e,r=i.MALE)=>{let t=[pe[Math.floor(e/100)-1],de[Math.floor(e%100/10)-1],ue[e%10]].filter(n=>!!n);return he(t.join(" "),r)},dr=(e,r=l.SHORT,t=i.MALE)=>{if(BigInt(e)===0n)return t===i.FEMALE?"zer\xE9sima":"zer\xE9simo";let n=L(e),o=ge[r];return n.map((s,a)=>{if(s===0)return"";let m=n.length-a-2;if(m<0)return Ee(s,t);let d=o[m];if(d===void 0)throw new Error(`Number exceeds ${r} scale limit`);let u=s===1?"":`${Ee(s,t)} `;return he(`${u}${d}`,t)}).filter(Boolean).join(" ")},be=dr;var pr=(e,r="0",t=l.SHORT,n=i.MALE,o=!1)=>{let s=/[1-9]/.test(r);if(o){if(s)throw new RangeError("Ordinal numbers must be integers");return be(e,t,n)}if(e==="0"&&!s)return"zero";if(e==="0")return R(r);let a;if(!s)a=h(e,t,n);else{let m=n===i.FEMALE?"inteira":"inteiro";a=`${h(e,t,n)} ${b(m,Number(e))} e ${R(r)}`}return a},ye=pr;var fr=(e,r,t,n)=>{let{integer:o,decimal:s,decimalSeparator:a,hasDecimalSeparator:m}=e;switch(r){case"abbreviated":return W(o,s,n.scale);case"currency":if(s.length>2)throw new RangeError("Currency values must have zero, one, or two decimal places");return le(o,s,t,n.scale);case"digit":return m?O(`${o}${a}${s}`):O(o);case"number":return ye(o,s,n.scale,n.number?.gender,n.number?.ordinal)}},xe=fr;var gr=e=>{let r=[...new Set(P(e))];if(r.length>1)throw new Error(`Conflicting currency markers: ${r.join(", ")}`);return r[0]},we=gr;var Er=e=>e!==void 0&&e.singular!==void 0,hr=(e,r)=>{let t=typeof e=="string"?we(e):void 0,n=Er(r.currency)?r.currency:void 0,o=r.currency?.code??t??c.BRL;return{currency:n??o,mode:r.mode??(t||r.currency!==void 0?y.CURRENCY:y.NUMBER)}},Ie=hr;var br="-",yr=(e,r={})=>{X(r);let t=typeof e=="number",{currency:n,mode:o}=Ie(e,r);e=F(e);let s=t?g.POINT:r?.decimalSeparator,a=s===g.COMMA?",":".",m=e.includes(a),{integer:d,decimal:u}=_(e,s,o===y.DIGIT),w=/[1-9]/.test(d)||/[1-9]/.test(u),p=xe({integer:d,decimal:u,decimalSeparator:a,hasDecimalSeparator:m},o,n,r);return p=H(p,r.locale),e.startsWith(br)&&w&&(p=`menos ${p}`),r.removeAccents&&(p=Y(p)),p},xr=yr;return Se(wr);})();
extenso=extenso.default;
//# sourceMappingURL=extenso.umd.js.map