-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: formatAmount with bigint and better property names
- Loading branch information
Showing
5 changed files
with
58 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
import { formatAmount } from './formatter' | ||
|
||
test('calculate unit price from units and total price', () => { | ||
expect(formatAmount('10000000', 6)).toMatchObject({ base: BigInt(10000000), display: '10', formatted: '10' }) | ||
expect(formatAmount('1110000', 6, 0)).toMatchObject({ base: BigInt(1110000), display: '1', formatted: '1.11' }) | ||
expect(formatAmount('0', 6, 1)).toMatchObject({ base: BigInt(0), display: '0', formatted: '0' }) | ||
expect(formatAmount('', 6, 1)).toMatchObject({ base: BigInt(0), display: '0', formatted: '0' }) | ||
expect(formatAmount('1110000', 6)) | ||
.toMatchObject({ base: BigInt(1110000), display: '1.11', formatted: '1.11' }) | ||
expect(formatAmount('1110000', 6, 1)) | ||
.toMatchObject({ base: BigInt(1110000), display: '1.1', formatted: '1.11' }) | ||
expect(formatAmount('123456789', 8, 3)) | ||
.toMatchObject({ base: BigInt(123456789), display: '1.234', formatted: '1.23456789' }) | ||
expect(formatAmount('12345678900223', 6, 3)) | ||
.toMatchObject({ base: BigInt(12345678900223), display: '12,345,678.9', formatted: '12345678.900223' }) | ||
|
||
// large numbers | ||
expect(formatAmount('1234567890123456789012345678901234567890', 30, 3)) | ||
.toMatchObject({ | ||
base: BigInt('1234567890123456789012345678901234567890'), | ||
display: '1,234,567,890.123', | ||
formatted: '1234567890.12345678901234567890123456789', | ||
}) | ||
expect(formatAmount('1234567890123456789', 15, 2)) | ||
.toMatchObject({ | ||
base: BigInt('1234567890123456789'), | ||
display: '1,234.56', | ||
formatted: '1234.567890123456789', | ||
}) | ||
}) |
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,22 @@ | ||
import { unScale } from './unscale' | ||
|
||
export interface FormattedAmount { | ||
base: bigint | ||
display: string | ||
formatted: string | ||
} | ||
|
||
/** | ||
* un-scales unit with give decimals and returns | ||
* @returns base: original value, display: unScaled value with 0-3 decimals with comma, formatted: unScaled value with all decimals | ||
* @example | ||
* getFormattedAmount('12345678900223', 6, 3) // { base: 12345678900223n, display: '12,345,678.9', formatted: '12345678.900223' } | ||
*/ | ||
export function formatAmount(value: bigint | string, decimals: number, displayDecimals: 0 | 1 | 2 | 3 = 3) { | ||
const valueUnScaled = unScale(value, decimals) | ||
return { | ||
base: BigInt(value), | ||
display: parseFloat(valueUnScaled.slice(0, valueUnScaled.indexOf('.') + displayDecimals + 1)).toLocaleString(), | ||
formatted: valueUnScaled, | ||
} | ||
} |
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