-
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: unScale unit with native bigint
- Loading branch information
Showing
7 changed files
with
65 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
import { unScale } from './unscale.js' | ||
|
||
test('converts value to number', () => { | ||
expect(unScale(BigInt(69), 0)).toMatchInlineSnapshot('"69"') | ||
expect(unScale(BigInt(69), 5)).toMatchInlineSnapshot('"0.00069"') | ||
expect(unScale(BigInt(690), 1)).toMatchInlineSnapshot('"69"') | ||
expect(unScale(BigInt(1300000), 5)).toMatchInlineSnapshot('"13"') | ||
expect(unScale(BigInt('4200000000000'), 10)).toMatchInlineSnapshot('"420"') | ||
expect(unScale(BigInt('20000000000'), 9)).toMatchInlineSnapshot('"20"') | ||
expect(unScale(BigInt('40000000000000000000'), 18)).toMatchInlineSnapshot('"40"') | ||
expect(unScale(BigInt('10000000000000'), 18)).toMatchInlineSnapshot('"0.00001"') | ||
expect(unScale(BigInt(12345), 4)).toMatchInlineSnapshot('"1.2345"') | ||
expect(unScale(BigInt(12345), 4)).toMatchInlineSnapshot('"1.2345"') | ||
expect(unScale(BigInt('6942069420123456789123450000'), 18)).toMatchInlineSnapshot( | ||
'"6942069420.12345678912345"', | ||
) | ||
expect( | ||
unScale(BigInt('694212312312306942012345444446789123450000000000000000000000000000000'), 50), | ||
).toMatchInlineSnapshot('"6942123123123069420.1234544444678912345"') | ||
// negative values | ||
expect(unScale(BigInt(-690), 1)).toMatchInlineSnapshot('"-69"') | ||
expect(unScale(BigInt(-1300000), 5)).toMatchInlineSnapshot('"-13"') | ||
expect(unScale(BigInt('-4200000000000'), 10)).toMatchInlineSnapshot('"-420"') | ||
expect(unScale(BigInt('-20000000000'), 9)).toMatchInlineSnapshot('"-20"') | ||
expect(unScale(BigInt('-40000000000000000000'), 18)).toMatchInlineSnapshot('"-40"') | ||
expect(unScale(BigInt(-12345), 4)).toMatchInlineSnapshot('"-1.2345"') | ||
expect(unScale(BigInt(-12345), 4)).toMatchInlineSnapshot('"-1.2345"') | ||
expect(unScale(BigInt('-6942069420123456789123450000'), 18)).toMatchInlineSnapshot( | ||
'"-6942069420.12345678912345"', | ||
) | ||
expect( | ||
unScale(BigInt('-694212312312306942012345444446789123450000000000000000000000000000000'), 50), | ||
).toMatchInlineSnapshot('"-6942123123123069420.1234544444678912345"') | ||
}) |
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 @@ | ||
/** | ||
* Divides a number by a given exponent of base 10 (10exponent), and un-scales it into a string representation of the number. | ||
* @returns A string representation of the number | ||
* @example | ||
* unScale(112000000000000000000n, 18) // '112' | ||
* unScale('112000000000000000000', 18) // '112' | ||
*/ | ||
export function unScale(value: bigint | string, decimals: number) { | ||
let display = value.toString() | ||
|
||
const negative = display.startsWith('-') | ||
if (negative) | ||
display = display.slice(1) | ||
|
||
display = display.padStart(decimals, '0') | ||
|
||
const integer = display.slice(0, display.length - decimals) | ||
let fraction = display.slice(display.length - decimals) | ||
fraction = fraction.replace(/(0+)$/, '') | ||
|
||
return `${negative ? '-' : ''}${integer || '0'}${fraction ? `.${fraction}` : ''}` | ||
} |
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