From 4ae6d4caa94f9734af6cd1d17f0ae6613a01cf39 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 8 Oct 2024 09:40:03 -0700 Subject: [PATCH] Add test for converting small amounts of raw to fractional nano. Fix issue found with said test. --- src/lib/tools.ts | 2 +- test/tools.test.mjs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index fb5c13d..22b0176 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -53,7 +53,7 @@ export async function convert (amount: bigint | string, inputUnit: string, outpu // convert to desired denomination if (UNITS[outputUnit] !== 0) { - f = i.slice(-UNITS[outputUnit]) + f + f = i.padStart(40, '0').slice(-UNITS[outputUnit]) + f i = i.slice(0, -UNITS[outputUnit]) } i = i.replace(/^0*/g, '') ?? '0' diff --git a/test/tools.test.mjs b/test/tools.test.mjs index d2c1b6f..08de50d 100644 --- a/test/tools.test.mjs +++ b/test/tools.test.mjs @@ -23,6 +23,11 @@ describe('unit conversion tests', async () => { assert.equal(result, '1') }) + it('should convert 1 raw to 10^-29 nano', async () => { + const result = await Tools.convert('1', 'RAW', 'NANO') + assert.equal(result, '.000000000000000000000000000001') + }) + it('should ignore leading and trailing zeros', async () => { const result = await Tools.convert('0011002200.0033004400', 'nano', 'nano') assert.equal(result, '11002200.00330044') -- 2.34.1