diff --git a/src/utils.ts b/src/utils.ts index 29851ae..d1e273a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -244,17 +244,17 @@ function toFixedPoint( } else { fractional = roundPrecise(floating % 1, precision); } - // if the fractional is rounded to 1 + // If the fractional is rounded to 1 // then it should be added to the integer if (fractional === 1) { integer += fractional; fractional = 0; } - // floor is used to round down to a number that can be represented by the bit size + // Floor is used to round down to a number that can be represented by the bit size // if ceil or round was used, it's possible to return a number that would overflow the bit size // for example if 12 bits is used, then 4096 would overflow to all zeros // the maximum for 12 bit is 4095 - const fractionalFixed = Math.floor(fractional * (2 ** size)); + const fractionalFixed = Math.floor(fractional * 2 ** size); return [integer, fractionalFixed]; } @@ -272,7 +272,7 @@ function fromFixedPoint( if (precision == null) { fractional = fractionalFixed / 2 ** size; } else { - fractional = roundPrecise(fractionalFixed / (2 ** size), precision); + fractional = roundPrecise(fractionalFixed / 2 ** size, precision); } return integer + fractional; } diff --git a/tests/IdSortable.test.ts b/tests/IdSortable.test.ts index a8242f1..78b3c9b 100644 --- a/tests/IdSortable.test.ts +++ b/tests/IdSortable.test.ts @@ -102,13 +102,15 @@ describe('IdSortable', () => { } }); test('encoded multibase strings may be lexically sortable (base58btc)', () => { - // base58btc's alphabet preserves sort order + // Base58btc's alphabet preserves sort order const idGen = new IdSortable(); // This generating over 100,000 ids and checks that they maintain // sort order for each 100 chunk of ids let count = 1000; while (count > 0) { - const idStrings = [...utils.take(idGen, 100)].map((id) => utils.toMultibase(id, 'base58btc')); + const idStrings = [...utils.take(idGen, 100)].map((id) => + utils.toMultibase(id, 'base58btc'), + ); const idStringsShuffled = idStrings.slice(); shuffle(idStringsShuffled); idStringsShuffled.sort(); @@ -117,13 +119,15 @@ describe('IdSortable', () => { } }); test('encoded multibase strings may not be lexically sortable (base64)', async () => { - // base64's alphabet does not preserve sort order + // Base64's alphabet does not preserve sort order const idGen = new IdSortable(); - const idStrings = [...utils.take(idGen, 100)].map((id) => utils.toMultibase(id, 'base64')); + const idStrings = [...utils.take(idGen, 100)].map((id) => + utils.toMultibase(id, 'base64'), + ); const idStringsShuffled = idStrings.slice(); shuffle(idStringsShuffled); idStringsShuffled.sort(); - // it will not equal + // It will not equal expect(idStringsShuffled).not.toStrictEqual(idStrings); }); test('ids are monotonic within the same timestamp', () => { diff --git a/tests/utils.test.ts b/tests/utils.test.ts index b6fc9f7..41fe9c0 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -100,39 +100,39 @@ describe('utils', () => { expect(utils.fromFixedPoint([0, 0], 12, 3)).toBe(0.0); }); test('fixed point conversion when close to 1', () => { - // highest number 12 digits can represent is 4095 + // Highest number 12 digits can represent is 4095 // a number of 4096 would result in overflow // here we test when we get close to 4096 // the conversion from toFixedPoint and fromFixedPoint will be lossy // this is because toFixedPoint will round off precision and floor any number getting close to 4096 let closeTo: number; let fp: [number, number]; - // exactly at 3 decimal points + // Exactly at 3 decimal points closeTo = 0.999; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 4091]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.999); - // will round below + // Will round below closeTo = 0.9994; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 4091]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.999); - // will round above to 1 + // Will round above to 1 closeTo = 0.9995; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([1, 0]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(1); - // will round above to 1 + // Will round above to 1 closeTo = 0.9999; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([1, 0]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(1); - // will round above to 1 + // Will round above to 1 closeTo = 0.99999; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([1, 0]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(1); - // exactly at 5 decimal points + // Exactly at 5 decimal points closeTo = 0.99999; fp = utils.toFixedPoint(closeTo, 12, 5); expect(fp).toStrictEqual([0, 4095]); @@ -141,37 +141,37 @@ describe('utils', () => { test('fixed point conversion when close to 0', () => { let closeTo: number; let fp: [number, number]; - // exactly 3 decimal places + // Exactly 3 decimal places closeTo = 0.001; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 4]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.001); - // will round to 0 + // Will round to 0 closeTo = 0.0001; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 0]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0); - // will round to 0 + // Will round to 0 closeTo = 0.0004; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 0]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0); - // will round to 0.001 + // Will round to 0.001 closeTo = 0.0005; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 4]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.001); - // will round to 0.001 + // Will round to 0.001 closeTo = 0.00055; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 4]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.001); - // will round to 0.001 + // Will round to 0.001 closeTo = 0.0009; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 4]); expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.001); - // will round to 0.002 + // Will round to 0.002 closeTo = 0.0015; fp = utils.toFixedPoint(closeTo, 12, 3); expect(fp).toStrictEqual([0, 8]);