Skip to content

Commit

Permalink
Merge pull request #288 from yearn/feat/extract-amountV2
Browse files Browse the repository at this point in the history
feat(amountV2): Move amountV2 from yearn.fi
  • Loading branch information
Majorfi authored Jul 11, 2023
2 parents a6d22eb + c17e792 commit 6fe9116
Show file tree
Hide file tree
Showing 2 changed files with 573 additions and 1 deletion.
376 changes: 375 additions & 1 deletion packages/web-lib/utils/format.number.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {amount, formatNumberOver10K} from './format.number';
import {amount,
amountV2,
assertValidNumber,
defaultOptions,
formatLocalAmount,
formatNumberOver10K} from './format.number';

describe('format.number', (): void => {
describe('amount()', (): void => {
Expand Down Expand Up @@ -74,4 +79,373 @@ describe('format.number', (): void => {
expect(formatNumberOver10K(0)).toBe('0,00');
});
});

describe('assertValidNumber', (): void => {
it('Undefined should fallback to default value', (): void => {
expect(assertValidNumber(undefined, 22, '')).toBe(22);
});
it('Negative value should fallback to default value', (): void => {
expect(assertValidNumber(-5, 10, '')).toBe(10);
});
it('Value greater than 18 should be limited to 18', (): void => {
expect(assertValidNumber(20, 10, '')).toBe(18);
});
it('NaN value should fallback to default value', (): void => {
expect(assertValidNumber(NaN, 10, '')).toBe(10);
});
it('Non-integer value should fallback to default value', (): void => {
expect(assertValidNumber(10.5, 10, '')).toBe(10);
});
it('Valid value should be returned as is', (): void => {
expect(assertValidNumber(15, 10, '')).toBe(15);
});
it('Undefined should fallback to default value (0)', (): void => {
expect(assertValidNumber(undefined, 0, '')).toBe(0);
});
it('Zero value should be returned as is', (): void => {
expect(assertValidNumber(0, 10, '')).toBe(0);
});
it('Value equal to the upper limit (18) should be returned as is', (): void => {
expect(assertValidNumber(18, 10, '')).toBe(18);
});
it('Floating-point value within the range should fallback', (): void => {
expect(assertValidNumber(17.5, 18, '')).toBe(18);
});
it('Value greater than 18 should be limited to 18', (): void => {
expect(assertValidNumber(25, 10, '')).toBe(18);
});
it('Invalid value should fallback to default value', (): void => {
expect(assertValidNumber('abc' as any, 10, '')).toBe(10);
});
it('Non-integer value should fallback to default value', (): void => {
expect(assertValidNumber(10.7, 10, '')).toBe(10);
});
it('Floating-point value with zero decimal should be returned as is', (): void => {
expect(assertValidNumber(10.0, 10, '')).toBe(10);
});
it('Negative value should fallback to default value', (): void => {
expect(assertValidNumber(-20, 10, '')).toBe(10);
});
it('Value within the range should be returned as is', (): void => {
expect(assertValidNumber(3, 10, '')).toBe(3);
});
it('Value within the range should be returned as is', (): void => {
expect(assertValidNumber(7, 10, '')).toBe(7);
});
it('Value within the range should be returned as is', (): void => {
expect(assertValidNumber(13, 10, '')).toBe(13);
});
it('Value equal to the upper limit (18) should be returned as is', (): void => {
expect(assertValidNumber(18, 10, '')).toBe(18);
});
it('Negative value should fallback to default value', (): void => {
expect(assertValidNumber(-1, 7, '')).toBe(7);
});
it('Floating-point value within the range should fallback', (): void => {
expect(assertValidNumber(1.5, 10, '')).toBe(10);
});
});

describe('formatLocalAmount', (): void => {
it('Currency symbol should be displayed', (): void => {
expect(
formatLocalAmount(1234.5678, 2, '$', defaultOptions).normalize(
'NFKC'
)).toBe(
'1 234,57 $');

}
);

it('Currency symbol should be displayed', (): void => {
expect(
formatLocalAmount(1234.5678, 2, '$', defaultOptions).normalize(
'NFKC'
)).toBe(
'1 234,57 $');

}
);

it('USD currency symbol should be displayed', (): void => {
expect(
formatLocalAmount(
1234.5678,
2,
'USD',
defaultOptions
).normalize('NFKC')).toBe(
'1 234,57 $');

}
);

it('DAI currency symbol should be displayed', (): void => {
expect(
formatLocalAmount(
1234.5678,
2,
'DAI',
defaultOptions
).normalize('NFKC')).toBe(
'1 234,57 DAI');

}
);

it('Percent symbol should be displayed', (): void => {
expect(
formatLocalAmount(
0.123,
2,
'PERCENT',
defaultOptions
).normalize('NFKC')).toBe(
'12,30 %');

}
);
it('Empty symbol should not affect formatting', (): void => {
expect(
formatLocalAmount(1234.5678, 2, '', defaultOptions).normalize(
'NFKC'
)).toBe(
'1 234,57');
});
it('Empty symbol should not affect formatting', (): void => {
expect(
formatLocalAmount(1234.5678, 2, '', {
shouldDisplaySymbol: false
}).normalize('NFKC')).toBe(
'1 234,568');

}
);
it('Amount should be formatted in short notation', (): void => {
expect(
formatLocalAmount(12345.6789, 2, '$', defaultOptions).normalize(
'NFKC'
)).toBe(
'12,35 k $');

}
);
it('Amount should be formatted in short notation', (): void => {
expect(
formatLocalAmount(
12345678.9,
2,
'USD',
defaultOptions
).normalize('NFKC')).toBe(
'12,35 M $');

}
);
it('Amount should be formatted with the specified number of decimals', (): void => {
expect(
formatLocalAmount(0.00000123, 8, '$', defaultOptions).normalize(
'NFKC'
)).toBe(
'0,00000123 $');

}
);
it('Amount should be formatted with the specified number of decimals', (): void => {
expect(
formatLocalAmount(
0.00000000123,
12,
'USD',
defaultOptions
).normalize('NFKC')).toBe(
'0,00000000123 $');

}
);
it('Amount above 0.01 should be formatted as is', (): void => {
expect(
formatLocalAmount(0.01, 2, 'USD', defaultOptions).normalize(
'NFKC'
)).toBe(
'0,01 $');

}
);
it('Amount above 0.01 should be formatted as is', (): void => {
expect(
formatLocalAmount(0.001, 2, 'OPT', defaultOptions).normalize(
'NFKC'
)).toBe(
'0,001 OPT');

}
);
it('Amount should be formatted with the specified number of decimals', (): void => {
expect(
formatLocalAmount(
0.000000000000123,
18,
'YFI',
defaultOptions
).normalize('NFKC')).toBe(
'0,000000000000123 YFI');

}
);
it("Amount should be 0,00 YFI when it's too small", (): void => {
expect(
formatLocalAmount(
0.000000000000000000123,
18,
'YFI',
defaultOptions
).normalize('NFKC')).toBe(
'0,00 YFI');

}
);
});

describe('is ok for amountV2', (): void => {
it('Formatted amount with decimal places and currency symbol should be returned', (): void => {
expect(
amountV2({
value: 1234.5678,
decimals: 2,
symbol: '$',
options: defaultOptions
}).normalize('NFKC')).toBe(
'1 234,57 $');

}
);
it('Formatted amount with decimal places and USD currency symbol should be returned', (): void => {
expect(
amountV2({
value: 1234.5678,
decimals: 2,
symbol: 'USD',
options: defaultOptions
}).normalize('NFKC')).toBe(
'1 234,57 $');

}
);
it('Formatted amount with decimal places and EUR currency symbol should be returned', (): void => {
expect(
amountV2({
value: 1234.5678,
decimals: 2,
symbol: 'DAI',
options: defaultOptions
}).normalize('NFKC')).toBe(
'1 234,57 DAI');

}
);
it('Formatted amount with decimal places and no currency symbol should be returned', (): void => {
expect(
amountV2({
value: 1234.5678,
decimals: 2,
symbol: 'USD',
options: {shouldDisplaySymbol: false}
}).normalize('NFKC')).toBe(
'1 234,57');

}
);
it('Formatted amount with decimal places and percent symbol should be returned', (): void => {
expect(
amountV2({
value: 1234.5678,
decimals: 2,
symbol: 'PERCENT',
options: defaultOptions
}).normalize('NFKC')).toBe(
'> 500,00 %');

}
);
it('Formatted amount with decimal places and no percent symbol should be returned', (): void => {
expect(
amountV2({
value: 1234.5678,
decimals: 2,
symbol: 'PERCENT',
options: {shouldDisplaySymbol: false}
}).normalize('NFKC')).toBe(
'1 234,57');

}
);
it('Formatted zero amount with no symbol should be returned', (): void => {
expect(
amountV2({
value: 0,
decimals: 2,
symbol: '',
options: defaultOptions
}).normalize('NFKC')).toBe(
'0,00');

}
);
it('Formatted infinity amount should be returned', (): void => {
expect(
amountV2({
value: Infinity,
decimals: 2,
symbol: '$',
options: defaultOptions
}).normalize('NFKC')).toBe(
'∞');

}
);
it('Formatted BigInt amount with unit should be returned', (): void => {
expect(
amountV2({value: BigInt(123456789), decimals: 2, symbol: '$', options: defaultOptions}).normalize('NFKC')).toBe(
'1,23 M $');

}
);
it('Formatted NaN amount should return infinity', (): void => {
expect(
amountV2({
value: NaN,
decimals: 2,
symbol: '$',
options: defaultOptions
}).normalize('NFKC')).toBe(
'0,00 $');

}
);
it('Formatted small amount with decimal places and no percent symbol should be returned', (): void => {
expect(
amountV2({
value: 0.0000000012345678,
decimals: 2,
symbol: 'PERCENT',
options: {shouldDisplaySymbol: false}
}).normalize('NFKC')).toBe(
'0,000000001235');

}
);
it('Format small amount to 0,00 and no percent symbol should be returned', (): void => {
expect(
amountV2({
value: 0.000000000000012345678,
decimals: 2,
symbol: 'PERCENT',
options: {shouldDisplaySymbol: false}
}).normalize('NFKC')).toBe(
'0,00');

}
);
});
});
Loading

0 comments on commit 6fe9116

Please sign in to comment.