@metamask/assets-controllers 74.3.3 → 75.1.0

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.
Files changed (100) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/AccountTrackerController.cjs +43 -10
  3. package/dist/AccountTrackerController.cjs.map +1 -1
  4. package/dist/AccountTrackerController.d.cts +8 -5
  5. package/dist/AccountTrackerController.d.cts.map +1 -1
  6. package/dist/AccountTrackerController.d.mts +8 -5
  7. package/dist/AccountTrackerController.d.mts.map +1 -1
  8. package/dist/AccountTrackerController.mjs +43 -10
  9. package/dist/AccountTrackerController.mjs.map +1 -1
  10. package/dist/CurrencyRateController.cjs +12 -2
  11. package/dist/CurrencyRateController.cjs.map +1 -1
  12. package/dist/CurrencyRateController.d.cts.map +1 -1
  13. package/dist/CurrencyRateController.d.mts.map +1 -1
  14. package/dist/CurrencyRateController.mjs +12 -2
  15. package/dist/CurrencyRateController.mjs.map +1 -1
  16. package/dist/DeFiPositionsController/DeFiPositionsController.cjs +4 -0
  17. package/dist/DeFiPositionsController/DeFiPositionsController.cjs.map +1 -1
  18. package/dist/DeFiPositionsController/DeFiPositionsController.d.cts.map +1 -1
  19. package/dist/DeFiPositionsController/DeFiPositionsController.d.mts.map +1 -1
  20. package/dist/DeFiPositionsController/DeFiPositionsController.mjs +4 -0
  21. package/dist/DeFiPositionsController/DeFiPositionsController.mjs.map +1 -1
  22. package/dist/MultichainAssetsController/MultichainAssetsController.cjs +4 -0
  23. package/dist/MultichainAssetsController/MultichainAssetsController.cjs.map +1 -1
  24. package/dist/MultichainAssetsController/MultichainAssetsController.d.cts.map +1 -1
  25. package/dist/MultichainAssetsController/MultichainAssetsController.d.mts.map +1 -1
  26. package/dist/MultichainAssetsController/MultichainAssetsController.mjs +4 -0
  27. package/dist/MultichainAssetsController/MultichainAssetsController.mjs.map +1 -1
  28. package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.cjs +12 -2
  29. package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.cjs.map +1 -1
  30. package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.cts.map +1 -1
  31. package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.mts.map +1 -1
  32. package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.mjs +12 -2
  33. package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.mjs.map +1 -1
  34. package/dist/MultichainBalancesController/MultichainBalancesController.cjs +2 -0
  35. package/dist/MultichainBalancesController/MultichainBalancesController.cjs.map +1 -1
  36. package/dist/MultichainBalancesController/MultichainBalancesController.d.cts.map +1 -1
  37. package/dist/MultichainBalancesController/MultichainBalancesController.d.mts.map +1 -1
  38. package/dist/MultichainBalancesController/MultichainBalancesController.mjs +2 -0
  39. package/dist/MultichainBalancesController/MultichainBalancesController.mjs.map +1 -1
  40. package/dist/NftController.cjs +18 -3
  41. package/dist/NftController.cjs.map +1 -1
  42. package/dist/NftController.d.cts.map +1 -1
  43. package/dist/NftController.d.mts.map +1 -1
  44. package/dist/NftController.mjs +18 -3
  45. package/dist/NftController.mjs.map +1 -1
  46. package/dist/RatesController/RatesController.cjs +18 -3
  47. package/dist/RatesController/RatesController.cjs.map +1 -1
  48. package/dist/RatesController/RatesController.d.cts.map +1 -1
  49. package/dist/RatesController/RatesController.d.mts.map +1 -1
  50. package/dist/RatesController/RatesController.mjs +18 -3
  51. package/dist/RatesController/RatesController.mjs.map +1 -1
  52. package/dist/TokenBalancesController.cjs +35 -10
  53. package/dist/TokenBalancesController.cjs.map +1 -1
  54. package/dist/TokenBalancesController.d.cts +7 -5
  55. package/dist/TokenBalancesController.d.cts.map +1 -1
  56. package/dist/TokenBalancesController.d.mts +7 -5
  57. package/dist/TokenBalancesController.d.mts.map +1 -1
  58. package/dist/TokenBalancesController.mjs +35 -10
  59. package/dist/TokenBalancesController.mjs.map +1 -1
  60. package/dist/TokenListController.cjs +12 -2
  61. package/dist/TokenListController.cjs.map +1 -1
  62. package/dist/TokenListController.d.cts.map +1 -1
  63. package/dist/TokenListController.d.mts.map +1 -1
  64. package/dist/TokenListController.mjs +12 -2
  65. package/dist/TokenListController.mjs.map +1 -1
  66. package/dist/TokenRatesController.cjs +6 -1
  67. package/dist/TokenRatesController.cjs.map +1 -1
  68. package/dist/TokenRatesController.d.cts.map +1 -1
  69. package/dist/TokenRatesController.d.mts.map +1 -1
  70. package/dist/TokenRatesController.mjs +6 -1
  71. package/dist/TokenRatesController.mjs.map +1 -1
  72. package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.cjs +12 -2
  73. package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.cjs.map +1 -1
  74. package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.d.cts.map +1 -1
  75. package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.d.mts.map +1 -1
  76. package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.mjs +12 -2
  77. package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.mjs.map +1 -1
  78. package/dist/TokensController.cjs +6 -0
  79. package/dist/TokensController.cjs.map +1 -1
  80. package/dist/TokensController.d.cts.map +1 -1
  81. package/dist/TokensController.d.mts.map +1 -1
  82. package/dist/TokensController.mjs +6 -0
  83. package/dist/TokensController.mjs.map +1 -1
  84. package/dist/index.cjs +3 -1
  85. package/dist/index.cjs.map +1 -1
  86. package/dist/index.d.cts +1 -0
  87. package/dist/index.d.cts.map +1 -1
  88. package/dist/index.d.mts +1 -0
  89. package/dist/index.d.mts.map +1 -1
  90. package/dist/index.mjs +1 -0
  91. package/dist/index.mjs.map +1 -1
  92. package/dist/utils/formatters.cjs +251 -0
  93. package/dist/utils/formatters.cjs.map +1 -0
  94. package/dist/utils/formatters.d.cts +56 -0
  95. package/dist/utils/formatters.d.cts.map +1 -0
  96. package/dist/utils/formatters.d.mts +56 -0
  97. package/dist/utils/formatters.d.mts.map +1 -0
  98. package/dist/utils/formatters.mjs +247 -0
  99. package/dist/utils/formatters.mjs.map +1 -0
  100. package/package.json +12 -12
