UNPKG

extenso

Version:

Uma biblioteca avançada para escrever números por extenso (em português).

5 lines (3 loc) 13.7 kB
/*! extenso v3.0.0 | MIT (c) 2015-2026 by Matheus Alves */ var R=(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))(R||{}),c=R;var D={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},$e=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),Ae=Object.keys(D).sort((e,r)=>r.length-e.length).map($e).join("|"),Le=Object.values(c).join("|"),G=new RegExp(`${Ae}|(?<![A-Za-z])(?:${Le})(?![A-Za-z])`,"g"),z=e=>Array.from(e.matchAll(G),r=>D[r[0]]??r[0]),P=e=>e.replace(G," ");var q=1e3,Ce=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)}`},Ne=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"?Ce(e.toString()):e.toString();if(r.length>q)throw new Error(`Input exceeds maximum length of ${q} characters`);if(/^[+-]?(?:NaN|Infinity)$/.test(r.trim()))throw new RangeError("Input number must be finite");let t=P(r).trim().replace(/^-\s+/,"-");if(/\s/.test(t))throw new Error("Invalid number format: whitespace is not allowed within the number");return t},U=Ne;var F=(t=>(t.COMMA="comma",t.POINT="point",t))(F||{}),g=F;var f=class extends Error{constructor(t,n){super(t);this.code=n;this.name="ParseError"}},Me=(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"}},B=Me;var _=(t=>(t.BR="br",t.PT="pt",t))(_||{}),x=_;var Se=(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}},V=Se;var H=(o=>(o.ABBREVIATED="abbreviated",o.CURRENCY="currency",o.DIGIT="digit",o.NUMBER="number",o))(H||{}),y=H;var j=(t=>(t.MALE="male",t.FEMALE="female",t))(j||{}),i=j;var k=(t=>(t.SHORT="short",t.LONG="long",t))(k||{}),l=k;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`)},Te=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)},ve=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)?Te(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)}`)},Z=ve;var Oe=e=>e.normalize("NFD").replace(new RegExp("\\p{Diacritic}","gu"),""),X=Oe;var Re={[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"]},De=(e,r)=>{let t=r.replace(/0+$/,"");return t===""?e:`${e},${t}`},Ge=(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},ze=(e,r="0",t=l.SHORT)=>{if(e.length<4)return De(e,r);let n=Math.floor((e.length-1)/3)-1,o=Ge(e,r,(n+1)*3);o===10000n&&(n+=1,o=10n);let s=Re[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}`},Y=ze;var Pe={[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}}},T=Pe;var qe=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=qe;var C="mil",Ue={[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=Ue;var Fe=["zero","um","dois","tr\xEAs","quatro","cinco","seis","sete","oito","nove"],W=["zero","uma","duas","tr\xEAs","quatro","cinco","seis","sete","oito","nove"],K=Fe;var Be=["dez","onze","doze","treze","quatorze","quinze","dezesseis","dezessete","dezoito","dezenove"],J=Be;var _e=["vinte","trinta","quarenta","cinquenta","sessenta","setenta","oitenta","noventa"],Q=_e;var Ve=["cento","duzentos","trezentos","quatrocentos","quinhentos","seiscentos","setecentos","oitocentos","novecentos"],ee=["cento","duzentas","trezentas","quatrocentas","quinhentas","seiscentas","setecentas","oitocentas","novecentas"],re=Ve;var I=(e,r=i.MALE)=>r===i.FEMALE?W[e]:K[e],je=(e,r=i.MALE)=>e<10?I(e,r):J[e-10],te=(e,r=i.MALE)=>{if(e<20)return je(e,r);let t=Q[(e-e%10)/10-2];return e%10===0?t:`${t} e ${I(e%10,r)}`},ke=(e,r=i.MALE)=>{if(e<100)return te(e,r);let n=(r===i.FEMALE?ee:re)[(e-e%100)/100-1];return e%100===0?e===100?"cem":n:`${n} e ${te(e%100,r)}`},M=ke;var ne=e=>e<100||e%100===0,Ze=(e,r)=>r===1?e===C?"":e:e.replace("\xE3o","\xF5es"),Xe=(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 ne(e)?`e ${d}`:d;if(s===void 0)throw new Error(`Number exceeds ${n} scale limit`);let u=Ze(s,e),w=`${u?`${d} `:""}${u||C}`,p=t.slice(r+1).some(S=>S>0),Ie=t.slice(0,r).every(S=>S===0);return p&&Ie&&ne(e)?`e ${w}`:w},Ye=e=>e.map((r,t)=>t<e.length-2?`${r},`:r),We=(e,r=l.SHORT,t=i.MALE)=>{let n=L(e).reverse(),o=n.map((s,a)=>Xe(s,a,n,r,t)).reverse().filter(Boolean);return Ye(o).join(" ")},oe=We;var Ke=(e,r=l.SHORT,t=i.MALE)=>{let n=e.replace(/^0+(?=\d)/,"");return n.length<4?M(Number(n),t):oe(n,r,t)},h=Ke;var ie=1000000n,se=(e,r,t=l.SHORT)=>{let n=h(e,t,r.gender),o=BigInt(e);return o===1n?`${n} ${r.singular}`:o>=ie&&o%ie===0n?`${n} de ${r.plural}`:`${n} ${r.plural}`},ae=(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}`},Je=(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(T).includes(t))throw new Error("Invalid currency");o=T[t]}else o=t;let s=BigInt(e)>0n,a=BigInt(r)>0n;return!s&&!a?`zero ${o.plural}`:s?a?`${se(e,o,n)} e ${ae(r,o)}`:se(e,o,n):ae(r,o)},ce=Je;var Qe=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}`},v=Qe;var er=(e,r)=>r===1?e:`${e}s`,b=er;var rr=["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"],le=rr;var tr=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=le[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)}`}},O=tr;var me=["","primeiro","segundo","terceiro","quarto","quinto","sexto","s\xE9timo","oitavo","nono"],ue=["d\xE9cimo","vig\xE9simo","trig\xE9simo","quadrag\xE9simo","quinquag\xE9simo","sexag\xE9simo","septuag\xE9simo","octog\xE9simo","nonag\xE9simo"],de=["cent\xE9simo","ducent\xE9simo","trecent\xE9simo","quadringent\xE9simo","quingent\xE9simo","sexcent\xE9simo","septingent\xE9simo","octingent\xE9simo","nongent\xE9simo"],pe=["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"],nr=pe.flatMap((e,r)=>r===0?[e]:[e,`mil\xE9simo ${e}`]).slice(0,N[l.LONG].length),or={[l.SHORT]:pe,[l.LONG]:nr},fe=or;var Ee=(e,r)=>r===i.MALE?e:e.replace(/o\b/g,"a"),ge=(e,r=i.MALE)=>{let t=[de[Math.floor(e/100)-1],ue[Math.floor(e%100/10)-1],me[e%10]].filter(n=>!!n);return Ee(t.join(" "),r)},ir=(e,r=l.SHORT,t=i.MALE)=>{if(BigInt(e)===0n)return t===i.FEMALE?"zer\xE9sima":"zer\xE9simo";let n=L(e),o=fe[r];return n.map((s,a)=>{if(s===0)return"";let m=n.length-a-2;if(m<0)return ge(s,t);let d=o[m];if(d===void 0)throw new Error(`Number exceeds ${r} scale limit`);let u=s===1?"":`${ge(s,t)} `;return Ee(`${u}${d}`,t)}).filter(Boolean).join(" ")},he=ir;var sr=(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 he(e,t,n)}if(e==="0"&&!s)return"zero";if(e==="0")return O(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 ${O(r)}`}return a},be=sr;var ar=(e,r,t,n)=>{let{integer:o,decimal:s,decimalSeparator:a,hasDecimalSeparator:m}=e;switch(r){case"abbreviated":return Y(o,s,n.scale);case"currency":if(s.length>2)throw new RangeError("Currency values must have zero, one, or two decimal places");return ce(o,s,t,n.scale);case"digit":return m?v(`${o}${a}${s}`):v(o);case"number":return be(o,s,n.scale,n.number?.gender,n.number?.ordinal)}},ye=ar;var cr=e=>{let r=[...new Set(z(e))];if(r.length>1)throw new Error(`Conflicting currency markers: ${r.join(", ")}`);return r[0]},xe=cr;var lr=e=>e!==void 0&&e.singular!==void 0,mr=(e,r)=>{let t=typeof e=="string"?xe(e):void 0,n=lr(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)}},we=mr;var ur="-",dr=(e,r={})=>{Z(r);let t=typeof e=="number",{currency:n,mode:o}=we(e,r);e=U(e);let s=t?g.POINT:r?.decimalSeparator,a=s===g.COMMA?",":".",m=e.includes(a),{integer:d,decimal:u}=B(e,s,o===y.DIGIT),w=/[1-9]/.test(d)||/[1-9]/.test(u),p=ye({integer:d,decimal:u,decimalSeparator:a,hasDecimalSeparator:m},o,n,r);return p=V(p,r.locale),e.startsWith(ur)&&w&&(p=`menos ${p}`),r.removeAccents&&(p=X(p)),p},on=dr;export{on as default}; //# sourceMappingURL=extenso.mjs.map