@alexkeating/common-utilities
Version:
Our **Common Utilities** package is a set of helper tools and utilities that are used throughout our libraries and apps. This includes things like our constants, types that are commonly shared across packages, and various utilities and helper functions.
97 lines • 4.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.readableNumbers = exports.formatValueTo = exports.toWholeUnits = exports.toBaseUnits = void 0;
const ethers_1 = require("ethers");
const numbro_1 = require("numbro");
const typeguards_1 = require("./typeguards");
const toBaseUnits = (amount, decimals = 18) => ethers_1.utils.parseUnits(amount, decimals).toString();
exports.toBaseUnits = toBaseUnits;
const toWholeUnits = (amount, decimals = 18) => ethers_1.utils.formatUnits(amount, decimals).toString();
exports.toWholeUnits = toWholeUnits;
const generateNumeral = ({ value, type, decimals, }) => {
// cheap zeroFormatter
if (value === 0)
return '0';
// short formats shrink large numbers to the shorter representation
// e.g. 1.000 -> 1k, 1.000.000 -> 1m, etc
const short = type.match(/(short)/i) ? 'a' : '';
const decimalCount = decimals ? '0'.repeat(decimals) : '';
// build the correct numbro.js format OR pass a one off custom format on.
//
// custom format: create your own format and it will bypass straight to numbro.js
// eg. readableNumbers.toCustomFormat({value: 10, unit: 'Place', format: '0o '}) => '10th Place'
let format;
if (type.match(/(exponent)/i)) {
format = `0,0[.]${decimalCount}e`;
return (0, numbro_1.default)(value).format(format);
}
else if (type.match(/(percent)/i)) {
const decimalMatcher = decimals > 0 ? `[.]${decimalCount}` : '';
format = `0${decimalMatcher}%`;
return (0, numbro_1.default)(value / 100).format(format);
}
else if (type.match(/(currency)/i)) {
format = `$0[.]${decimalCount}${short}`;
return (0, numbro_1.default)(value).format(format);
}
else if (type.match(/(number)/)) {
format = `0,0[.]${decimalCount}${short}`;
return (0, numbro_1.default)(value).format(format);
}
else {
return (0, numbro_1.default)(value).format(type);
}
};
const formatValueTo = ({ value, unit = '', decimals = 4, separator = ' ', format, }) => {
if (typeof value === 'string' && (0, typeguards_1.isNumberish)(value)) {
value = Number(value);
}
if (typeof value === 'string' && !(0, typeguards_1.isNumberish)(value)) {
throw new Error(`${value} is not a number`);
}
if (value > 0 && value < 1) {
return unit
? `${value.toFixed(decimals)}${separator}${unit}`
: value.toFixed(decimals);
}
if (!format) {
throw new Error(`must define a format if ${value} is not between 0 and 1`);
}
const formatted = generateNumeral({
value,
type: format,
decimals,
});
return `${formatted}${separator}${unit}`;
};
exports.formatValueTo = formatValueTo;
exports.readableNumbers = {
toCurrency: (args) => {
return (0, exports.formatValueTo)(Object.assign(Object.assign({}, args), { format: 'currency' }));
},
toCurrencyShort: (args) => {
return (0, exports.formatValueTo)(Object.assign(Object.assign({}, args), { format: 'currencyShort' }));
},
toDollars: (args) => {
return (0, exports.formatValueTo)(Object.assign(Object.assign({}, args), { decimals: 2, format: 'currency' }));
},
toNumber: (args) => {
return (0, exports.formatValueTo)(Object.assign(Object.assign({}, args), { format: 'number' }));
},
toNumberShort: (args) => {
return (0, exports.formatValueTo)(Object.assign(Object.assign({}, args), { format: 'numberShort' }));
},
toPercent: (args) => {
return (0, exports.formatValueTo)(Object.assign(Object.assign({}, args), { decimals: 0, format: 'percent' }));
},
toPercentDecimals: (args) => {
return (0, exports.formatValueTo)(Object.assign(Object.assign({}, args), { decimals: 2, format: 'percentShort' }));
},
toExponential: (args) => {
return (0, exports.formatValueTo)(Object.assign(Object.assign({}, args), { format: 'exponential' }));
},
toCustomFormat: (args) => {
return (0, exports.formatValueTo)(args);
},
};
//# sourceMappingURL=units.js.map