Skip to content

Full Toolkit for Next-Level Domain Analysis

Trim trailing zeros from sale prices ✂️💰 (#117)

Felix Wotschofskyd95e8329fd3d
diff --git a/lib/format-sale-price.test.ts b/lib/format-sale-price.test.ts
index 8eebc56..70ec06b 100644
--- a/lib/format-sale-price.test.ts
+++ b/lib/format-sale-price.test.ts
@@ -9,18 +9,22 @@ describe('sale price formatting', () => {
['GBP 1250.50', '£1,250.50'],
['JPY 5000', '¥5,000'],
['USD 0', '$0'],
- ['USD 10.00', '$10.00'],
- ['EUR 0.00', '€0.00'],
- ['JPY 10.00', '¥10.00'],
- ['BTC 10.0000', 'BTC\u00a010.0000'],
+ ['USD 10.00', '$10'],
+ ['USD 10.5', '$10.50'],
+ ['USD 1000000.00', '$1,000,000'],
+ ['EUR 0.00', '€0'],
+ ['JPY 10.00', '¥10'],
+ ['BTC 10.0000', 'BTC\u00a010'],
+ ['BTC 12.8900000', 'BTC\u00a012.89'],
+ ['DOGE 11.4200', 'DOGE 11.42'],
['EUR 0.000001', '€0.000001'],
['USD 9007199254740993.01', '$9,007,199,254,740,993.01'],
])('formats %s as %s', (price, formatted) => {
expect(formatSalePrice(price)).toBe(formatted);
});
- it('keeps cryptocurrency precision when there is no standard symbol', () => {
- expect(formatSalePrice('BTC 0.000010')).toBe('BTC\u00a00.000010');
+ it('keeps significant cryptocurrency precision without trailing zeros', () => {
+ expect(formatSalePrice('BTC 0.000010')).toBe('BTC\u00a00.00001');
});
it.each(['TOKEN 100', 'not a price', 'USD -1', `USD 0.${'1'.repeat(101)}`])(
diff --git a/lib/format-sale-price.ts b/lib/format-sale-price.ts
index f8e10fa..4d4ea3c 100644
--- a/lib/format-sale-price.ts
+++ b/lib/format-sale-price.ts
@@ -3,7 +3,11 @@ export const formatSalePrice = (price: string): string => {
const match = /^([A-Z]+) (\d+(?:\.\d+)?)$/.exec(price);
if (!match) return price;
- const [, currency, amount] = match;
+ const [, currency, rawAmount] = match;
+ // Trailing fraction zeros carry no value; "12.8900000" reads as "12.89".
+ const amount = rawAmount.includes('.')
+ ? rawAmount.replace(/\.?0+$/, '')
+ : rawAmount;
const fractionDigits = amount.split('.')[1]?.length ?? 0;
if (fractionDigits > 100) return price;
@@ -21,10 +25,10 @@ export const formatSalePrice = (price: string): string => {
currency,
minimumFractionDigits: digits,
maximumFractionDigits: digits,
- trailingZeroDisplay: fractionDigits ? 'auto' : 'stripIfInteger',
+ trailingZeroDisplay: 'stripIfInteger',
}).format(amount as Intl.StringNumericLiteral);
} catch {
// Nonstandard currency codes remain readable instead of breaking the tile.
- return price;
+ return `${currency} ${amount}`;
}
};