From d50575036f585dc0333f4dd0fc2f1bf32acd0772 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 22 Mar 2021 11:36:55 +0100 Subject: [PATCH] Add arrayContentStartsWith --- CHANGELOG.md | 2 ++ packages/utils/src/array.spec.ts | 30 +++++++++++++++++++++++++++++- packages/utils/src/arrays.ts | 19 +++++++++++++++++++ packages/utils/src/index.ts | 2 +- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e3e53c7..660720ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ and this project adheres to supported nested pubkeys for multisig. `SinglePubkey` is the old `PubKey` in which the `value` is a base64 encoded string. And `Secp256k1Pubkey` is a single secp256k1 pubkey. +- @cosmjs/utils: The new `arrayContentStartsWith` works similar to + `arrayContentEquals` but only checks the start of an array. ### Changed diff --git a/packages/utils/src/array.spec.ts b/packages/utils/src/array.spec.ts index d42241bc..0dae826e 100644 --- a/packages/utils/src/array.spec.ts +++ b/packages/utils/src/array.spec.ts @@ -1,4 +1,4 @@ -import { arrayContentEquals } from "./arrays"; +import { arrayContentEquals, arrayContentStartsWith } from "./arrays"; describe("array", () => { describe("arrayContentEquals", () => { @@ -30,4 +30,32 @@ describe("array", () => { expect(arrayContentEquals([], new Uint8Array([]))).toEqual(true); }); }); + + describe("arrayContentStartsWith", () => { + it("can compare number arrays", () => { + // same length + expect(arrayContentStartsWith([], [])).toEqual(true); // Same behaviour as "".startsWith("") + expect(arrayContentStartsWith([1, 2, 3], [1, 2, 3])).toEqual(true); + expect(arrayContentStartsWith([1, 2, 3], [1, 2, 8])).toEqual(false); + expect(arrayContentStartsWith([1, 2, 3], [0, 0, 0])).toEqual(false); + + // a shorter than b + expect(arrayContentStartsWith([], [1, 2, 3])).toEqual(false); + expect(arrayContentStartsWith([1], [1, 2, 3])).toEqual(false); + expect(arrayContentStartsWith([1, 2], [1, 2, 3])).toEqual(false); + + // a longer than b + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])).toEqual(true); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1, 2, 3, 4])).toEqual(true); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1, 2, 3])).toEqual(true); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1, 2])).toEqual(true); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1])).toEqual(true); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [])).toEqual(true); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1, 2, 3, 4, 0])).toEqual(false); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1, 2, 3, 0])).toEqual(false); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1, 2, 0])).toEqual(false); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [1, 0])).toEqual(false); + expect(arrayContentStartsWith([1, 2, 3, 4, 5], [0])).toEqual(false); + }); + }); }); diff --git a/packages/utils/src/arrays.ts b/packages/utils/src/arrays.ts index 87f5a442..4f15b89e 100644 --- a/packages/utils/src/arrays.ts +++ b/packages/utils/src/arrays.ts @@ -16,3 +16,22 @@ export function arrayContentEquals( } return true; } + +/** + * Checks if `a` starts with the contents of `b`. + * + * This requires equality of the element values, where element equality means `===` returning `true`. + * + * This allows you to compare the content of a Buffer, Uint8Array or number[], ignoring the specific type. + * As a consequence, this returns different results than Jasmine's `toEqual`, which ensures elements have the same type. + */ +export function arrayContentStartsWith( + a: ArrayLike, + b: ArrayLike, +): boolean { + if (a.length < b.length) return false; + for (let i = 0; i < b.length; ++i) { + if (a[i] !== b[i]) return false; + } + return true; +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 372b3615..790c7d27 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,4 +1,4 @@ -export { arrayContentEquals } from "./arrays"; +export { arrayContentEquals, arrayContentStartsWith } from "./arrays"; export { assert, assertDefined, assertDefinedAndNotNull } from "./assert"; export { sleep } from "./sleep"; export { isNonNullObject, isUint8Array } from "./typechecks";