-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
180 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { formatAmount } from './parseAmount'; | ||
|
||
export default { title: 'Amount/Format' }; | ||
|
||
const inputs = [ | ||
'123456789123456789', | ||
'1234567891234567890000', | ||
'1234567', | ||
'123', | ||
'0', | ||
'', | ||
'1000000000200000000', | ||
'1000000000', | ||
'1000000000000000000', | ||
'1000000000000000000234', | ||
'1000000000000000000000000000000000', | ||
'1000000000000000000000000000000001', | ||
]; | ||
|
||
export const _FormatAmount = { | ||
render: () => { | ||
return ( | ||
<div className="mas-body text-f-primary"> | ||
<p>input: preview / full</p> | ||
{[2, 9, 18].map((decimals) => { | ||
return ( | ||
<> | ||
<h2 className="mas-subtitle"> | ||
Format amount that have {decimals} decimals | ||
</h2> | ||
{inputs.map((input) => { | ||
const { preview, full } = formatAmount(input, decimals); | ||
return ( | ||
<p> | ||
{input}: {preview} / {full} | ||
</p> | ||
); | ||
})} | ||
</> | ||
); | ||
})} | ||
</div> | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,161 +1,156 @@ | ||
import { | ||
formatAmount, | ||
formatStandard, | ||
roundDecimalPartToOneSignificantDigit, | ||
roundDecimalPartToTwoSignificantDigit, | ||
} from './parseAmount'; | ||
|
||
describe('formatAmount', () => { | ||
test('formats an empty string', () => { | ||
const result = formatAmount('', 18); | ||
expect(result).toEqual({ | ||
amountFormattedPreview: '0.0', | ||
amountFormattedFull: '0.000000000000000000', | ||
preview: '0', | ||
full: '0', | ||
}); | ||
}); | ||
|
||
test('formats an amount with default parameters', () => { | ||
const result = formatAmount('123456789012345678901', 18); | ||
expect(result).toEqual({ | ||
amountFormattedPreview: '123.46', | ||
amountFormattedFull: '123.456789012345678901', | ||
preview: '123.46', | ||
full: '123.456789012345678901', | ||
}); | ||
}); | ||
|
||
test('formats an amount with BigInt', () => { | ||
const result = formatAmount(123456789012345678901n, 18); | ||
expect(result).toEqual({ | ||
preview: '123.46', | ||
full: '123.456789012345678901', | ||
}); | ||
}); | ||
|
||
test('formats an amount with less than the specified decimals', () => { | ||
const result = formatAmount('12345', 8); | ||
expect(result).toEqual({ | ||
amountFormattedPreview: '0.0001', | ||
amountFormattedFull: '0.00012345', | ||
preview: '0.00012', | ||
full: '0.00012345', | ||
}); | ||
}); | ||
|
||
test('formats an amount with custom separator', () => { | ||
const result = formatAmount('123456789012345678901', 9, "'"); | ||
expect(result).toEqual({ | ||
amountFormattedPreview: "123'456'789'012.35", | ||
amountFormattedFull: "123'456'789'012.345678901", | ||
preview: "123'456'789'012.35", | ||
full: "123'456'789'012.345678901", | ||
}); | ||
}); | ||
|
||
test('adds padding zeroes when necessary', () => { | ||
const result = formatAmount('1', 18, ','); | ||
expect(result).toEqual({ | ||
amountFormattedPreview: '0.000000000000000001', | ||
amountFormattedFull: '0.000000000000000001', | ||
preview: '0.000000000000000001', | ||
full: '0.000000000000000001', | ||
}); | ||
}); | ||
|
||
test('handles amount with exact decimals length', () => { | ||
const result = formatAmount('1000000000000000000', 18); | ||
expect(result).toEqual({ | ||
amountFormattedPreview: '1.00', | ||
amountFormattedFull: '1.000000000000000000', | ||
preview: '1', | ||
full: '1', | ||
}); | ||
}); | ||
|
||
test('formats an amount with less than the specified decimals and round up', () => { | ||
const result = formatAmount('69000', 9); | ||
const result = formatAmount('69500', 9); | ||
expect(result).toEqual({ | ||
amountFormattedPreview: '0.00007', | ||
amountFormattedFull: '0.000069000', | ||
preview: '0.00007', | ||
full: '0.0000695', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('roundDecimalPartToOneSignificantDigit', () => { | ||
test("should return '0' when all digits are zero", () => { | ||
expect(roundDecimalPartToOneSignificantDigit('000')).toEqual('0'); | ||
}); | ||
|
||
test('should handle a single zero without leading zeroes', () => { | ||
expect(roundDecimalPartToOneSignificantDigit('0')).toEqual('0'); | ||
}); | ||
|
||
test('should handle a single digit without rounding', () => { | ||
expect(roundDecimalPartToOneSignificantDigit('4')).toEqual('4'); | ||
}); | ||
|
||
test('should round down when the second digit is less than 5', () => { | ||
expect(roundDecimalPartToOneSignificantDigit('004300')).toEqual('004'); | ||
}); | ||
|
||
test('should round up when the second digit is 5 or more', () => { | ||
expect(roundDecimalPartToOneSignificantDigit('00046')).toEqual('0005'); | ||
}); | ||
|
||
test('should round up and handle carry-over with trailing zeroes', () => { | ||
expect(roundDecimalPartToOneSignificantDigit('0099')).toEqual('01'); | ||
}); | ||
}); | ||
|
||
describe('formatStandard', () => { | ||
test('formats an empty string', () => { | ||
const result = formatStandard('', 18); | ||
const result = formatAmount('', 18).full; | ||
expect(result).toEqual('0'); | ||
}); | ||
|
||
test('formats an amount with default parameters', () => { | ||
const result = formatStandard('123456789012345678901', 18); | ||
const result = formatAmount('123456789012345678901', 18).full; | ||
expect(result).toEqual('123.456789012345678901'); | ||
}); | ||
|
||
test('formats an amount with less than the specified decimals', () => { | ||
const result = formatStandard('12345', 8); | ||
const result = formatAmount('12345', 8).full; | ||
expect(result).toEqual('0.00012345'); | ||
}); | ||
|
||
test('adds padding zeroes when necessary', () => { | ||
const result = formatStandard('1', 18); | ||
const result = formatAmount('1', 18).full; | ||
expect(result).toEqual('0.000000000000000001'); | ||
}); | ||
|
||
test('handles amount with exact decimals length', () => { | ||
const result = formatStandard('1000000000000000000', 18); | ||
const result = formatAmount('1000000000000000000', 18).full; | ||
expect(result).toEqual('1'); | ||
}); | ||
|
||
test('formats an amount with less than the specified decimals and round up', () => { | ||
const result = formatStandard('69000', 9); | ||
const result = formatAmount('69000', 9).full; | ||
expect(result).toEqual('0.000069'); | ||
}); | ||
|
||
it('formatStandard with min string value', () => { | ||
it('formatAmount with min string value', () => { | ||
const value = '0000000000'; | ||
|
||
const result = formatStandard(value.toString()); | ||
|
||
const result = formatAmount(value.toString()).full; | ||
expect(result).toBe('0'); | ||
}); | ||
|
||
it('formatStandard with min bigint value', () => { | ||
it('formatAmount with min bigint value', () => { | ||
const value = 0n; | ||
|
||
const result = formatStandard(value.toString()); | ||
|
||
const result = formatAmount(value.toString()).full; | ||
expect(result).toBe('0'); | ||
}); | ||
|
||
it('formatStandard with mid range string value', () => { | ||
it('formatAmount with mid range string value', () => { | ||
const value = '10000000000000'; | ||
|
||
const result = formatStandard(value.toString()); | ||
|
||
const result = formatAmount(value.toString()).full; | ||
expect(result).toBe('10,000'); | ||
}); | ||
|
||
it('formatStandard with mid range bigint value', () => { | ||
it('formatAmount with mid range bigint value', () => { | ||
const value = 10000000000000n; | ||
|
||
const result = formatStandard(value.toString()); | ||
|
||
const result = formatAmount(value.toString()).full; | ||
expect(result).toBe('10,000'); | ||
}); | ||
|
||
it('formatStandard with max string value', () => { | ||
it('formatAmount with max string value', () => { | ||
const value = '922337203600000000000'; | ||
const result = formatAmount(value.toString()).full; | ||
expect(result).toBe('922,337,203,600'); | ||
}); | ||
}); | ||
|
||
const result = formatStandard(value.toString()); | ||
describe('roundDecimalPartToOneSignificantDigit', () => { | ||
test("should return '0' when all digits are zero", () => { | ||
expect(roundDecimalPartToTwoSignificantDigit('000')).toEqual('0'); | ||
}); | ||
|
||
expect(result).toBe('922,337,203,600'); | ||
test('should handle a single zero without leading zeroes', () => { | ||
expect(roundDecimalPartToTwoSignificantDigit('0')).toEqual('0'); | ||
}); | ||
|
||
test('should handle a single digit without rounding', () => { | ||
expect(roundDecimalPartToTwoSignificantDigit('4')).toEqual('4'); | ||
}); | ||
|
||
test('should round down when the second digit is less than 5', () => { | ||
expect(roundDecimalPartToTwoSignificantDigit('0043100')).toEqual('0043'); | ||
}); | ||
|
||
test('should round up when the second digit is 5 or more', () => { | ||
expect(roundDecimalPartToTwoSignificantDigit('000468')).toEqual('00047'); | ||
}); | ||
|
||
test('should round up and handle carry-over with trailing zeroes', () => { | ||
expect(roundDecimalPartToTwoSignificantDigit('0099')).toEqual('0099'); | ||
}); | ||
}); |
Oops, something went wrong.