@@ -0,0 +1,251 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createFormatters = void 0;
4
+ const FALLBACK_LOCALE = 'en';
5
+ const twoDecimals = {
6
+ minimumFractionDigits: 2,
7
+ maximumFractionDigits: 2,
8
+ };
9
+ const oneSignificantDigit = {
10
+ minimumSignificantDigits: 1,
11
+ maximumSignificantDigits: 1,
12
+ };
13
+ const threeSignificantDigits = {
14
+ minimumSignificantDigits: 3,
15
+ maximumSignificantDigits: 3,
16
+ };
17
+ const numberFormatCache = {};
18
+ /**
19
+ * Get cached number format instance.
20
+ *
21
+ * @param locale - Locale string.
22
+ * @param options - Optional Intl.NumberFormat options.
23
+ * @returns Cached Intl.NumberFormat instance.
24
+ */
25
+ function getCachedNumberFormat(locale, options = {}) {
26
+ const key = `${locale}_${JSON.stringify(options)}`;
27
+ let format = numberFormatCache[key];
28
+ if (format) {
29
+ return format;
30
+ }
31
+ try {
32
+ format = new Intl.NumberFormat(locale, options);
33
+ }
34
+ catch (error) {
35
+ if (error instanceof RangeError) {
36
+ // Fallback for invalid options (e.g. currency code)
37
+ format = new Intl.NumberFormat(locale, twoDecimals);
38
+ }
39
+ else {
40
+ throw error;
41
+ }
42
+ }
43
+ numberFormatCache[key] = format;
44
+ return format;
45
+ }
46
+ /**
47
+ * Format a value as a currency string.
48
+ *
49
+ * @param config - Configuration object with locale.
50
+ * @param config.locale - Locale string.
51
+ * @param value - Numeric value to format.
52
+ * @param currency - ISO 4217 currency code.
53
+ * @param options - Optional Intl.NumberFormat overrides.
54
+ * @returns Formatted currency string.
55
+ */
56
+ function formatCurrency(config, value, currency, options = {}) {
57
+ if (!Number.isFinite(Number(value))) {
58
+ return '';
59
+ }
60
+ const numberFormat = getCachedNumberFormat(config.locale, {
61
+ style: 'currency',
62
+ currency,
63
+ ...options,
64
+ });
65
+ // @ts-expect-error Remove this comment once TypeScript is updated to 5.5+
66
+ return numberFormat.format(value);
67
+ }
68
+ /**
69
+ * Compact currency formatting (e.g. $1.2K, $3.4M).
70
+ *
71
+ * @param config - Configuration object with locale.
72
+ * @param config.locale - Locale string.
73
+ * @param value - Numeric value to format.
74
+ * @param currency - ISO 4217 currency code.
75
+ * @returns Formatted compact currency string.
76
+ */
77
+ function formatCurrencyCompact(config, value, currency) {
78
+ return formatCurrency(config, value, currency, {
79
+ notation: 'compact',
80
+ ...twoDecimals,
81
+ });
82
+ }
83
+ /**
84
+ * Currency formatting with minimum threshold for small values.
85
+ *
86
+ * @param config - Configuration object with locale.
87
+ * @param config.locale - Locale string.
88
+ * @param value - Numeric value to format.
89
+ * @param currency - ISO 4217 currency code.
90
+ * @returns Formatted currency string with threshold handling.
91
+ */
92
+ function formatCurrencyWithMinThreshold(config, value, currency) {
93
+ const minThreshold = 0.01;
94
+ const number = Number(value);
95
+ const absoluteValue = Math.abs(number);
96
+ if (!Number.isFinite(number)) {
97
+ return '';
98
+ }
99
+ if (number === 0) {
100
+ return formatCurrency(config, 0, currency);
101
+ }
102
+ if (absoluteValue < minThreshold) {
103
+ const formattedMin = formatCurrency(config, minThreshold, currency);
104
+ return `<${formattedMin}`;
105
+ }
106
+ return formatCurrency(config, number, currency);
107
+ }
108
+ /**
109
+ * Format a value as a token string with symbol.
110
+ *
111
+ * @param config - Configuration object with locale.
112
+ * @param config.locale - Locale string.
113
+ * @param value - Numeric value to format.
114
+ * @param symbol - Token symbol.
115
+ * @param options - Optional Intl.NumberFormat overrides.
116
+ * @returns Formatted token string.
117
+ */
118
+ function formatToken(config, value, symbol, options = {}) {
119
+ if (!Number.isFinite(Number(value))) {
120
+ return '';
121
+ }
122
+ const numberFormat = getCachedNumberFormat(config.locale, {
123
+ style: 'decimal',
124
+ ...options,
125
+ });
126
+ // @ts-expect-error Remove this comment once TypeScript is updated to 5.5+
127
+ const formattedNumber = numberFormat.format(value);
128
+ return `${formattedNumber} ${symbol}`;
129
+ }
130
+ /**
131
+ * Format token price with varying precision based on value.
132
+ *
133
+ * @param config - Configuration object with locale.
134
+ * @param config.locale - Locale string.
135
+ * @param value - Numeric value to format.
136
+ * @param currency - ISO 4217 currency code.
137
+ * @returns Formatted token price string.
138
+ */
139
+ function formatCurrencyTokenPrice(config, value, currency) {
140
+ const minThreshold = 0.00000001;
141
+ const number = Number(value);
142
+ const absoluteValue = Math.abs(number);
143
+ if (!Number.isFinite(number)) {
144
+ return '';
145
+ }
146
+ if (number === 0) {
147
+ return formatCurrency(config, 0, currency);
148
+ }
149
+ if (absoluteValue < minThreshold) {
150
+ return `<${formatCurrency(config, minThreshold, currency, oneSignificantDigit)}`;
151
+ }
152
+ if (absoluteValue < 1) {
153
+ return formatCurrency(config, number, currency, threeSignificantDigits);
154
+ }
155
+ if (absoluteValue < 1000000) {
156
+ return formatCurrency(config, number, currency);
157
+ }
158
+ return formatCurrencyCompact(config, number, currency);
159
+ }
160
+ /**
161
+ * Format token quantity with varying precision based on value.
162
+ *
163
+ * @param config - Configuration object with locale.
164
+ * @param config.locale - Locale string.
165
+ * @param value - Numeric value to format.
166
+ * @param symbol - Token symbol.
167
+ * @returns Formatted token quantity string.
168
+ */
169
+ function formatTokenQuantity(config, value, symbol) {
170
+ const minThreshold = 0.00001;
171
+ const number = Number(value);
172
+ const absoluteValue = Math.abs(number);
173
+ if (!Number.isFinite(number)) {
174
+ return '';
175
+ }
176
+ if (number === 0) {
177
+ return formatToken(config, 0, symbol);
178
+ }
179
+ if (absoluteValue < minThreshold) {
180
+ return `<${formatToken(config, minThreshold, symbol, oneSignificantDigit)}`;
181
+ }
182
+ if (absoluteValue < 1) {
183
+ return formatToken(config, number, symbol, threeSignificantDigits);
184
+ }
185
+ if (absoluteValue < 1000000) {
186
+ return formatToken(config, number, symbol);
187
+ }
188
+ return formatToken(config, number, symbol, {
189
+ notation: 'compact',
190
+ ...twoDecimals,
191
+ });
192
+ }
193
+ /**
194
+ * Create formatter functions with the given locale.
195
+ *
196
+ * @param options - Configuration options.
197
+ * @param options.locale - Locale string.
198
+ * @returns Object with formatter functions.
199
+ */
200
+ function createFormatters({ locale = FALLBACK_LOCALE }) {
201
+ return {
202
+ /**
203
+ * Format a value as a currency string.
204
+ *
205
+ * @param value - Numeric value to format.
206
+ * @param currency - ISO 4217 currency code (e.g. 'USD').
207
+ * @param options - Optional Intl.NumberFormat overrides.
208
+ */
209
+ formatCurrency: formatCurrency.bind(null, { locale }),
210
+ /**
211
+ * Compact currency (e.g. $1.2K, $3.4M) with up to two decimal digits.
212
+ *
213
+ * @param value - Numeric value to format.
214
+ * @param currency - ISO 4217 currency code.
215
+ */
216
+ formatCurrencyCompact: formatCurrencyCompact.bind(null, { locale }),
217
+ /**
218
+ * Currency with thresholds for small values.
219
+ *
220
+ * @param value - Numeric value to format.
221
+ * @param currency - ISO 4217 currency code.
222
+ */
223
+ formatCurrencyWithMinThreshold: formatCurrencyWithMinThreshold.bind(null, {
224
+ locale,
225
+ }),
226
+ /**
227
+ * Format token price with varying precision based on value.
228
+ *
229
+ * @param value - Numeric value to format.
230
+ * @param currency - ISO 4217 currency code.
231
+ */
232
+ formatCurrencyTokenPrice: formatCurrencyTokenPrice.bind(null, { locale }),
233
+ /**
234
+ * Format a value as a token string with symbol.
235
+ *
236
+ * @param value - Numeric value to format.
237
+ * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').
238
+ * @param options - Optional Intl.NumberFormat overrides.
239
+ */
240
+ formatToken: formatToken.bind(null, { locale }),
241
+ /**
242
+ * Format token quantity with varying precision based on value.
243
+ *
244
+ * @param value - Numeric value to format.
245
+ * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').
246
+ */
247
+ formatTokenQuantity: formatTokenQuantity.bind(null, { locale }),
248
+ };
249
+ }
250
+ exports.createFormatters = createFormatters;
251
+ //# sourceMappingURL=formatters.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.cjs","sourceRoot":"","sources":["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;;;WAKG;QACH,8BAA8B,EAAE,8BAA8B,CAAC,IAAI,CAAC,IAAI,EAAE;YACxE,MAAM;SACP,CAAC;QACF;;;;;WAKG;QACH,wBAAwB,EAAE,wBAAwB,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;QACzE;;;;;;WAMG;QACH,WAAW,EAAE,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;QAC/C;;;;;WAKG;QACH,mBAAmB,EAAE,mBAAmB,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;KAChE,CAAC;AACJ,CAAC;AAjDD,4CAiDC","sourcesContent":["const FALLBACK_LOCALE = 'en';\n\nconst twoDecimals = {\n minimumFractionDigits: 2,\n maximumFractionDigits: 2,\n};\n\nconst oneSignificantDigit = {\n minimumSignificantDigits: 1,\n maximumSignificantDigits: 1,\n};\n\nconst threeSignificantDigits = {\n minimumSignificantDigits: 3,\n maximumSignificantDigits: 3,\n};\n\nconst numberFormatCache: Record<string, Intl.NumberFormat> = {};\n\n/**\n * Get cached number format instance.\n *\n * @param locale - Locale string.\n * @param options - Optional Intl.NumberFormat options.\n * @returns Cached Intl.NumberFormat instance.\n */\nfunction getCachedNumberFormat(\n locale: string,\n options: Intl.NumberFormatOptions = {},\n) {\n const key = `${locale}_${JSON.stringify(options)}`;\n\n let format = numberFormatCache[key];\n\n if (format) {\n return format;\n }\n\n try {\n format = new Intl.NumberFormat(locale, options);\n } catch (error) {\n if (error instanceof RangeError) {\n // Fallback for invalid options (e.g. currency code)\n format = new Intl.NumberFormat(locale, twoDecimals);\n } else {\n throw error;\n }\n }\n\n numberFormatCache[key] = format;\n return format;\n}\n\n/**\n * Format a value as a currency string.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n * @param options - Optional Intl.NumberFormat overrides.\n * @returns Formatted currency string.\n */\nfunction formatCurrency(\n config: { locale: string },\n value: number | bigint | `${number}`,\n currency: Intl.NumberFormatOptions['currency'],\n options: Intl.NumberFormatOptions = {},\n) {\n if (!Number.isFinite(Number(value))) {\n return '';\n }\n\n const numberFormat = getCachedNumberFormat(config.locale, {\n style: 'currency',\n currency,\n ...options,\n });\n\n // @ts-expect-error Remove this comment once TypeScript is updated to 5.5+\n return numberFormat.format(value);\n}\n\n/**\n * Compact currency formatting (e.g. $1.2K, $3.4M).\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n * @returns Formatted compact currency string.\n */\nfunction formatCurrencyCompact(\n config: { locale: string },\n value: number | bigint | `${number}`,\n currency: Intl.NumberFormatOptions['currency'],\n) {\n return formatCurrency(config, value, currency, {\n notation: 'compact',\n ...twoDecimals,\n });\n}\n\n/**\n * Currency formatting with minimum threshold for small values.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n * @returns Formatted currency string with threshold handling.\n */\nfunction formatCurrencyWithMinThreshold(\n config: { locale: string },\n value: number | bigint | `${number}`,\n currency: Intl.NumberFormatOptions['currency'],\n) {\n const minThreshold = 0.01;\n const number = Number(value);\n const absoluteValue = Math.abs(number);\n\n if (!Number.isFinite(number)) {\n return '';\n }\n\n if (number === 0) {\n return formatCurrency(config, 0, currency);\n }\n\n if (absoluteValue < minThreshold) {\n const formattedMin = formatCurrency(config, minThreshold, currency);\n return `<${formattedMin}`;\n }\n\n return formatCurrency(config, number, currency);\n}\n\n/**\n * Format a value as a token string with symbol.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param symbol - Token symbol.\n * @param options - Optional Intl.NumberFormat overrides.\n * @returns Formatted token string.\n */\nfunction formatToken(\n config: { locale: string },\n value: number | bigint | `${number}`,\n symbol: string,\n options: Intl.NumberFormatOptions = {},\n) {\n if (!Number.isFinite(Number(value))) {\n return '';\n }\n\n const numberFormat = getCachedNumberFormat(config.locale, {\n style: 'decimal',\n ...options,\n });\n\n // @ts-expect-error Remove this comment once TypeScript is updated to 5.5+\n const formattedNumber = numberFormat.format(value);\n\n return `${formattedNumber} ${symbol}`;\n}\n\n/**\n * Format token price with varying precision based on value.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n * @returns Formatted token price string.\n */\nfunction formatCurrencyTokenPrice(\n config: { locale: string },\n value: number | bigint | `${number}`,\n currency: Intl.NumberFormatOptions['currency'],\n) {\n const minThreshold = 0.00000001;\n const number = Number(value);\n const absoluteValue = Math.abs(number);\n\n if (!Number.isFinite(number)) {\n return '';\n }\n\n if (number === 0) {\n return formatCurrency(config, 0, currency);\n }\n\n if (absoluteValue < minThreshold) {\n return `<${formatCurrency(config, minThreshold, currency, oneSignificantDigit)}`;\n }\n\n if (absoluteValue < 1) {\n return formatCurrency(config, number, currency, threeSignificantDigits);\n }\n\n if (absoluteValue < 1_000_000) {\n return formatCurrency(config, number, currency);\n }\n\n return formatCurrencyCompact(config, number, currency);\n}\n\n/**\n * Format token quantity with varying precision based on value.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param symbol - Token symbol.\n * @returns Formatted token quantity string.\n */\nfunction formatTokenQuantity(\n config: { locale: string },\n value: number | bigint | `${number}`,\n symbol: string,\n) {\n const minThreshold = 0.00001;\n const number = Number(value);\n const absoluteValue = Math.abs(number);\n\n if (!Number.isFinite(number)) {\n return '';\n }\n\n if (number === 0) {\n return formatToken(config, 0, symbol);\n }\n\n if (absoluteValue < minThreshold) {\n return `<${formatToken(config, minThreshold, symbol, oneSignificantDigit)}`;\n }\n\n if (absoluteValue < 1) {\n return formatToken(config, number, symbol, threeSignificantDigits);\n }\n\n if (absoluteValue < 1_000_000) {\n return formatToken(config, number, symbol);\n }\n\n return formatToken(config, number, symbol, {\n notation: 'compact',\n ...twoDecimals,\n });\n}\n\n/**\n * Create formatter functions with the given locale.\n *\n * @param options - Configuration options.\n * @param options.locale - Locale string.\n * @returns Object with formatter functions.\n */\nexport function createFormatters({ locale = FALLBACK_LOCALE }) {\n return {\n /**\n * Format a value as a currency string.\n *\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code (e.g. 'USD').\n * @param options - Optional Intl.NumberFormat overrides.\n */\n formatCurrency: formatCurrency.bind(null, { locale }),\n /**\n * Compact currency (e.g. $1.2K, $3.4M) with up to two decimal digits.\n *\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n */\n formatCurrencyCompact: formatCurrencyCompact.bind(null, { locale }),\n /**\n * Currency with thresholds for small values.\n *\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n */\n formatCurrencyWithMinThreshold: formatCurrencyWithMinThreshold.bind(null, {\n locale,\n }),\n /**\n * Format token price with varying precision based on value.\n *\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n */\n formatCurrencyTokenPrice: formatCurrencyTokenPrice.bind(null, { locale }),\n /**\n * Format a value as a token string with symbol.\n *\n * @param value - Numeric value to format.\n * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').\n * @param options - Optional Intl.NumberFormat overrides.\n */\n formatToken: formatToken.bind(null, { locale }),\n /**\n * Format token quantity with varying precision based on value.\n *\n * @param value - Numeric value to format.\n * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').\n */\n formatTokenQuantity: formatTokenQuantity.bind(null, { locale }),\n };\n}\n"]}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Create formatter functions with the given locale.
3
+ *
4
+ * @param options - Configuration options.
5
+ * @param options.locale - Locale string.
6
+ * @returns Object with formatter functions.
7
+ */
8
+ export declare function createFormatters({ locale }: {
9
+ locale?: string | undefined;
10
+ }): {
11
+ /**
12
+ * Format a value as a currency string.
13
+ *
14
+ * @param value - Numeric value to format.
15
+ * @param currency - ISO 4217 currency code (e.g. 'USD').
16
+ * @param options - Optional Intl.NumberFormat overrides.
17
+ */
18
+ formatCurrency: (value: number | bigint | `${number}`, currency: string | undefined, options?: Intl.NumberFormatOptions | undefined) => string;
19
+ /**
20
+ * Compact currency (e.g. $1.2K, $3.4M) with up to two decimal digits.
21
+ *
22
+ * @param value - Numeric value to format.
23
+ * @param currency - ISO 4217 currency code.
24
+ */
25
+ formatCurrencyCompact: (value: number | bigint | `${number}`, currency: string | undefined) => string;
26
+ /**
27
+ * Currency with thresholds for small values.
28
+ *
29
+ * @param value - Numeric value to format.
30
+ * @param currency - ISO 4217 currency code.
31
+ */
32
+ formatCurrencyWithMinThreshold: (value: number | bigint | `${number}`, currency: string | undefined) => string;
33
+ /**
34
+ * Format token price with varying precision based on value.
35
+ *
36
+ * @param value - Numeric value to format.
37
+ * @param currency - ISO 4217 currency code.
38
+ */
39
+ formatCurrencyTokenPrice: (value: number | bigint | `${number}`, currency: string | undefined) => string;
40
+ /**
41
+ * Format a value as a token string with symbol.
42
+ *
43
+ * @param value - Numeric value to format.
44
+ * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').
45
+ * @param options - Optional Intl.NumberFormat overrides.
46
+ */
47
+ formatToken: (value: number | bigint | `${number}`, symbol: string, options?: Intl.NumberFormatOptions | undefined) => string;
48
+ /**
49
+ * Format token quantity with varying precision based on value.
50
+ *
51
+ * @param value - Numeric value to format.
52
+ * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').
53
+ */
54
+ formatTokenQuantity: (value: number | bigint | `${number}`, symbol: string) => string;
55
+ };
56
+ //# sourceMappingURL=formatters.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.d.cts","sourceRoot":"","sources":["../../src/utils/formatters.ts"],"names":[],"mappings":"AA6PA;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,MAAwB,EAAE;;CAAA;IAEzD;;;;;;OAMG;;IAEH;;;;;OAKG;;IAEH;;;;;OAKG;;IAIH;;;;;OAKG;;IAEH;;;;;;OAMG;;IAEH;;;;;OAKG;;EAGN"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Create formatter functions with the given locale.
3
+ *
4
+ * @param options - Configuration options.
5
+ * @param options.locale - Locale string.
6
+ * @returns Object with formatter functions.
7
+ */
8
+ export declare function createFormatters({ locale }: {
9
+ locale?: string | undefined;
10
+ }): {
11
+ /**
12
+ * Format a value as a currency string.
13
+ *
14
+ * @param value - Numeric value to format.
15
+ * @param currency - ISO 4217 currency code (e.g. 'USD').
16
+ * @param options - Optional Intl.NumberFormat overrides.
17
+ */
18
+ formatCurrency: (value: number | bigint | `${number}`, currency: string | undefined, options?: Intl.NumberFormatOptions | undefined) => string;
19
+ /**
20
+ * Compact currency (e.g. $1.2K, $3.4M) with up to two decimal digits.
21
+ *
22
+ * @param value - Numeric value to format.
23
+ * @param currency - ISO 4217 currency code.
24
+ */
25
+ formatCurrencyCompact: (value: number | bigint | `${number}`, currency: string | undefined) => string;
26
+ /**
27
+ * Currency with thresholds for small values.
28
+ *
29
+ * @param value - Numeric value to format.
30
+ * @param currency - ISO 4217 currency code.
31
+ */
32
+ formatCurrencyWithMinThreshold: (value: number | bigint | `${number}`, currency: string | undefined) => string;
33
+ /**
34
+ * Format token price with varying precision based on value.
35
+ *
36
+ * @param value - Numeric value to format.
37
+ * @param currency - ISO 4217 currency code.
38
+ */
39
+ formatCurrencyTokenPrice: (value: number | bigint | `${number}`, currency: string | undefined) => string;
40
+ /**
41
+ * Format a value as a token string with symbol.
42
+ *
43
+ * @param value - Numeric value to format.
44
+ * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').
45
+ * @param options - Optional Intl.NumberFormat overrides.
46
+ */
47
+ formatToken: (value: number | bigint | `${number}`, symbol: string, options?: Intl.NumberFormatOptions | undefined) => string;
48
+ /**
49
+ * Format token quantity with varying precision based on value.
50
+ *
51
+ * @param value - Numeric value to format.
52
+ * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').
53
+ */
54
+ formatTokenQuantity: (value: number | bigint | `${number}`, symbol: string) => string;
55
+ };
56
+ //# sourceMappingURL=formatters.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.d.mts","sourceRoot":"","sources":["../../src/utils/formatters.ts"],"names":[],"mappings":"AA6PA;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,MAAwB,EAAE;;CAAA;IAEzD;;;;;;OAMG;;IAEH;;;;;OAKG;;IAEH;;;;;OAKG;;IAIH;;;;;OAKG;;IAEH;;;;;;OAMG;;IAEH;;;;;OAKG;;EAGN"}
@@ -0,0 +1,247 @@
1
+ const FALLBACK_LOCALE = 'en';
2
+ const twoDecimals = {
3
+ minimumFractionDigits: 2,
4
+ maximumFractionDigits: 2,
5
+ };
6
+ const oneSignificantDigit = {
7
+ minimumSignificantDigits: 1,
8
+ maximumSignificantDigits: 1,
9
+ };
10
+ const threeSignificantDigits = {
11
+ minimumSignificantDigits: 3,
12
+ maximumSignificantDigits: 3,
13
+ };
14
+ const numberFormatCache = {};
15
+ /**
16
+ * Get cached number format instance.
17
+ *
18
+ * @param locale - Locale string.
19
+ * @param options - Optional Intl.NumberFormat options.
20
+ * @returns Cached Intl.NumberFormat instance.
21
+ */
22
+ function getCachedNumberFormat(locale, options = {}) {
23
+ const key = `${locale}_${JSON.stringify(options)}`;
24
+ let format = numberFormatCache[key];
25
+ if (format) {
26
+ return format;
27
+ }
28
+ try {
29
+ format = new Intl.NumberFormat(locale, options);
30
+ }
31
+ catch (error) {
32
+ if (error instanceof RangeError) {
33
+ // Fallback for invalid options (e.g. currency code)
34
+ format = new Intl.NumberFormat(locale, twoDecimals);
35
+ }
36
+ else {
37
+ throw error;
38
+ }
39
+ }
40
+ numberFormatCache[key] = format;
41
+ return format;
42
+ }
43
+ /**
44
+ * Format a value as a currency string.
45
+ *
46
+ * @param config - Configuration object with locale.
47
+ * @param config.locale - Locale string.
48
+ * @param value - Numeric value to format.
49
+ * @param currency - ISO 4217 currency code.
50
+ * @param options - Optional Intl.NumberFormat overrides.
51
+ * @returns Formatted currency string.
52
+ */
53
+ function formatCurrency(config, value, currency, options = {}) {
54
+ if (!Number.isFinite(Number(value))) {
55
+ return '';
56
+ }
57
+ const numberFormat = getCachedNumberFormat(config.locale, {
58
+ style: 'currency',
59
+ currency,
60
+ ...options,
61
+ });
62
+ // @ts-expect-error Remove this comment once TypeScript is updated to 5.5+
63
+ return numberFormat.format(value);
64
+ }
65
+ /**
66
+ * Compact currency formatting (e.g. $1.2K, $3.4M).
67
+ *
68
+ * @param config - Configuration object with locale.
69
+ * @param config.locale - Locale string.
70
+ * @param value - Numeric value to format.
71
+ * @param currency - ISO 4217 currency code.
72
+ * @returns Formatted compact currency string.
73
+ */
74
+ function formatCurrencyCompact(config, value, currency) {
75
+ return formatCurrency(config, value, currency, {
76
+ notation: 'compact',
77
+ ...twoDecimals,
78
+ });
79
+ }
80
+ /**
81
+ * Currency formatting with minimum threshold for small values.
82
+ *
83
+ * @param config - Configuration object with locale.
84
+ * @param config.locale - Locale string.
85
+ * @param value - Numeric value to format.
86
+ * @param currency - ISO 4217 currency code.
87
+ * @returns Formatted currency string with threshold handling.
88
+ */
89
+ function formatCurrencyWithMinThreshold(config, value, currency) {
90
+ const minThreshold = 0.01;
91
+ const number = Number(value);
92
+ const absoluteValue = Math.abs(number);
93
+ if (!Number.isFinite(number)) {
94
+ return '';
95
+ }
96
+ if (number === 0) {
97
+ return formatCurrency(config, 0, currency);
98
+ }
99
+ if (absoluteValue < minThreshold) {
100
+ const formattedMin = formatCurrency(config, minThreshold, currency);
101
+ return `<${formattedMin}`;
102
+ }
103
+ return formatCurrency(config, number, currency);
104
+ }
105
+ /**
106
+ * Format a value as a token string with symbol.
107
+ *
108
+ * @param config - Configuration object with locale.
109
+ * @param config.locale - Locale string.
110
+ * @param value - Numeric value to format.
111
+ * @param symbol - Token symbol.
112
+ * @param options - Optional Intl.NumberFormat overrides.
113
+ * @returns Formatted token string.
114
+ */
115
+ function formatToken(config, value, symbol, options = {}) {
116
+ if (!Number.isFinite(Number(value))) {
117
+ return '';
118
+ }
119
+ const numberFormat = getCachedNumberFormat(config.locale, {
120
+ style: 'decimal',
121
+ ...options,
122
+ });
123
+ // @ts-expect-error Remove this comment once TypeScript is updated to 5.5+
124
+ const formattedNumber = numberFormat.format(value);
125
+ return `${formattedNumber} ${symbol}`;
126
+ }
127
+ /**
128
+ * Format token price with varying precision based on value.
129
+ *
130
+ * @param config - Configuration object with locale.
131
+ * @param config.locale - Locale string.
132
+ * @param value - Numeric value to format.
133
+ * @param currency - ISO 4217 currency code.
134
+ * @returns Formatted token price string.
135
+ */
136
+ function formatCurrencyTokenPrice(config, value, currency) {
137
+ const minThreshold = 0.00000001;
138
+ const number = Number(value);
139
+ const absoluteValue = Math.abs(number);
140
+ if (!Number.isFinite(number)) {
141
+ return '';
142
+ }
143
+ if (number === 0) {
144
+ return formatCurrency(config, 0, currency);
145
+ }
146
+ if (absoluteValue < minThreshold) {
147
+ return `<${formatCurrency(config, minThreshold, currency, oneSignificantDigit)}`;
148
+ }
149
+ if (absoluteValue < 1) {
150
+ return formatCurrency(config, number, currency, threeSignificantDigits);
151
+ }
152
+ if (absoluteValue < 1000000) {
153
+ return formatCurrency(config, number, currency);
154
+ }
155
+ return formatCurrencyCompact(config, number, currency);
156
+ }
157
+ /**
158
+ * Format token quantity with varying precision based on value.
159
+ *
160
+ * @param config - Configuration object with locale.
161
+ * @param config.locale - Locale string.
162
+ * @param value - Numeric value to format.
163
+ * @param symbol - Token symbol.
164
+ * @returns Formatted token quantity string.
165
+ */
166
+ function formatTokenQuantity(config, value, symbol) {
167
+ const minThreshold = 0.00001;
168
+ const number = Number(value);
169
+ const absoluteValue = Math.abs(number);
170
+ if (!Number.isFinite(number)) {
171
+ return '';
172
+ }
173
+ if (number === 0) {
174
+ return formatToken(config, 0, symbol);
175
+ }
176
+ if (absoluteValue < minThreshold) {
177
+ return `<${formatToken(config, minThreshold, symbol, oneSignificantDigit)}`;
178
+ }
179
+ if (absoluteValue < 1) {
180
+ return formatToken(config, number, symbol, threeSignificantDigits);
181
+ }
182
+ if (absoluteValue < 1000000) {
183
+ return formatToken(config, number, symbol);
184
+ }
185
+ return formatToken(config, number, symbol, {
186
+ notation: 'compact',
187
+ ...twoDecimals,
188
+ });
189
+ }
190
+ /**
191
+ * Create formatter functions with the given locale.
192
+ *
193
+ * @param options - Configuration options.
194
+ * @param options.locale - Locale string.
195
+ * @returns Object with formatter functions.
196
+ */
197
+ export function createFormatters({ locale = FALLBACK_LOCALE }) {
198
+ return {
199
+ /**
200
+ * Format a value as a currency string.
201
+ *
202
+ * @param value - Numeric value to format.
203
+ * @param currency - ISO 4217 currency code (e.g. 'USD').
204
+ * @param options - Optional Intl.NumberFormat overrides.
205
+ */
206
+ formatCurrency: formatCurrency.bind(null, { locale }),
207
+ /**
208
+ * Compact currency (e.g. $1.2K, $3.4M) with up to two decimal digits.
209
+ *
210
+ * @param value - Numeric value to format.
211
+ * @param currency - ISO 4217 currency code.
212
+ */
213
+ formatCurrencyCompact: formatCurrencyCompact.bind(null, { locale }),
214
+ /**
215
+ * Currency with thresholds for small values.
216
+ *
217
+ * @param value - Numeric value to format.
218
+ * @param currency - ISO 4217 currency code.
219
+ */
220
+ formatCurrencyWithMinThreshold: formatCurrencyWithMinThreshold.bind(null, {
221
+ locale,
222
+ }),
223
+ /**
224
+ * Format token price with varying precision based on value.
225
+ *
226
+ * @param value - Numeric value to format.
227
+ * @param currency - ISO 4217 currency code.
228
+ */
229
+ formatCurrencyTokenPrice: formatCurrencyTokenPrice.bind(null, { locale }),
230
+ /**
231
+ * Format a value as a token string with symbol.
232
+ *
233
+ * @param value - Numeric value to format.
234
+ * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').
235
+ * @param options - Optional Intl.NumberFormat overrides.
236
+ */
237
+ formatToken: formatToken.bind(null, { locale }),
238
+ /**
239
+ * Format token quantity with varying precision based on value.
240
+ *
241
+ * @param value - Numeric value to format.
242
+ * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').
243
+ */
244
+ formatTokenQuantity: formatTokenQuantity.bind(null, { locale }),
245
+ };
246
+ }
247
+ //# sourceMappingURL=formatters.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.mjs","sourceRoot":"","sources":["../../src/utils/formatters.ts"],"names":[],"mappings":"AAAA,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B,MAAM,WAAW,GAAG;IAClB,qBAAqB,EAAE,CAAC;IACxB,qBAAqB,EAAE,CAAC;CACzB,CAAC;AAEF,MAAM,mBAAmB,GAAG;IAC1B,wBAAwB,EAAE,CAAC;IAC3B,wBAAwB,EAAE,CAAC;CAC5B,CAAC;AAEF,MAAM,sBAAsB,GAAG;IAC7B,wBAAwB,EAAE,CAAC;IAC3B,wBAAwB,EAAE,CAAC;CAC5B,CAAC;AAEF,MAAM,iBAAiB,GAAsC,EAAE,CAAC;AAEhE;;;;;;GAMG;AACH,SAAS,qBAAqB,CAC5B,MAAc,EACd,UAAoC,EAAE;IAEtC,MAAM,GAAG,GAAG,GAAG,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;IAEnD,IAAI,MAAM,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC;IAEpC,IAAI,MAAM,EAAE;QACV,OAAO,MAAM,CAAC;KACf;IAED,IAAI;QACF,MAAM,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACjD;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,YAAY,UAAU,EAAE;YAC/B,oDAAoD;YACpD,MAAM,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;SACrD;aAAM;YACL,MAAM,KAAK,CAAC;SACb;KACF;IAED,iBAAiB,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC;IAChC,OAAO,MA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,EAAE;IAC3D,OAAO;QACL;;;;;;WAMG;QACH,cAAc,EAAE,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;QACrD;;;;;WAKG;QACH,qBAAqB,EAAE,qBAAqB,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;QACnE;;;;;WAKG;QACH,8BAA8B,EAAE,8BAA8B,CAAC,IAAI,CAAC,IAAI,EAAE;YACxE,MAAM;SACP,CAAC;QACF;;;;;WAKG;QACH,wBAAwB,EAAE,wBAAwB,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;QACzE;;;;;;WAMG;QACH,WAAW,EAAE,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;QAC/C;;;;;WAKG;QACH,mBAAmB,EAAE,mBAAmB,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;KAChE,CAAC;AACJ,CAAC","sourcesContent":["const FALLBACK_LOCALE = 'en';\n\nconst twoDecimals = {\n minimumFractionDigits: 2,\n maximumFractionDigits: 2,\n};\n\nconst oneSignificantDigit = {\n minimumSignificantDigits: 1,\n maximumSignificantDigits: 1,\n};\n\nconst threeSignificantDigits = {\n minimumSignificantDigits: 3,\n maximumSignificantDigits: 3,\n};\n\nconst numberFormatCache: Record<string, Intl.NumberFormat> = {};\n\n/**\n * Get cached number format instance.\n *\n * @param locale - Locale string.\n * @param options - Optional Intl.NumberFormat options.\n * @returns Cached Intl.NumberFormat instance.\n */\nfunction getCachedNumberFormat(\n locale: string,\n options: Intl.NumberFormatOptions = {},\n) {\n const key = `${locale}_${JSON.stringify(options)}`;\n\n let format = numberFormatCache[key];\n\n if (format) {\n return format;\n }\n\n try {\n format = new Intl.NumberFormat(locale, options);\n } catch (error) {\n if (error instanceof RangeError) {\n // Fallback for invalid options (e.g. currency code)\n format = new Intl.NumberFormat(locale, twoDecimals);\n } else {\n throw error;\n }\n }\n\n numberFormatCache[key] = format;\n return format;\n}\n\n/**\n * Format a value as a currency string.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n * @param options - Optional Intl.NumberFormat overrides.\n * @returns Formatted currency string.\n */\nfunction formatCurrency(\n config: { locale: string },\n value: number | bigint | `${number}`,\n currency: Intl.NumberFormatOptions['currency'],\n options: Intl.NumberFormatOptions = {},\n) {\n if (!Number.isFinite(Number(value))) {\n return '';\n }\n\n const numberFormat = getCachedNumberFormat(config.locale, {\n style: 'currency',\n currency,\n ...options,\n });\n\n // @ts-expect-error Remove this comment once TypeScript is updated to 5.5+\n return numberFormat.format(value);\n}\n\n/**\n * Compact currency formatting (e.g. $1.2K, $3.4M).\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n * @returns Formatted compact currency string.\n */\nfunction formatCurrencyCompact(\n config: { locale: string },\n value: number | bigint | `${number}`,\n currency: Intl.NumberFormatOptions['currency'],\n) {\n return formatCurrency(config, value, currency, {\n notation: 'compact',\n ...twoDecimals,\n });\n}\n\n/**\n * Currency formatting with minimum threshold for small values.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n * @returns Formatted currency string with threshold handling.\n */\nfunction formatCurrencyWithMinThreshold(\n config: { locale: string },\n value: number | bigint | `${number}`,\n currency: Intl.NumberFormatOptions['currency'],\n) {\n const minThreshold = 0.01;\n const number = Number(value);\n const absoluteValue = Math.abs(number);\n\n if (!Number.isFinite(number)) {\n return '';\n }\n\n if (number === 0) {\n return formatCurrency(config, 0, currency);\n }\n\n if (absoluteValue < minThreshold) {\n const formattedMin = formatCurrency(config, minThreshold, currency);\n return `<${formattedMin}`;\n }\n\n return formatCurrency(config, number, currency);\n}\n\n/**\n * Format a value as a token string with symbol.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param symbol - Token symbol.\n * @param options - Optional Intl.NumberFormat overrides.\n * @returns Formatted token string.\n */\nfunction formatToken(\n config: { locale: string },\n value: number | bigint | `${number}`,\n symbol: string,\n options: Intl.NumberFormatOptions = {},\n) {\n if (!Number.isFinite(Number(value))) {\n return '';\n }\n\n const numberFormat = getCachedNumberFormat(config.locale, {\n style: 'decimal',\n ...options,\n });\n\n // @ts-expect-error Remove this comment once TypeScript is updated to 5.5+\n const formattedNumber = numberFormat.format(value);\n\n return `${formattedNumber} ${symbol}`;\n}\n\n/**\n * Format token price with varying precision based on value.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n * @returns Formatted token price string.\n */\nfunction formatCurrencyTokenPrice(\n config: { locale: string },\n value: number | bigint | `${number}`,\n currency: Intl.NumberFormatOptions['currency'],\n) {\n const minThreshold = 0.00000001;\n const number = Number(value);\n const absoluteValue = Math.abs(number);\n\n if (!Number.isFinite(number)) {\n return '';\n }\n\n if (number === 0) {\n return formatCurrency(config, 0, currency);\n }\n\n if (absoluteValue < minThreshold) {\n return `<${formatCurrency(config, minThreshold, currency, oneSignificantDigit)}`;\n }\n\n if (absoluteValue < 1) {\n return formatCurrency(config, number, currency, threeSignificantDigits);\n }\n\n if (absoluteValue < 1_000_000) {\n return formatCurrency(config, number, currency);\n }\n\n return formatCurrencyCompact(config, number, currency);\n}\n\n/**\n * Format token quantity with varying precision based on value.\n *\n * @param config - Configuration object with locale.\n * @param config.locale - Locale string.\n * @param value - Numeric value to format.\n * @param symbol - Token symbol.\n * @returns Formatted token quantity string.\n */\nfunction formatTokenQuantity(\n config: { locale: string },\n value: number | bigint | `${number}`,\n symbol: string,\n) {\n const minThreshold = 0.00001;\n const number = Number(value);\n const absoluteValue = Math.abs(number);\n\n if (!Number.isFinite(number)) {\n return '';\n }\n\n if (number === 0) {\n return formatToken(config, 0, symbol);\n }\n\n if (absoluteValue < minThreshold) {\n return `<${formatToken(config, minThreshold, symbol, oneSignificantDigit)}`;\n }\n\n if (absoluteValue < 1) {\n return formatToken(config, number, symbol, threeSignificantDigits);\n }\n\n if (absoluteValue < 1_000_000) {\n return formatToken(config, number, symbol);\n }\n\n return formatToken(config, number, symbol, {\n notation: 'compact',\n ...twoDecimals,\n });\n}\n\n/**\n * Create formatter functions with the given locale.\n *\n * @param options - Configuration options.\n * @param options.locale - Locale string.\n * @returns Object with formatter functions.\n */\nexport function createFormatters({ locale = FALLBACK_LOCALE }) {\n return {\n /**\n * Format a value as a currency string.\n *\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code (e.g. 'USD').\n * @param options - Optional Intl.NumberFormat overrides.\n */\n formatCurrency: formatCurrency.bind(null, { locale }),\n /**\n * Compact currency (e.g. $1.2K, $3.4M) with up to two decimal digits.\n *\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n */\n formatCurrencyCompact: formatCurrencyCompact.bind(null, { locale }),\n /**\n * Currency with thresholds for small values.\n *\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n */\n formatCurrencyWithMinThreshold: formatCurrencyWithMinThreshold.bind(null, {\n locale,\n }),\n /**\n * Format token price with varying precision based on value.\n *\n * @param value - Numeric value to format.\n * @param currency - ISO 4217 currency code.\n */\n formatCurrencyTokenPrice: formatCurrencyTokenPrice.bind(null, { locale }),\n /**\n * Format a value as a token string with symbol.\n *\n * @param value - Numeric value to format.\n * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').\n * @param options - Optional Intl.NumberFormat overrides.\n */\n formatToken: formatToken.bind(null, { locale }),\n /**\n * Format token quantity with varying precision based on value.\n *\n * @param value - Numeric value to format.\n * @param symbol - Token symbol (e.g. 'ETH', 'SepoliaETH').\n */\n formatTokenQuantity: formatTokenQuantity.bind(null, { locale }),\n };\n}\n"]}