From 49d200fb8532b50eabf810eb8bee194a9144aef1 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 17 Jun 2021 12:03:02 +0300 Subject: [PATCH] Add u8aCmp (sorting determination) (#1015) * Add u8aCmp (sorting determination) * Update packages/util/src/u8a/cmp.ts * Update packages/util/src/u8a/cmp.ts * Update packages/util/src/u8a/cmp.ts * Use u8aCmp in u8aEq --- CHANGELOG.md | 7 ++++ packages/networks/src/defaults.spec.ts | 11 ++++++ packages/util/src/u8a/cmp.spec.ts | 39 ++++++++++++++++++++ packages/util/src/u8a/cmp.ts | 51 ++++++++++++++++++++++++++ packages/util/src/u8a/eq.ts | 17 ++------- packages/util/src/u8a/index.ts | 1 + packages/util/src/u8a/sorted.ts | 38 ++++++++----------- 7 files changed, 129 insertions(+), 35 deletions(-) create mode 100644 packages/util/src/u8a/cmp.spec.ts create mode 100644 packages/util/src/u8a/cmp.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 450c7dd15..364627402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Changes: + +- Added `u8aCmp(a, b)` to do sorting compares + + ## 6.8.1 Jun 14, 2021 Contributed: diff --git a/packages/networks/src/defaults.spec.ts b/packages/networks/src/defaults.spec.ts index 67be90ea5..af6b388e4 100644 --- a/packages/networks/src/defaults.spec.ts +++ b/packages/networks/src/defaults.spec.ts @@ -1,9 +1,20 @@ // Copyright 2017-2021 @polkadot/networks authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { isHex } from '@polkadot/util'; + import { knownGenesis, knownLedger } from './defaults'; describe('defaults', (): void => { + it('has hex values for all genesis chains', (): void => { + expect( + Object.keys(knownGenesis).filter((network) => + !knownGenesis[network].length || + knownGenesis[network].some((g) => !isHex(g, 256)) + ) + ).toEqual([]); + }); + it('has genesis for all Ledger chains', (): void => { expect( Object.keys(knownLedger).filter((network) => diff --git a/packages/util/src/u8a/cmp.spec.ts b/packages/util/src/u8a/cmp.spec.ts new file mode 100644 index 000000000..6148806f2 --- /dev/null +++ b/packages/util/src/u8a/cmp.spec.ts @@ -0,0 +1,39 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { u8aCmp } from '.'; + +describe('u8aCmp', (): void => { + it('has the same result as localCompare (equal length)', (): void => { + const a = 'test abc'; + const b = 'test def'; + + expect( + u8aCmp(a, b) + ).toEqual(a.localeCompare(b)); + expect( + u8aCmp(b, a) + ).toEqual(b.localeCompare(a)); + }); + + it('has the same result as localCompare (diff length)', (): void => { + const a = 'test'; + const b = 'test abc'; + + expect( + u8aCmp(a, b) + ).toEqual(a.localeCompare(b)); + expect( + u8aCmp(b, a) + ).toEqual(b.localeCompare(a)); + }); + + it('compares between string and Uint8Array', (): void => { + expect( + u8aCmp( + '0x12345678', + new Uint8Array([0x12, 0x34, 0x56, 0x78]) + ) + ).toEqual(0); + }); +}); diff --git a/packages/util/src/u8a/cmp.ts b/packages/util/src/u8a/cmp.ts new file mode 100644 index 000000000..93cf0992b --- /dev/null +++ b/packages/util/src/u8a/cmp.ts @@ -0,0 +1,51 @@ +// Copyright 2017-2021 @polkadot/util authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { u8aToU8a } from './toU8a'; + +function compare (a: Uint8Array, b: Uint8Array): number { + let i = 0; + + while (true) { + const overA = i >= a.length; + const overB = i >= b.length; + + if (overA && overB) { + // both ends reached + return 0; + } else if (overA) { + // a has no more data, b has data + return -1; + } else if (overB) { + // b has no more data, a has data + return 1; + } else if (a[i] !== b[i]) { + // the number in this index doesn't match + return a[i] > b[i] + ? 1 + : -1; + } + + i++; + } +} + +/** + * @name u8aCmp + * @summary Compares two Uint8Arrays for sorting. + * @description + * For `UInt8Array` (or hex string) input values returning -1, 0 or +1 + * @example + *
+ * + * ```javascript + * import { u8aCmp } from '@polkadot/util'; + * + * u8aCmp(new Uint8Array([0x67, 0x65]), new Uint8Array([0x68, 0x65])); // -1 + * u8aCmp(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // 0 + * u8aCmp(new Uint8Array([0x69, 0x65]), new Uint8Array([0x68, 0x65])); // +1 + * ``` + */ +export function u8aCmp (a: Uint8Array | string, b: Uint8Array | string): number { + return compare(u8aToU8a(a), u8aToU8a(b)); +} diff --git a/packages/util/src/u8a/eq.ts b/packages/util/src/u8a/eq.ts index 2225e9052..16c11731a 100644 --- a/packages/util/src/u8a/eq.ts +++ b/packages/util/src/u8a/eq.ts @@ -1,32 +1,23 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { u8aCmp } from './cmp'; import { u8aToU8a } from './toU8a'; function equals (a: Uint8Array, b: Uint8Array): boolean { - if (a.length !== b.length) { - return false; - } - - for (let i = 0; i < a.length; i++) { - if (a[i] !== b[i]) { - return false; - } - } - - return true; + return (a.length === b.length) && (u8aCmp(a, b) === 0); } /** * @name u8aEq - * @summary Compares two Uint8Arrays. + * @summary Compares two Uint8Arrays for equality. * @description * For `UInt8Array` (or hex string) input values true if there is a match. * @example *
* * ```javascript - * import { { u8aEq } from '@polkadot/util'; + * import { u8aEq } from '@polkadot/util'; * * u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true * ``` diff --git a/packages/util/src/u8a/index.ts b/packages/util/src/u8a/index.ts index e2c4db9fb..4f815a7e9 100644 --- a/packages/util/src/u8a/index.ts +++ b/packages/util/src/u8a/index.ts @@ -5,6 +5,7 @@ * @summary Utility methods to convert to and from `Uint8Array` objects */ +export { u8aCmp } from './cmp'; export { u8aConcat } from './concat'; export { u8aEq } from './eq'; export { u8aFixLength } from './fixLength'; diff --git a/packages/util/src/u8a/sorted.ts b/packages/util/src/u8a/sorted.ts index 66b016b9f..8c1842aed 100644 --- a/packages/util/src/u8a/sorted.ts +++ b/packages/util/src/u8a/sorted.ts @@ -1,28 +1,22 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { isUndefined } from '../is/undefined'; +import { u8aCmp } from './cmp'; +/** + * @name u8aSorted + * @summary Sorts an array of Uint8Arrays + * @description + * For input `UInt8Array[]` return the sorted result + * @example + *
+ * + * ```javascript + * import { u8aSorted} from '@polkadot/util'; + * + * u8aSorted([new Uint8Array([0x69]), new Uint8Array([0x68])]); // [0x68, 0x69] + * ``` + */ export function u8aSorted (u8as: Uint8Array[]): Uint8Array[] { - return u8as.sort((a, b): number => { - let i = 0; - - while (true) { - if (isUndefined(a[i]) && isUndefined(b[i])) { - return 0; - } else if (isUndefined(a[i])) { - return -1; - } else if (isUndefined(b[i])) { - return 1; - } - - const cmp = a[i] - b[i]; - - if (cmp !== 0) { - return cmp; - } - - i++; - } - }); + return u8as.sort(u8aCmp); }