UNPKG

spell-vn-number

Version:
1 lines 8.57 kB
!function(e,r){"object"===typeof exports&&"object"===typeof module?module.exports=r():"function"===typeof define&&define.amd?define("spellVnNumber",[],r):"object"===typeof exports?exports.spellVnNumber=r():e.spellVnNumber=r()}(this,(function(){return function(){"use strict";var e={185:function(e,r,t){Object.defineProperty(r,"__esModule",{value:!0}),r.cleanInputNumber=function(e,r){if(null===e||void 0===e||""===e)throw new n.InvalidFormatError("Input cannot be null or undefined");var t;if("bigint"===typeof e)return e.toString();if("number"===typeof e){if(!Number.isFinite(e)||Number.isNaN(e))throw new n.InvalidFormatError("Input must be a finite number");t=e.toString(),/e/i.test(t)&&(t=function(e){var r=e.toString();if(!r.includes("e")&&!r.includes("E"))return r;var t=r.replace(/E/i,"e").split("e"),n=t[0],i=parseInt(t[1],10);if(0===i)return n;var o=n.includes("."),a=o?n.replace(".",""):n,u=o?n.indexOf("."):n.length,l="";l=(u+=i)<=0?"0."+"0".repeat(-u)+a:u>=a.length?a+"0".repeat(u-a.length):a.substring(0,u)+"."+a.substring(u);n.startsWith("-")&&(l="-"+l.replace("-",""));return l}(e))}else t=i(e,r);var a=o(r.decimalPoint||"."),u=o(r.thousandSign||","),l=new RegExp("^-?\\d+("+a+"\\d+)?$"),s=t.replace(new RegExp(u,"g"),"");if(!l.test(s))throw new n.InvalidNumberError("Invalid number format");if(/e/i.test(s))throw new n.InvalidNumberError("Invalid number format");return s},r.normalizeNumberString=i,r.trimLeft=a,r.trimRight=u,r.handleRedundantZeros=function(e,r){var t,n=null!==(t=e.keepOneZeroWhenAllZeros)&&void 0!==t&&t;if(r.includes(e.decimalPoint)){var i=r.split(e.decimalPoint),o=a(i[0],e.filledDigit),l=u(i[1],e.filledDigit);return""===o&&(o="0"),""===l?n&&i[1].length>0?"".concat(o).concat(e.decimalPoint,"0"):"".concat(o).concat(e.decimalPoint):"".concat(o).concat(e.decimalPoint).concat(l)}var s=a(r,e.filledDigit);return""===s?"0":s};var n=t(613);function i(e,r){void 0===r&&(r={decimalPoint:".",thousandSign:","});var t=r.thousandSign||",",n=e.trim().replace(/[\u2013\u2014]/g,"-").replace(/[\s\u00A0]/g,"");return n=n.replace(new RegExp(o(t),"g"),"")}function o(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function a(e,r){void 0===r&&(r="0");for(var t=0;t<e.length&&e.charAt(t)===r;)t++;return t===e.length?"":e.substring(t)}function u(e,r){void 0===r&&(r="0");for(var t=e.length-1;t>=0&&e.charAt(t)===r;)t--;return t<0?"":e.substring(0,t+1)}},613:function(e,r){var t=this&&this.__extends||function(){var e=function(r,t){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])},e(r,t)};return function(r,t){if("function"!==typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=r}e(r,t),r.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}}();Object.defineProperty(r,"__esModule",{value:!0}),r.SpellerConfig=r.InvalidNumberError=r.InvalidFormatError=void 0;var n=function(e){function r(t){var n=e.call(this,t)||this;return n.name="InvalidFormatError",Object.setPrototypeOf(n,r.prototype),n}return t(r,e),r}(Error);r.InvalidFormatError=n;var i=function(e){function r(t){var n=e.call(this,t)||this;return n.name="InvalidNumberError",Object.setPrototypeOf(n,r.prototype),n}return t(r,e),r}(Error);r.InvalidNumberError=i;var o=function(e){for(var r in void 0===e&&(e={}),this.separator=" ",this.negativeSign="-",this.decimalPoint=".",this.thousandSign=",",this.filledDigit="0",this.capitalizeInitial=!0,this.currencyUnit="",this.keepOneZeroWhenAllZeros=!1,this.negativeText="\xe2m",this.pointText="ch\u1ea5m",this.oddText="l\u1ebb",this.tenText="m\u01b0\u1eddi",this.oneToneText="m\u1ed1t",this.fourToneText="t\u01b0",this.fiveToneText="l\u0103m",this.digits={0:"kh\xf4ng",1:"m\u1ed9t",2:"hai",3:"ba",4:"b\u1ed1n",5:"n\u0103m",6:"s\xe1u",7:"b\u1ea3y",8:"t\xe1m",9:"ch\xedn"},this.UNIT_GROUP=["BILLION","MINION","THOUSAND"],this.UNIT_OF_GROUP={BILLION:"t\u1ef7",MINION:"tri\u1ec7u",THOUSAND:"ngh\xecn"},this.UNIT_GROUP_MAPPER={BILLION:["tr\u0103m","m\u01b0\u01a1i","tri\u1ec7u"],MINION:["tr\u0103m","m\u01b0\u01a1i","ngh\xecn"],THOUSAND:["tr\u0103m","m\u01b0\u01a1i",""]},this.UNIT_EACH_GROUP=["HUNDRED","TENS","UNITS"],this.AT_HUNDRED=0,this.AT_TEN=1,this.AT_UNIT=2,e)Object.prototype.hasOwnProperty.call(e,r)&&(this[r]=e[r])};r.SpellerConfig=o},961:function(e,r,t){Object.defineProperty(r,"__esModule",{value:!0}),r.spellVnNumber=s,r.spell=function(e){return s(new n.SpellerConfig,e)},r.spellOrDefault=function(e,r,t){void 0===r&&(r={});try{return s(new n.SpellerConfig(r),e)}catch(i){if(void 0===t)throw i;return i instanceof n.InvalidFormatError?console.warn("\u0110\u1ecbnh d\u1ea1ng input kh\xf4ng h\u1ee3p l\u1ec7"):i instanceof n.InvalidNumberError?console.warn("S\u1ed1 kh\xf4ng h\u1ee3p l\u1ec7"):console.error(i),t}};var n=t(613),i=t(185);function o(e,r){if(""===r)return[];var t=r.length%e.UNIT_EACH_GROUP.length,n=r;if(0!==t){var i=e.UNIT_EACH_GROUP.length-t;n="0".repeat(i)+r}for(var o=n.split(""),l=[],s=o.length/e.UNIT_EACH_GROUP.length,c=e.UNIT_GROUP.length,f=s%c,p=0===f?f:c-f,h=!0,d=0,g=0===f?s/c:Math.floor(s/c)+1;g>0;){if(!h){var v=e.UNIT_OF_GROUP[e.UNIT_GROUP[p]];l.push(v)}for(;p<e.UNIT_GROUP.length;p++){var m=[o[d],o[d+1],o[d+2]];if(d+=3,h){h=!1;for(var b=a(e,m,p),N=0;N<b.length;N++)l.push(b[N])}else{var T=u(e,m,p);for(N=0;N<T.length;N++)l.push(T[N])}}g--,p=0}return l}function a(e,r,t){for(var n=[],i=!0,o=0;o<r.length;o++)if(i&&"0"!==r[o]&&(i=!1),!i)for(var a=l(e,r,o,t),u=0;u<a.length;u++)n.push(a[u]);return 0===n.length&&n.push(e.digits[0]),n}function u(e,r,t){for(var n=[],i=0;i<r.length;i++)for(var o=l(e,r,i,t),a=0;a<o.length;a++)n.push(o[a]);return n}function l(e,r,t,n){var i=r[t],o=[];switch(i){case"0":t===e.AT_UNIT||(t===e.AT_TEN?"0"!==r[t+1]&&o.push(e.oddText):"0"===r[t+1]&&"0"===r[t+2]||o.push(e.digits[i]));break;case"1":if(t===e.AT_UNIT){var a=r[t-1];"0"!==a&&"1"!==a?o.push(e.oneToneText):o.push(e.digits[i])}else t===e.AT_TEN?o.push(e.tenText):o.push(e.digits[i]);break;case"4":t===e.AT_UNIT&&"0"!==r[t-1]&&"1"!==r[t-1]?o.push(e.fourToneText):o.push(e.digits[i]);break;case"5":t===e.AT_UNIT&&"0"!==r[t-1]?o.push(e.fiveToneText):o.push(e.digits[i]);break;default:o.push(e.digits[i])}var u=e.UNIT_GROUP[n];if(o.length>0)"1"===i&&t===e.AT_TEN||o[0]===e.oddText&&t===e.AT_TEN||""!==(l=e.UNIT_GROUP_MAPPER[u][t])&&o.push(l);else if(t===e.AT_UNIT&&"0"===i&&("0"!==r[t-1]||"0"!==r[t-2])){var l;""!==(l=e.UNIT_GROUP_MAPPER[u][t])&&o.push(l)}return o}function s(e,r){var t=function(e,r){var t=(0,i.cleanInputNumber)(r,e),n=t.startsWith(e.negativeSign);t=n?t.substring(e.negativeSign.length):t;var o=(t=(0,i.handleRedundantZeros)(e,t)).indexOf(e.decimalPoint);return{isNegative:n,integralPart:-1===o?t:t.substring(0,o),fractionalPart:-1===o?"":t.substring(o+1)}}(e,r),n=[];t.isNegative&&n.push(e.negativeText);for(var a=o(e,t.integralPart),u=0;u<a.length;u++)n.push(a[u]);if(t.fractionalPart.length>0){n.push(e.pointText);var l=o(e,t.fractionalPart);for(u=0;u<l.length;u++)n.push(l[u])}var s=n.join(e.separator);return e.capitalizeInitial&&(s=s.charAt(0).toUpperCase()+s.slice(1)),e.currencyUnit&&(s+=" ".concat(e.currencyUnit)),s}}},r={};function t(n){var i=r[n];if(void 0!==i)return i.exports;var o=r[n]={exports:{}};return e[n].call(o.exports,o,o.exports,t),o.exports}var n={};return function(){var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.InvalidNumberError=e.InvalidFormatError=e.SpellerConfig=e.normalizeNumberString=e.handleRedundantZeros=e.cleanInputNumber=e.spellOrDefault=e.spellVnNumber=e.spell=void 0;var r=t(961);Object.defineProperty(e,"spell",{enumerable:!0,get:function(){return r.spell}}),Object.defineProperty(e,"spellVnNumber",{enumerable:!0,get:function(){return r.spellVnNumber}}),Object.defineProperty(e,"spellOrDefault",{enumerable:!0,get:function(){return r.spellOrDefault}});var i=t(185);Object.defineProperty(e,"cleanInputNumber",{enumerable:!0,get:function(){return i.cleanInputNumber}}),Object.defineProperty(e,"handleRedundantZeros",{enumerable:!0,get:function(){return i.handleRedundantZeros}}),Object.defineProperty(e,"normalizeNumberString",{enumerable:!0,get:function(){return i.normalizeNumberString}});var o=t(613);Object.defineProperty(e,"SpellerConfig",{enumerable:!0,get:function(){return o.SpellerConfig}}),Object.defineProperty(e,"InvalidFormatError",{enumerable:!0,get:function(){return o.InvalidFormatError}}),Object.defineProperty(e,"InvalidNumberError",{enumerable:!0,get:function(){return o.InvalidNumberError}})}(),n}()}));