-
Notifications
You must be signed in to change notification settings - Fork 75
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
1 parent
6516fc8
commit 9128666
Showing
2 changed files
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const NB_DECIMALS = 9 | ||
|
||
export type Mas = bigint | ||
|
||
export function fromMas(value: number): Mas { | ||
if (!Number.isSafeInteger(value)) { | ||
throw new Error('value is not a safe integer.') | ||
} | ||
|
||
return BigInt(value * 10 ** NB_DECIMALS) | ||
} | ||
|
||
export function fromMilliMas(value: number): Mas { | ||
if (!Number.isSafeInteger(value)) { | ||
throw new Error('value is not a safe integer.') | ||
} | ||
|
||
return BigInt(value * 10 ** (NB_DECIMALS - 3)) | ||
} | ||
|
||
export function fromMicroMas(value: number): Mas { | ||
if (!Number.isSafeInteger(value)) { | ||
throw new Error('value is not a safe integer.') | ||
} | ||
|
||
return BigInt(value * 10 ** (NB_DECIMALS - 6)) | ||
} | ||
|
||
export function fromNanoMas(value: number): Mas { | ||
if (!Number.isSafeInteger(value)) { | ||
throw new Error('value is not a safe integer.') | ||
} | ||
|
||
return BigInt(value * 10 ** (NB_DECIMALS - 9)) | ||
} | ||
|
||
export function fromString(value: string): Mas { | ||
const [integerPart, decimalPart = ''] = value.split('.') | ||
return BigInt(integerPart + decimalPart.padEnd(NB_DECIMALS, '0')) | ||
} | ||
|
||
export function toString(value: Mas): string { | ||
const valueString = value.toString() | ||
const integerPart = valueString.slice(0, -NB_DECIMALS) || '0' | ||
const decimalPart = valueString.slice(-NB_DECIMALS).replace(/0+$/, '') | ||
return `${integerPart}${decimalPart.length ? '.' + decimalPart : ''}` | ||
} |
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,34 @@ | ||
import * as Mas from '../../../src/experimental/basicElements/mas' | ||
|
||
describe('amount conversion', () => { | ||
it('converts from integer', () => { | ||
expect(Mas.fromMas(1)).toStrictEqual(1_000_000_000n) | ||
expect(Mas.fromMas(1234)).toStrictEqual(1_234_000_000_000n) | ||
expect(() => Mas.fromMas(1.1)).toThrow('value is not a safe integer.') | ||
|
||
expect(Mas.fromMilliMas(1)).toStrictEqual(1_000_000n) | ||
expect(Mas.fromMilliMas(1234)).toStrictEqual(1_234_000_000n) | ||
expect(() => Mas.fromMilliMas(1.1)).toThrow('value is not a safe integer.') | ||
|
||
expect(Mas.fromMicroMas(1)).toStrictEqual(1_000n) | ||
expect(Mas.fromMicroMas(1234)).toStrictEqual(1_234_000n) | ||
expect(() => Mas.fromMicroMas(1.1)).toThrow('value is not a safe integer.') | ||
|
||
expect(Mas.fromNanoMas(1)).toStrictEqual(1n) | ||
expect(Mas.fromNanoMas(1234)).toStrictEqual(1_234n) | ||
expect(() => Mas.fromNanoMas(1.1)).toThrow('value is not a safe integer.') | ||
}) | ||
|
||
it('converts from string', () => { | ||
expect(Mas.fromString('1')).toStrictEqual(1_000_000_000n) | ||
expect(Mas.fromString('1.1')).toStrictEqual(1_100_000_000n) | ||
expect(Mas.fromString('01234.56789')).toStrictEqual(1_234_567_890_000n) | ||
expect(Mas.fromString('01234.567890000')).toStrictEqual(1_234_567_890_000n) | ||
}) | ||
|
||
it('converts to string', () => { | ||
expect(Mas.toString(1_000_000_000n)).toStrictEqual('1') | ||
expect(Mas.toString(1_100_000_000n)).toStrictEqual('1.1') | ||
expect(Mas.toString(1_234_567_890_000n)).toStrictEqual('1234.56789') | ||
}) | ||
}) |
9128666
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for experimental massa-web3
Test suite run success
50 tests passing in 8 suites.
Report generated by 🧪jest coverage report action from 9128666