Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-decimals-kill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aragon/gov-ui-kit': minor
---

Shortening the TOKEN_AMOUNT_LONG formatting style and using it in AssetTransfer
8 changes: 5 additions & 3 deletions src/core/utils/formatterUtils/formatterUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ describe('formatter utils', () => {
test.each([
{ value: -1234.5678, result: '-1,234.5678' },
{ value: -1234.5678, result: '-1.234,5678', locale: 'de' },
{ value: -0.0123456789012345678, result: '-0.012345678901234568' },
{ value: -0.0123456789012345678, result: '-0.012346' },
{ value: -0.0000001234, result: '-<0.000001' },
{ value: 0, result: '0' },
{ value: 0.0000001234, result: '<0.000001' },
{ value: 0.0012, result: '0.0012' },
{ value: 0.0012, result: '+0.0012', withSign: true },
{ value: 0.0123456789012345678, result: '0.012345678901234568' },
{ value: 0.12345678901234567, result: '0.12345678901234566' },
{ value: 0.0123456789012345678, result: '0.012346' },
{ value: 0.12345678901234567, result: '0.123457' },
{ value: 123.4567, result: '123.4567' },
{ value: 1234, result: '1,234' },
{ value: 1234, result: '+1,234', withSign: true },
Expand Down
6 changes: 4 additions & 2 deletions src/core/utils/formatterUtils/formatterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ class FormatterUtils {
isCurrency,
isPercentage,
withSign,
fallback = null,
fallback,
displayFallback,
} = mergedOptions;

const parsedValue = typeof value === 'number' ? value : parseFloat(value ?? '');

const fallbackValue = this.getDynamicOption(parsedValue, fallback);

if (Boolean(displayFallback?.(parsedValue)) || isNaN(parsedValue)) {
return fallback;
return fallbackValue ?? null;
}

let processedValue = isPercentage ? parsedValue * 100 : parsedValue;
Expand Down
9 changes: 6 additions & 3 deletions src/core/utils/formatterUtils/formatterUtilsDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ export interface INumberFormat {
*/
withSign?: boolean;
/**
* Fallback to display in case the value is null.
* Fallback to display in case the value is null. Can be a string or a
* function returning a string based on the current value.
*/
fallback?: string;
fallback?: DynamicOption<number, string>;
/**
* Displays the specified fallback when this function returns true, by default the formatter will display
* the fallback when the value is NaN.
Expand Down Expand Up @@ -84,7 +85,9 @@ export const numberFormats: Record<NumberFormat, INumberFormat> = {
useBaseSymbol: true,
},
[NumberFormat.TOKEN_AMOUNT_LONG]: {
maxFractionDigits: 18,
maxFractionDigits: 6,
fallback: (value) => (value < 0 ? '-<0.000001' : '<0.000001'),
displayFallback: (value) => Math.abs(value) < 0.000001 && value !== 0,
},
[NumberFormat.TOKEN_PRICE]: {
fixedFractionDigits: (value) => (Math.abs(value) >= 1 ? 2 : undefined),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ describe('<AssetTransfer /> component', () => {
expect(screen.getByText('$1.00K')).toBeInTheDocument();
});

it('renders the signed asset amount and symbol', () => {
it('renders the asset amount and symbol', () => {
const assetSymbol = 'ETH';
const assetAmount = 10;
render(createTestComponent({ assetSymbol, assetAmount }));
expect(screen.getByText('+10 ETH')).toBeInTheDocument();
expect(screen.getByText('10 ETH')).toBeInTheDocument();
});

it('renders both avatar elements for the from and to addresses', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export const AssetTransfer: React.FC<IAssetTransferProps> = (props) => {
const assetUrl = isNativeTransfer ? undefined : buildEntityUrl({ type: ChainEntityType.TOKEN, id: assetAddress });

const formattedTokenValue = formatterUtils.formatNumber(assetAmount, {
format: NumberFormat.TOKEN_AMOUNT_SHORT,
withSign: true,
format: NumberFormat.TOKEN_AMOUNT_LONG,
fallback: '-',
})!;
const formattedTokenAmount = `${formattedTokenValue} ${assetSymbol}`;
Expand Down