extenso
Version:
Uma biblioteca avançada para escrever números por extenso (em português).
2 lines • 14.6 kB
JavaScript
/*! Extenso.js v2.1.0 | MIT (c) 2015-2024 by Matheus Alves */
((e,t)=>{"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.extenso=t():e.extenso=t()})("undefined"!=typeof self?self:this,function(){return n=[function(e){e.exports=JSON.parse('{"BRL":{"singular":"real","plural":"reais","subunit":{"singular":"centavo","plural":"centavos"}},"CVE":{"singular":"escudo","plural":"escudos","subunit":{"singular":"cêntimo","plural":"cêntimos"}},"EUR":{"singular":"euro","plural":"euros","subunit":{"singular":"cêntimo","plural":"cêntimos"}},"MZN":{"singular":"metical","plural":"meticais","subunit":{"singular":"centavos","plural":"centavos"}}}')},function(e,t,n){function f(e){return(f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var r=Object.prototype.toString,s=n(4),p=e.exports=function(e){for(var t,n,r=0,o=arguments.length,i=new Array(1<o?o-1:0),a=1;a<o;a++)i[a-1]=arguments[a];for(e=(e=("object"===f(t=e)?null===t:"function"!=typeof t)?i[r++]:e)||{};r<i.length;r++)if(d(i[r])){for(var u=0,c=Object.keys(i[r]);u<c.length;u++){var l=c[u];"__proto__"!==(n=l)&&"constructor"!==n&&"prototype"!==n&&(d(e[l])&&d(i[r][l])?p(e[l],i[r][l]):e[l]=i[r][l])}s(e,i[r])}return e};function d(e){return"function"==typeof e||"[object Object]"===r.call(e)}},function(e,t){function n(c){if((c=c||{}).negativeType=c.negativeType||("R"===c.negative?"right":"left"),"string"!=typeof c.negativeLeftSymbol)switch(c.negativeType){case"left":c.negativeLeftSymbol="-";break;case"brackets":c.negativeLeftSymbol="(";break;default:c.negativeLeftSymbol=""}if("string"!=typeof c.negativeRightSymbol)switch(c.negativeType){case"right":c.negativeRightSymbol="-";break;case"brackets":c.negativeRightSymbol=")";break;default:c.negativeRightSymbol=""}function e(e,t){if(t=t||{},!e&&0!==e)return"";var n=[],r="-"===(e=""+e).charAt(0);if(e=e.replace(/^\-/g,""),c.negativeLeftOut||t.noUnits||n.push(c.prefix),r&&n.push(c.negativeLeftSymbol),c.negativeLeftOut&&!t.noUnits&&n.push(c.prefix),e=e.split("."),null!=c.round){var o=e,i=c.round;if(o[1]&&0<=i&&o[1].length>i){var a=o[1].slice(0,i);if(5<=+o[1].substr(i,1)){for(var u="";"0"===a.charAt(0);)u+="0",a=a.substr(1);(a=u+(a=+a+1+"")).length>i&&(o[0]=+o[0]+ +a.charAt(0)+"",a=a.substring(1))}o[1]=a}}return null!=c.truncate&&(e[1]=((e,t)=>(e&&(e+=""),e&&e.length>t?e.substr(0,t):e))(e[1],c.truncate)),0<c.padLeft&&(e[0]=((e,t)=>{e+="";for(var n=[];n.length+e.length<t;)n.push("0");return n.join("")+e})(e[0],c.padLeft)),0<c.padRight&&(e[1]=((e,t)=>{e?e+="":e="";for(var n=[];n.length+e.length<t;)n.push("0");return e+n.join("")})(e[1],c.padRight)),!t.noSeparator&&e[1]&&(e[1]=((e,t)=>{if(e+="",t)for(var n=/(\d{3})(\d+)/;n.test(e);)e=e.replace(n,"$1"+t+"$2");return e})(e[1],c.decimalsSeparator)),!t.noSeparator&&e[0]&&(e[0]=((e,t)=>{if(e+="",t)for(var n=/(\d+)(\d{3})/;n.test(e);)e=e.replace(n,"$1"+t+"$2");return e})(e[0],c.integerSeparator)),n.push(e[0]),e[1]&&(n.push(c.decimal),n.push(e[1])),c.negativeRightOut&&!t.noUnits&&n.push(c.suffix),r&&n.push(c.negativeRightSymbol),c.negativeRightOut||t.noUnits||n.push(c.suffix),n.join("")}function t(e,t){t=t||[],c.allowedSeparators&&c.allowedSeparators.forEach(function(e){t.push(e)}),t.push(c.integerSeparator),t.push(c.decimalsSeparator);var n=e=(e=e.replace(c.prefix,"")).replace(c.suffix,"");do{e=n;for(var r=0;r<t.length;r++)n=n.replace(t[r],"")}while(n!=e);return e}return"boolean"!=typeof c.negativeLeftOut&&(c.negativeLeftOut=!1!==c.negativeOut),"boolean"!=typeof c.negativeRightOut&&(c.negativeRightOut=!1!==c.negativeOut),c.prefix=c.prefix||"",c.suffix=c.suffix||"","string"!=typeof c.integerSeparator&&(c.integerSeparator="string"==typeof c.separator?c.separator:","),c.decimalsSeparator="string"==typeof c.decimalsSeparator?c.decimalsSeparator:"",c.decimal=c.decimal||".",c.padLeft=c.padLeft||-1,c.padRight=c.padRight||-1,e.negative=c.negative,e.negativeOut=c.negativeOut,e.negativeType=c.negativeType,e.negativeLeftOut=c.negativeLeftOut,e.negativeLeftSymbol=c.negativeLeftSymbol,e.negativeRightOut=c.negativeRightOut,e.negativeRightSymbol=c.negativeRightSymbol,e.prefix=c.prefix,e.suffix=c.suffix,e.separate=c.separate,e.integerSeparator=c.integerSeparator,e.decimalsSeparator=c.decimalsSeparator,e.decimal=c.decimal,e.padLeft=c.padLeft,e.padRight=c.padRight,e.truncate=c.truncate,e.round=c.round,e.unformat=t,e}e.exports=n,e.exports.default=n},function(e,t,n){e.exports=n(5).default},function(e,t,n){function s(e,t){var n,r,o,i,a="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(a)return o=!(r=!0),{s:function(){a=a.call(e)},n:function(){var e=a.next();return r=e.done,e},e:function(e){o=!0,n=e},f:function(){try{r||null==a.return||a.return()}finally{if(o)throw n}}};if(Array.isArray(e)||(a=((e,t)=>{var n;if(e)return"string"==typeof e?u(e,t):"Map"===(n="Object"===(n={}.toString.call(e).slice(8,-1))&&e.constructor?e.constructor.name:n)||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(e,t):void 0})(e))||t&&e&&"number"==typeof e.length)return a&&(e=a),i=0,{s:t=function(){},n:function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}},e:function(e){throw e},f:t};throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function u(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}var p=Object.prototype.toString,d=Object.prototype.propertyIsEnumerable,m=Object.getOwnPropertySymbols;e.exports=function(e){if("function"!=typeof(t=e)&&"[object Object]"!==p.call(t)&&!Array.isArray(t))throw new TypeError("expected the first argument to be an object");for(var t,n=arguments.length,r=new Array(1<n?n-1:0),o=1;o<n;o++)r[o-1]=arguments[o];if(0!==r.length&&"function"==typeof Symbol&&"function"==typeof m)for(var i=0,a=r;i<a.length;i++){var u,c=a[i],l=s(m(c));try{for(l.s();!(u=l.n()).done;){var f=u.value;d.call(c,f)&&(e[f]=c[f])}}catch(e){l.e(e)}finally{l.f()}}return e}},function(N,e,t){t.r(e),t.d(e,"isValidOpt",function(){return _}),t.d(e,"toNegative",function(){return M});var n=t(1),c=t.n(n);function l(e,t){return(e=>{if(Array.isArray(e))return e})(e)||((e,t)=>{var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,o,i,a,u=[],c=!0,l=!1;try{if(i=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;c=!1}else for(;!(c=(r=i.call(n)).done)&&(u.push(r.value),u.length!==t);c=!0);}catch(e){l=!0,o=e}finally{try{if(!c&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(l)throw o}}return u}})(e,t)||((e,t)=>{var n;if(e)return"string"==typeof e?r(e,t):"Map"===(n="Object"===(n={}.toString.call(e).slice(8,-1))&&e.constructor?e.constructor.name:n)||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(e,t):void 0})(e,t)||(()=>{throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")})()}function r(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function a(e,t,n){return A(w(R(z(I(L(O(e),t,n))),e)),t).join(" ")}function u(e){return e.replace(/\bum\b/,"uma").replace(/\bdois\b/,"duas")}function f(e){return/^0+$/.test(e)}function s(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:"0",r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:"0",o=4<arguments.length?arguments[4]:void 0;if(i=e,!E(d).includes(i))throw new Error("Invalid ISO code");1===r.length&&(r+="0");var i=d[e],e=((e,t,n,r)=>{var o=parseInt(e),e=$(e,t,"m",r);if(1===o)return"".concat(e," ").concat(n.singular);if(1e6<=o){t=o.toString(),r=parseInt(t.substr(-6,3)),o=parseInt(t.substr(-3,3));if(0===r&&0===o)return"".concat(e," de ").concat(n.plural)}return"".concat(e," ").concat(n.plural)})(n,t,i,o),o=((e,t,n)=>(t=$(e,t),1===parseInt(e)?"".concat(t," ").concat(n.subunit.singular):"".concat(t," ").concat(n.subunit.plural)))(r,t,i);return f(n)?o:f(r)?e:f(n)&&f(r)?"zero ".concat(i.plural):"".concat(e," e ").concat(o)}function p(e,t,n){return n&&"informal"===n?"vírgula ".concat($(e,t)):(n=t,e=(t=e).length,r=parseInt(t),t=t.replace(/^0+/,""),t=$(t,n),o=q(T(e),r),n=v(n)[Math.floor(e/3-1)],e<3?"".concat(t," ").concat(o):e%3==0?"".concat(t," ").concat(q(n,r)):"".concat(t," ").concat(o," de ").concat(n));var r,o}var d=t(0),o=function(e){return["zero","um","dois","três","quatro","cinco","seis","sete","oito","nove"]},i=function(e){return["dez","onze","doze","treze",{br:"quatorze",pt:"catorze"}[e],"quinze",{br:"dezesseis",pt:"dezasseis"}[e],{br:"dezessete",pt:"dezassete"}[e],"dezoito",{br:"dezenove",pt:"dezanove"}[e],"vinte","trinta","quarenta","cinquenta","sessenta","setenta","oitenta","noventa"]},m=function(e){return["cento","duzentos","trezentos","quatrocentos","quinhentos","seiscentos","setecentos","oitocentos","novecentos"]},g=function(n){var r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"short";return["mil","milhões","bil","tril","quatril","quintil","sextil","septil","octil","nonil","decil","undecil","duodecil"].map(function(e,t){return t<2?e:e+{br:"hões",pt:"iões"}[n]}).map(function(e,t,n){return"long"!==r||t<2?e:t%2==0?"mil "+n[t/2]:n[t/2+.5]})},v=function(e){return["milésimo","milionésimo","bilionésimo","trilionésimo","quatrilionésimo","quintilionésimo","sextilionésimo","septilionésimo","octilionésimo","nonilionésimo","decilionésimo","undecilionésimo","duodecilionésimo"]},b=function(e,t){return o(t)[e]},y=function(e,t){var n;return e<10?b(e,t):e<20?i(t)[e-10]:(n=b(e%10,t),t=i(t)[(e-e%10)/10+8],"zero"!==n?"".concat(t," e ").concat(n):t)},h=function(e,t){var n;return e<100?y(e,t):100===e?"cem":(n=e-e%100,e=e%100,n=m(t)[n/100-1],t=y(e,t),e?"".concat(n," e ").concat(t):n)},n=t(2),S=t.n(n),O=function(e){return S()()(e).split(",")},x=function(e){e=O(e),e=e[e.length-1];return parseInt(e)},j=function(e){if(null==e)return[];for(var t,n,r=0,o=e.length,i=o-1,a=o/2|0;r<a;r++)n=e[r],e[r]=e[t=i-r],e[t]=n;return e},w=function(e){return e.map(function(e,t,n){return t<n.length-2?"".concat(e,","):e})},R=function(e,t){t=x(t);return t<100||t%100==0?e.map(function(e,t,n){return t===n.length-2?"".concat(e," e"):e}):e},I=function(e){return e.map(function(e){return e.replace(/^0+\s?/,"")}).filter(function(e){return/^\d/.test(e)}).map(function(e){return e.replace(/^1\s(mil)$/,"$1")})},L=function(e,n,r){return j(j(e).map(function(e,t){t=g(n,r)[t-1];return t?"".concat(e," ").concat(t):e}))},z=function(e){var t=/^(1\s.*)ões/;return e.map(function(e){return e.replace(t,"$1ão")})},A=function(e,t){return e.map(function(e){return e.replace(/^(\d+)/,function(e){e=parseInt(e);return h(e,t)})})},$=function(e,t){var n,r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:"m",o=3<arguments.length&&void 0!==arguments[3]?arguments[3]:"short",i=parseInt(e);return i<1e3&&(n=h(i,t)),1e3===i&&(n="mil"),1e3<i&&(n=a(e,t,o)),"f"===r?u(n):n},E=function(e){return Object.keys(e)},T=function(e){switch(e%3){case 1:return"décimo";case 2:return"centésimo"}},q=function(e,t){return 1<t?"".concat(e,"s"):e},_=function(e,t){return t.includes(e)},M=function(e){return"informal"===(1<arguments.length&&void 0!==arguments[1]?arguments[1]:"formal")?"menos ".concat(e):"".concat(e," negativo")};e.default=function(e,t){if("string"!=typeof e&&"number"!=typeof e)throw new TypeError("Must be a string or a number");if(t=c()({mode:"number",locale:"br",negative:"formal",scale:"short",currency:{type:"BRL"},number:{gender:"m",decimal:"formal",decimalSeparator:"comma"}},t),!(_(t.mode,["number","currency"])&&_(t.locale,["pt","br"])&&_(t.negative,["formal","informal"])&&_(t.scale,["short","long"])&&_(t.currency.type,["BRL","EUR","CVE"])&&_(t.number.gender,["m","f"])&&_(t.number.decimal,["formal","informal"])&&_(t.number.decimalSeparator,["comma","dot"])))throw new Error("Invalid option");var n,r,o,i,a,u="dot"===t.number.decimalSeparator||"number"==typeof e;if(function(e){var t=1<arguments.length&&void 0!==arguments[1]&&arguments[1];if("number"==typeof e){if(Number.isInteger(e)&&!Number.isSafeInteger(e))return!1;if(!Number.isInteger(e))return!0}var n=/^-?\d+$/.test(e),r=/^-?\d{1,3}\d?((\.\d{3})+)?$/.test(e),o=/^-?\d{1,3}\d?((\.\d{3})+)?,\d+$/.test(e),i=/^-?\d+,\d+$/.test(e),a=/^-?\d{1,3}\d?((,\d{3})+)?$/.test(e),u=/^-?\d{1,3}\d?((,\d{3})+)?\.\d+$/.test(e),e=/^-?\d+\.\d+$/.test(e);return t?n||a||u||e:n||r||o||i}(e,u))return u=(e=function(e){var t=1<arguments.length&&void 0!==arguments[1]&&arguments[1],n=("number"==typeof e&&(e=e.toString(),t=!0),t?",":"."),t=t?".":",",r=/^-/.test(e),e=e.replace(RegExp("(-|\\".concat(n,")"),"g"),"");return e.includes(t)?{isNegative:r,integer:(n=l(e.split(t).map(function(e){return e.replace(/^0+$/,"0")}),2))[0],decimal:n[1]}:{isNegative:r,integer:e,decimal:"0"}}(e,u)).isNegative,n=e.integer,e=e.decimal,"currency"===t.mode?(o=t.currency.type,i=t.locale,r=e.slice(0,2),o=s(o,i,n,r,t.scale),u?M(o,t.negative):o):"number"===t.mode?(i="f"===t.number.gender?"inteira":"inteiro",r=1===parseInt(n)?i:"".concat(i,"s"),o=$(n,t.locale,t.number.gender,t.scale),i=p(e,t.locale,t.number.decimal),"0"===n&&"0"===e?o:"0"!==n&&"0"===e?u?M(o,t.negative):o:"0"===n&&"0"!==e?(a="informal"===t.number.decimal?"zero ".concat(i):i,u?M(a,t.negative):a):"0"!==n&&"0"!==e?"informal"===t.number.decimal?"".concat(o," ").concat(i):(a="".concat(o," ").concat(r," e ").concat(i),u?M(a,t.negative):a):void 0):void 0;throw new Error("Invalid number")}}],r={},o.m=n,o.c=r,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)o.d(n,r,function(e){return t[e]}.bind(null,r));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="",o(o.s=3);function o(e){var t;return(r[e]||(t=r[e]={i:e,l:!1,exports:{}},n[e].call(t.exports,t,t.exports,o),t.l=!0,t)).exports}var n,r});