@etsoo/shared
Version:
TypeScript shared utilities and functions
112 lines (111 loc) • 3.75 kB
JavaScript
Number.prototype.toExact = function (precision) {
if (precision == null || precision < 0)
precision = 2;
if (precision === 0)
return Math.round(this);
const p = Math.pow(10, precision);
return Math.round(this * p) / p;
};
Number.prototype.toStep = function (step) {
if (step <= 0)
return this;
return Math.floor(this / step) * step;
};
export var NumberUtils;
(function (NumberUtils) {
/**
* Format number
* @param input Input
* @param locale Locale
* @param options Options
* @returns Result
*/
function format(input, locale, options) {
// Formatter
const intl = new Intl.NumberFormat(locale, options);
return intl.format(input);
}
NumberUtils.format = format;
/**
* Format money
* @param input Input
* @param currency Currency, like USD for US dollar
* @param locale Locale
* @param isInteger Is integer value
* @param options Options
* @returns Result
*/
function formatMoney(input, currency, locale, isInteger = false, options = {}) {
if (currency) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat
options.style = "currency";
options.currency = currency;
options.currencyDisplay ?? (options.currencyDisplay = "narrowSymbol");
}
if (isInteger) {
options.minimumFractionDigits ?? (options.minimumFractionDigits = 0);
options.maximumFractionDigits ?? (options.maximumFractionDigits = 0);
}
else
options.minimumFractionDigits ?? (options.minimumFractionDigits = 2);
return format(input, locale, options);
}
NumberUtils.formatMoney = formatMoney;
/**
* Format file size
* @param size File size
* @param fractionDigits Fraction digits
* @returns Result
*/
function formatFileSize(size, fractionDigits = 2) {
const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
return ((size / Math.pow(1024, i)).toFixed(fractionDigits) +
" " +
["B", "KB", "MB", "GB", "TB", "PB"][i]);
}
NumberUtils.formatFileSize = formatFileSize;
/**
* Get currency symbol or name from ISO code
* @param code ISO currency code, like USD / CNY
* @param display Display format
* @param locale Locale
* @returns Result
*/
function getCurrencySymbol(code, display = "narrowSymbol", locale) {
const formatter = new Intl.NumberFormat(locale, {
style: "currency",
currency: code,
currencyDisplay: display
});
const parts = formatter.formatToParts();
const symbol = parts.find((part) => part.type === "currency")?.value;
return symbol;
}
NumberUtils.getCurrencySymbol = getCurrencySymbol;
function parse(rawData, defaultValue) {
if (rawData == null || rawData === "") {
return defaultValue;
}
if (typeof rawData === "number") {
if (isNaN(rawData))
return defaultValue;
return rawData;
}
const p = parseFloat(rawData.toString());
if (isNaN(p))
return defaultValue;
return p;
}
NumberUtils.parse = parse;
/**
* Parse float value and unit
* @param input Input string
* @returns Result, like [number, string]
*/
NumberUtils.parseWithUnit = (input) => {
if (/^(\d+)(\D*)$/g.test(input)) {
return [Number(RegExp.$1), RegExp.$2.trim()];
}
return undefined;
};
})(NumberUtils || (NumberUtils = {}));