UNPKG

price-like-humans

Version:

JS tools for formatting price or numbers to human likes format.

2 lines (1 loc) 2.1 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var reverser=function(e){return e.toString().split("").reverse().join("")},getLang=function(){var e="en";"undefined"!=typeof navigator&&void 0!==navigator.language&&(e=navigator.language),"undefined"!=typeof process&&void 0!==process.env&&(e=process.env.LANG||process.env.LC_CTYPE||e);return e.split(/[-_]/)[0]},getLocale=function(e){function r(e){return t.format(e).replace(/\d/g,"").split("")[0]}var t=new Intl.NumberFormat(e||getLang());return{lang:e||getLang(),separator:r(1e4),delimiter:r(1.1)}},_formattedPrice=function(e,r){var t,n,o,i,a,l,u;if(isNaN(Number(e.toString().split("")[0])))return!1;if(void 0!==(null===(t=r)||void 0===t?void 0:t.delimiter)&&(null===(n=r)||void 0===n?void 0:n.delimiter)===(null===(o=r)||void 0===o?void 0:o.separator))return console.error("The delimiter can`t be the same as the separator"),!1;var d=(null===(i=r)||void 0===i?void 0:i.delimiter)||getLocale(null===(a=r)||void 0===a?void 0:a.lang).delimiter,s=(null===(l=r)||void 0===l?void 0:l.separator)||getLocale(null===(u=r)||void 0===u?void 0:u.lang).separator;d===s&&("."===s&&(d=","),"."===s&&(d=","),","!==s&&" "!==s||(d="."));var v=1===reverser(e).replace(/\s|\d/g,"").length?reverser(e).replace(/\s|\d/g,"")[0]:d,p=d||v,c=e.toString().split("number"==typeof e?".":v),m=/\B(?=(\d{3})+(?!\d))/g,g=c[0].replace(m,s);if(1<c.length){var f=reverser(c[1]).replace(m,s);return[g,reverser(f)].join(p)}return g},_exponentFormatter=function(e){if(e<1e-6){var r=9<Number(e.toString().split("-"))?9:Number(e.toString().split("-")[1]);return new Intl.NumberFormat(getLocale().lang,{style:"decimal",minimumFractionDigits:r,useGrouping:!1}).format(Number(e)).replace(",",".")}return e.toString()};function formattedPrice(e,r){return _formattedPrice(e,r)}function exponentFormatter(e){return _exponentFormatter(e)}var index={formattedPrice:function(e,r){return _formattedPrice(e,r)},exponentFormatter:function(e){return _exponentFormatter(e)}};exports.default=index,exports.exponentFormatter=exponentFormatter,exports.formattedPrice=formattedPrice;