@swapkit/helpers 4.5.8 → 4.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +3 -3
- package/dist/index.js.map +3 -3
- package/dist/types/modules/bigIntArithmetics.d.ts +2 -1
- package/dist/types/modules/bigIntArithmetics.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/modules/__tests__/assetValue.test.ts +570 -10
- package/src/modules/__tests__/bigIntArithmetics.test.ts +14 -12
- package/src/modules/bigIntArithmetics.ts +20 -26
|
@@ -183,7 +183,8 @@ export class BigIntArithmetics {
|
|
|
183
183
|
const valueStr = this.getValue("string");
|
|
184
184
|
const isNegative = valueStr.startsWith("-");
|
|
185
185
|
const [int = "0", dec = ""] = (isNegative ? valueStr.slice(1) : valueStr).split(".");
|
|
186
|
-
const
|
|
186
|
+
const valueRoundsToZero = int === "0" && Number.parseInt(dec.slice(0, fixedDigits), 10) === 0;
|
|
187
|
+
const sign = isNegative && !valueRoundsToZero ? "-" : "";
|
|
187
188
|
|
|
188
189
|
if (fixedDigits === 0) {
|
|
189
190
|
if (int === "0" && !dec) return "0";
|
|
@@ -219,34 +220,27 @@ export class BigIntArithmetics {
|
|
|
219
220
|
|
|
220
221
|
toCurrency(
|
|
221
222
|
currency = "$",
|
|
222
|
-
{
|
|
223
|
+
{
|
|
224
|
+
currencyPosition = "start",
|
|
225
|
+
decimal = 2,
|
|
226
|
+
decimalSeparator = ".",
|
|
227
|
+
thousandSeparator = ",",
|
|
228
|
+
trimTrailingZeros = true,
|
|
229
|
+
} = {},
|
|
223
230
|
) {
|
|
224
|
-
const
|
|
225
|
-
const
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const trimmedDec = dec.replace(/0+$/, "");
|
|
232
|
-
const formatted = trimmedDec ? `${sign}0${decimalSeparator}${trimmedDec}` : "0";
|
|
233
|
-
|
|
234
|
-
return match(currencyPosition)
|
|
235
|
-
.with("start", () => `${currency}${formatted}`)
|
|
236
|
-
.with("end", () => `${formatted}${currency}`)
|
|
237
|
-
.otherwise(() => `${currency}${formatted}`);
|
|
238
|
-
}
|
|
231
|
+
const fixedValue = this.toFixed(decimal);
|
|
232
|
+
const isCurrencyAtEnd = currencyPosition === "end";
|
|
233
|
+
const [int = "0", dec = ""] = fixedValue.split(".");
|
|
234
|
+
|
|
235
|
+
const formattedInt = int.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
|
|
236
|
+
const hasDecimals = dec && Number.parseInt(dec, 10) > 0;
|
|
237
|
+
const formattedValue = hasDecimals ? `${formattedInt}${decimalSeparator}${dec}` : formattedInt;
|
|
239
238
|
|
|
240
|
-
|
|
241
|
-
const
|
|
242
|
-
const
|
|
243
|
-
const hasDecimals = roundedDec && Number.parseInt(roundedDec, 10) > 0;
|
|
244
|
-
const value = hasDecimals ? `${formattedInt}${decimalSeparator}${roundedDec}` : formattedInt;
|
|
239
|
+
// Prevent regex from taking too long for large values
|
|
240
|
+
const canTrimTrailingZeros = formattedValue.length < 100 && trimTrailingZeros && hasDecimals;
|
|
241
|
+
const value = canTrimTrailingZeros ? formattedValue.replace(/\.?0*$/, "") : formattedValue;
|
|
245
242
|
|
|
246
|
-
return
|
|
247
|
-
.with("start", () => `${currency}${value}`)
|
|
248
|
-
.with("end", () => `${value}${currency}`)
|
|
249
|
-
.otherwise(() => `${currency}${value}`);
|
|
243
|
+
return isCurrencyAtEnd ? `${value}${currency}` : `${currency}${value}`;
|
|
250
244
|
}
|
|
251
245
|
|
|
252
246
|
formatBigIntToSafeValue(value: bigint, decimal?: number) {
|