From 7620b1ef47600cbcd21a489e68ca05dcfffa9be0 Mon Sep 17 00:00:00 2001 From: shan8851 Date: Mon, 24 Feb 2025 16:17:44 +0000 Subject: [PATCH] chore: remove hardcoded small value formatting and configure with options instead --- CHANGELOG.md | 2 ++ src/core/utils/formatterUtils/formatterUtils.ts | 13 +++++++------ .../formatterUtils/formatterUtilsDefinitions.ts | 10 ++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e86940a8..b5f1f830f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Changed - Update `NumberFormat.TOKEN_AMOUNT_SHORT` to round small numbers (between -0.01 and 0.01) to the nearest hundredth + - Add configurable options (`smallValueThreshold` and `smallValueFractionDigits`) to INumberFormat. + - Update `TOKEN_AMOUNT_SHORT` defaults in numberFormats. ## [1.0.68] - 2025-02-21 diff --git a/src/core/utils/formatterUtils/formatterUtils.ts b/src/core/utils/formatterUtils/formatterUtils.ts index e479698c3..018ec1b37 100644 --- a/src/core/utils/formatterUtils/formatterUtils.ts +++ b/src/core/utils/formatterUtils/formatterUtils.ts @@ -55,6 +55,8 @@ class FormatterUtils { withSign, fallback = null, displayFallback, + smallValueThreshold, + smallValueFractionDigits, } = mergedOptions; const parsedValue = typeof value === 'number' ? value : parseFloat(value ?? ''); @@ -65,15 +67,14 @@ class FormatterUtils { let processedValue = isPercentage ? parsedValue * 100 : parsedValue; - // Special case TOKEN_AMOUNT_SHORT and the value is between -0.01 and 0.01 - // We should round to the nearest hundredth meaning the only valid values are - // 0.00, 0.01, -0.01 and -0.00 - if (format === NumberFormat.TOKEN_AMOUNT_SHORT) { + const applySmallValueFormatting = smallValueThreshold != null && smallValueFractionDigits != null; + + if (applySmallValueFormatting) { if (parsedValue === 0) { return '0'; } - if (Math.abs(parsedValue) < 0.01) { - return parsedValue.toFixed(2); + if (Math.abs(parsedValue) < smallValueThreshold) { + return parsedValue.toFixed(smallValueFractionDigits); } } diff --git a/src/core/utils/formatterUtils/formatterUtilsDefinitions.ts b/src/core/utils/formatterUtils/formatterUtilsDefinitions.ts index eb5006ffd..a1c17cac4 100644 --- a/src/core/utils/formatterUtils/formatterUtilsDefinitions.ts +++ b/src/core/utils/formatterUtils/formatterUtilsDefinitions.ts @@ -47,6 +47,14 @@ export interface INumberFormat { * the fallback when the value is NaN. */ displayFallback?: (value: number) => boolean; + /** + * Threshold value to consider a number as a small value and be formatted differently. + */ + smallValueThreshold?: number; + /** + * Number of fraction digits to use when formatting small values. + */ + smallValueFractionDigits?: number; } export enum NumberFormat { @@ -81,6 +89,8 @@ export const numberFormats: Record = { [NumberFormat.TOKEN_AMOUNT_SHORT]: { maxFractionDigits: 2, useBaseSymbol: true, + smallValueThreshold: 0.01, + smallValueFractionDigits: 2, }, [NumberFormat.TOKEN_AMOUNT_LONG]: { maxFractionDigits: 18,