@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.
@@ -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 sign = isNegative ? "-" : "";
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
- { currencyPosition = "start", decimal = 2, decimalSeparator = ".", thousandSeparator = "," } = {},
223
+ {
224
+ currencyPosition = "start",
225
+ decimal = 2,
226
+ decimalSeparator = ".",
227
+ thousandSeparator = ",",
228
+ trimTrailingZeros = true,
229
+ } = {},
223
230
  ) {
224
- const valueStr = this.getValue("string");
225
- const isNegative = valueStr.startsWith("-");
226
- const absValueStr = isNegative ? valueStr.slice(1) : valueStr;
227
- const [int = "0", dec = ""] = absValueStr.split(".");
228
- const sign = isNegative ? "-" : "";
229
-
230
- if (int === "0" && dec) {
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
- const roundedStr = this.toFixed(decimal);
241
- const [roundedInt = "0", roundedDec = ""] = roundedStr.split(".");
242
- const formattedInt = roundedInt.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
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 match(currencyPosition)
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) {