bb-inspired
Version:
Core library for BB-inspired NestJS backend
142 lines • 5.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BigNumberUtils = void 0;
const bignumber_js_1 = require("bignumber.js");
class BigNumberUtils {
static format(value, decimalPlaces = 2, roundingMode = bignumber_js_1.default.ROUND_HALF_UP, prefix = '', suffix = '') {
const bn = this.toBigNumber(value);
return `${prefix}${bn.toFormat(decimalPlaces, roundingMode)}${suffix}`;
}
static formatCurrency(value, currencySymbol = '$', decimalPlaces = 2) {
return this.format(value, decimalPlaces, bignumber_js_1.default.ROUND_HALF_UP, currencySymbol);
}
static formatPercent(value, decimalPlaces = 2) {
const bn = this.toBigNumber(value);
return this.format(bn.multipliedBy(100), decimalPlaces, bignumber_js_1.default.ROUND_HALF_UP, '', '%');
}
static toBigNumber(value) {
if (value instanceof bignumber_js_1.default) {
return value;
}
try {
return new bignumber_js_1.default(value);
}
catch (error) {
console.error('Failed to convert value to BigNumber:', value);
throw new Error(`Invalid number format: ${value}`);
}
}
static add(...values) {
if (values.length === 0)
return new bignumber_js_1.default(0);
let sum = new bignumber_js_1.default(0);
for (const value of values) {
const bn = this.toBigNumber(value);
sum = sum.plus(bn);
}
return sum;
}
static subtract(...values) {
if (values.length === 0)
return new bignumber_js_1.default(0);
if (values.length === 1)
return this.toBigNumber(values[0]);
let result = this.toBigNumber(values[0]);
for (let i = 1; i < values.length; i++) {
const bn = this.toBigNumber(values[i]);
result = result.minus(bn);
}
return result;
}
static multiply(...values) {
if (values.length === 0)
return new bignumber_js_1.default(1);
let product = new bignumber_js_1.default(1);
for (const value of values) {
const bn = this.toBigNumber(value);
product = product.multipliedBy(bn);
}
return product;
}
static divide(...values) {
if (values.length === 0)
return new bignumber_js_1.default(0);
if (values.length === 1)
return this.toBigNumber(values[0]);
let result = this.toBigNumber(values[0]);
for (let i = 1; i < values.length; i++) {
const bn = this.toBigNumber(values[i]);
if (bn.isZero()) {
throw new Error('Division by zero');
}
result = result.dividedBy(bn);
}
return result;
}
static power(base, exponent) {
const bn = this.toBigNumber(base);
return bn.pow(exponent);
}
static abs(value) {
const bn = this.toBigNumber(value);
return bn.abs();
}
static isEqual(a, b) {
const aBN = this.toBigNumber(a);
const bBN = this.toBigNumber(b);
return aBN.isEqualTo(bBN);
}
static isGreaterThan(a, b) {
const aBN = this.toBigNumber(a);
const bBN = this.toBigNumber(b);
return aBN.isGreaterThan(bBN);
}
static isLessThan(a, b) {
const aBN = this.toBigNumber(a);
const bBN = this.toBigNumber(b);
return aBN.isLessThan(bBN);
}
static max(...values) {
if (values.length === 0) {
throw new Error('Cannot determine maximum of empty list');
}
let max = this.toBigNumber(values[0]);
for (let i = 1; i < values.length; i++) {
const bn = this.toBigNumber(values[i]);
if (bn.isGreaterThan(max)) {
max = bn;
}
}
return max;
}
static min(...values) {
if (values.length === 0) {
throw new Error('Cannot determine minimum of empty list');
}
let min = this.toBigNumber(values[0]);
for (let i = 1; i < values.length; i++) {
const bn = this.toBigNumber(values[i]);
if (bn.isLessThan(min)) {
min = bn;
}
}
return min;
}
static average(...values) {
if (values.length === 0) {
return new bignumber_js_1.default(0);
}
const sum = this.add(...values);
return sum.dividedBy(values.length);
}
static round(value, decimalPlaces, roundingMode = bignumber_js_1.default.ROUND_HALF_UP) {
const bn = this.toBigNumber(value);
return bn.decimalPlaces(decimalPlaces, roundingMode);
}
static toFixed(value, decimalPlaces, roundingMode = bignumber_js_1.default.ROUND_HALF_UP) {
const bn = this.toBigNumber(value);
return bn.toFixed(decimalPlaces, roundingMode);
}
}
exports.BigNumberUtils = BigNumberUtils;
//# sourceMappingURL=bignumber.utils.js.map