From e30bac1ed69d1d9880709541409f0761b5589e76 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sat, 11 Jun 2022 16:28:55 +0200 Subject: [PATCH] Test for sqrt(MAX_SAFE_INT) (w/ comments for excluded functions) (#1526) * Test for sqrt(MAX_SAFE_INT) (w/ comments for excluded functions) * Adjust {bi, bn}{Max, Min} with factory --- packages/util/src/array/shuffle.ts | 4 ++++ packages/util/src/bi/min.ts | 29 ++++++++++++----------- packages/util/src/bi/sqrt.spec.ts | 15 +++++++++++- packages/util/src/bi/sqrt.ts | 5 ++-- packages/util/src/bi/toHex.ts | 1 + packages/util/src/bn/min.ts | 25 ++++++++++--------- packages/util/src/bn/sqrt.spec.ts | 17 ++++++++++++- packages/util/src/bn/sqrt.ts | 3 ++- packages/util/src/compact/defaults.ts | 1 + packages/util/src/format/formatDate.ts | 4 ++++ packages/util/src/format/formatDecimal.ts | 4 ++++ packages/util/src/format/formatElapsed.ts | 5 ++++ packages/util/src/format/formatNumber.ts | 4 ++++ packages/util/src/format/si.ts | 4 ++++ packages/util/src/is/ascii.ts | 1 + 15 files changed, 90 insertions(+), 32 deletions(-) diff --git a/packages/util/src/array/shuffle.ts b/packages/util/src/array/shuffle.ts index f6e2e5c44..e6838455c 100644 --- a/packages/util/src/array/shuffle.ts +++ b/packages/util/src/array/shuffle.ts @@ -1,6 +1,10 @@ // Copyright 2017-2022 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +/** + * @name arrayShuffle + * @description Shuffles the input array (unlike sort, this is not done in-place) + */ export function arrayShuffle (input: T[]): T[] { const result = input.slice(); let curr = result.length; diff --git a/packages/util/src/bi/min.ts b/packages/util/src/bi/min.ts index 6f6c47686..50092da37 100644 --- a/packages/util/src/bi/min.ts +++ b/packages/util/src/bi/min.ts @@ -3,40 +3,41 @@ import { assert } from '../assert'; +/** @internal */ function gt (a: bigint, b: bigint): boolean { return a > b; } +/** @internal */ function lt (a: bigint, b: bigint): boolean { return a < b; } -function find (items: bigint[], cmp: (a: bigint, b: bigint) => boolean): bigint { - assert(items.length >= 1, 'Must provide one or more bigint arguments'); +/** @internal */ +function createCmp (cmp: (a: bigint, b: bigint) => boolean): (...items: bigint[]) => bigint { + return (...items: bigint[]): bigint => { + assert(items.length >= 1, 'Must provide one or more bigint arguments'); - let result = items[0]; + let result = items[0]; - for (let i = 1; i < items.length; i++) { - if (cmp(items[i], result)) { - result = items[i]; + for (let i = 1; i < items.length; i++) { + if (cmp(items[i], result)) { + result = items[i]; + } } - } - return result; + return result; + }; } /** * @name nMax * @summary Finds and returns the highest value in an array of bigint. */ -export function nMax (...items: bigint[]): bigint { - return find(items, gt); -} +export const nMax = createCmp(gt); /** * @name nMin * @summary Finds and returns the lowest value in an array of bigint. */ -export function nMin (...items: bigint[]): bigint { - return find(items, lt); -} +export const nMin = createCmp(lt); diff --git a/packages/util/src/bi/sqrt.spec.ts b/packages/util/src/bi/sqrt.spec.ts index 41995fd3f..c83f4f534 100644 --- a/packages/util/src/bi/sqrt.spec.ts +++ b/packages/util/src/bi/sqrt.spec.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { BN } from '../bn'; +import { SQRT_MAX_SAFE_INTEGER } from './sqrt'; import { nSqrt } from '.'; const TESTS: [string | number | BN | bigint, string | number][] = [ @@ -34,9 +35,21 @@ describe('nSqrt', (): void => { ).toThrow(/negative numbers is not supported/); }); + it('has the correct constant for sqrt(Number.MAX_SAFE_INTEGER)', (): void => { + expect( + BigInt( + ~~Math.sqrt( + Number.MAX_SAFE_INTEGER + ) + ) === SQRT_MAX_SAFE_INTEGER + ).toEqual(true); + }); + TESTS.forEach(([value, expected], index): void => { it(`calcs for test #${index}`, (): void => { - expect(nSqrt(value) === BigInt(expected)).toEqual(true); + expect( + nSqrt(value) === BigInt(expected) + ).toEqual(true); }); }); }); diff --git a/packages/util/src/bi/sqrt.ts b/packages/util/src/bi/sqrt.ts index 456739bd3..bbb5d3170 100644 --- a/packages/util/src/bi/sqrt.ts +++ b/packages/util/src/bi/sqrt.ts @@ -10,7 +10,8 @@ import { assert } from '../assert'; import { _0n, _1n, _2pow53n } from './consts'; import { nToBigInt } from './toBigInt'; -const _sqrt2pow53n = BigInt(94906265); +/** @internal */ +export const SQRT_MAX_SAFE_INTEGER = BigInt(94906265); /** * @name nSqrt @@ -30,7 +31,7 @@ export function nSqrt (value: ExtToBn | BN | b // Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the // output will be larger than this, we expect this to be a safe start - let x0 = _sqrt2pow53n; + let x0 = SQRT_MAX_SAFE_INTEGER; while (true) { const x1 = ((n / x0) + x0) >> _1n; diff --git a/packages/util/src/bi/toHex.ts b/packages/util/src/bi/toHex.ts index eecbfe73a..08cc8eb1a 100644 --- a/packages/util/src/bi/toHex.ts +++ b/packages/util/src/bi/toHex.ts @@ -8,6 +8,7 @@ import { objectSpread } from '../object/spread'; import { u8aToHex } from '../u8a'; import { nToU8a } from './toU8a'; +/** @internal */ const ZERO_STR = '0x00'; /** diff --git a/packages/util/src/bn/min.ts b/packages/util/src/bn/min.ts index 7dcfaea5d..a8f40ba81 100644 --- a/packages/util/src/bn/min.ts +++ b/packages/util/src/bn/min.ts @@ -4,16 +4,19 @@ import { assert } from '../assert'; import { BN } from './bn'; -export function find (items: BN[], cmp: (a: BN, b: BN) => BN): BN { - assert(items.length >= 1, 'Must provide one or more BN arguments'); +/** @internal */ +function createCmp (cmp: (a: BN, b: BN) => BN): (...items: BN[]) => BN { + return (...items: BN[]): BN => { + assert(items.length >= 1, 'Must provide one or more BN arguments'); - let result = items[0]; + let result = items[0]; - for (let i = 1; i < items.length; i++) { - result = cmp(result, items[i]); - } + for (let i = 1; i < items.length; i++) { + result = cmp(result, items[i]); + } - return result; + return result; + }; } /** @@ -29,9 +32,7 @@ export function find (items: BN[], cmp: (a: BN, b: BN) => BN): BN { * bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3' * ``` */ -export function bnMax (...items: BN[]): BN { - return find(items, BN.max); -} +export const bnMax = createCmp(BN.max); /** * @name bnMin @@ -46,6 +47,4 @@ export function bnMax (...items: BN[]): BN { * bnMin([new BN(1), new BN(3), new BN(2)]).toString(); // => '1' * ``` */ -export function bnMin (...items: BN[]): BN { - return find(items, BN.min); -} +export const bnMin = createCmp(BN.min); diff --git a/packages/util/src/bn/sqrt.spec.ts b/packages/util/src/bn/sqrt.spec.ts index 05dfcabc5..fd1797e57 100644 --- a/packages/util/src/bn/sqrt.spec.ts +++ b/packages/util/src/bn/sqrt.spec.ts @@ -1,6 +1,7 @@ // Copyright 2017-2022 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { SQRT_MAX_SAFE_INTEGER } from './sqrt'; import { BN, bnSqrt } from '.'; const TESTS: [string | number | BN | bigint, string | number][] = [ @@ -33,9 +34,23 @@ describe('bnSqrt', (): void => { ).toThrow(/negative numbers is not supported/); }); + it('has the correct constant for sqrt(Number.MAX_SAFE_INTEGER)', (): void => { + expect( + SQRT_MAX_SAFE_INTEGER.eq( + new BN( + ~~Math.sqrt( + Number.MAX_SAFE_INTEGER + ) + ) + ) + ).toEqual(true); + }); + TESTS.forEach(([value, expected], index): void => { it(`calcs for test #${index}`, (): void => { - expect(bnSqrt(value).eq(new BN(expected))).toEqual(true); + expect( + bnSqrt(value).eq(new BN(expected)) + ).toEqual(true); }); }); }); diff --git a/packages/util/src/bn/sqrt.ts b/packages/util/src/bn/sqrt.ts index b55080a81..064d81616 100644 --- a/packages/util/src/bn/sqrt.ts +++ b/packages/util/src/bn/sqrt.ts @@ -8,7 +8,8 @@ import { BN } from './bn'; import { BN_MAX_INTEGER, BN_ONE, BN_ZERO } from './consts'; import { bnToBn } from './toBn'; -const SQRT_MAX_SAFE_INTEGER = new BN(94906265); +/** @internal */ +export const SQRT_MAX_SAFE_INTEGER = new BN(94906265); /** * @name bnSqrt diff --git a/packages/util/src/compact/defaults.ts b/packages/util/src/compact/defaults.ts index 5eb0206f4..a153b444c 100644 --- a/packages/util/src/compact/defaults.ts +++ b/packages/util/src/compact/defaults.ts @@ -3,4 +3,5 @@ import type { BitLength } from './types'; +/** @internal */ export const DEFAULT_BITLENGTH: BitLength = 32; diff --git a/packages/util/src/format/formatDate.ts b/packages/util/src/format/formatDate.ts index 20ff6fdf2..c78164dff 100644 --- a/packages/util/src/format/formatDate.ts +++ b/packages/util/src/format/formatDate.ts @@ -6,6 +6,10 @@ function zeroPad (value: number): string { return value.toString().padStart(2, '0'); } +/** + * @name formatDate + * @description Formats a date in CCYY-MM-DD HH:MM:SS format + */ export function formatDate (date: Date): string { const year = date.getFullYear().toString(); const month = zeroPad((date.getMonth() + 1)); diff --git a/packages/util/src/format/formatDecimal.ts b/packages/util/src/format/formatDecimal.ts index 09d1c0481..0cbcbd3b9 100644 --- a/packages/util/src/format/formatDecimal.ts +++ b/packages/util/src/format/formatDecimal.ts @@ -4,6 +4,10 @@ // eslint-disable-next-line prefer-regex-literals const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g'); +/** + * @name formatDecimal + * @description Formats a number into string format with thousand seperators + */ export function formatDecimal (value: string): string { // We can do this by adjusting the regx, however for the sake of clarity // we rather strip and re-add the negative sign in the output diff --git a/packages/util/src/format/formatElapsed.ts b/packages/util/src/format/formatElapsed.ts index d658ae406..85fc65d73 100644 --- a/packages/util/src/format/formatElapsed.ts +++ b/packages/util/src/format/formatElapsed.ts @@ -6,6 +6,7 @@ import type { ToBn } from '../types'; import { bnToBn } from '../bn/toBn'; +/** @internal */ function formatValue (elapsed: number): string { if (elapsed < 15) { return `${elapsed.toFixed(1)}s`; @@ -18,6 +19,10 @@ function formatValue (elapsed: number): string { return `${elapsed / 3600 | 0}h`; } +/** + * @name formatElapsed + * @description Formats an elapsed value into s, m, h or day segments + */ export function formatElapsed (now?: Date | null, value?: bigint | BN | ExtToBn | Date | number | null): string { const tsNow = (now && now.getTime()) || 0; const tsValue = value instanceof Date diff --git a/packages/util/src/format/formatNumber.ts b/packages/util/src/format/formatNumber.ts index e6b42e9b7..ae095ae0d 100644 --- a/packages/util/src/format/formatNumber.ts +++ b/packages/util/src/format/formatNumber.ts @@ -7,6 +7,10 @@ import type { ToBn } from '../types'; import { bnToBn } from '../bn/toBn'; import { formatDecimal } from './formatDecimal'; +/** + * @name formatNumber + * @description Formats a number into string format with thousand seperators + */ export function formatNumber (value?: ExtToBn | BN | bigint | number | null): string { return formatDecimal(bnToBn(value).toString()); } diff --git a/packages/util/src/format/si.ts b/packages/util/src/format/si.ts index a54b5ec97..1edd31783 100644 --- a/packages/util/src/format/si.ts +++ b/packages/util/src/format/si.ts @@ -3,8 +3,10 @@ import type { SiDef } from '../types'; +/** @internal */ export const SI_MID = 8; +/** @internal */ export const SI: SiDef[] = [ { power: -24, text: 'yocto', value: 'y' }, { power: -21, text: 'zepto', value: 'z' }, @@ -26,6 +28,7 @@ export const SI: SiDef[] = [ ]; // Given a SI type (e.g. k, m, Y) find the SI definition +/** @internal */ export function findSi (type: string): SiDef { // use a loop here, better RN support (which doesn't have [].find) for (let i = 0; i < SI.length; i++) { @@ -37,6 +40,7 @@ export function findSi (type: string): SiDef { return SI[SI_MID]; } +/** @internal */ export function calcSi (text: string, decimals: number, forceUnit?: string): SiDef { if (forceUnit) { return findSi(forceUnit); diff --git a/packages/util/src/is/ascii.ts b/packages/util/src/is/ascii.ts index 670bfa286..ee65541e3 100644 --- a/packages/util/src/is/ascii.ts +++ b/packages/util/src/is/ascii.ts @@ -17,6 +17,7 @@ function isAsciiByte (b: number): boolean { ); } +/** @internal */ function isAsciiChar (s: string): boolean { return isAsciiByte(s.charCodeAt(0)); }