Add arrayContentStartsWith

This commit is contained in:
Simon Warta 2021-03-22 11:36:55 +01:00
parent 810389d784
commit d50575036f
4 changed files with 51 additions and 2 deletions

View File

@ -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

View File

@ -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);
});
});
});

View File

@ -16,3 +16,22 @@ export function arrayContentEquals<T extends string | number | boolean>(
}
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<T extends string | number | boolean>(
a: ArrayLike<T>,
b: ArrayLike<T>,
): 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;
}

View File

@ -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";