Add and arrayContentEquals
This commit is contained in:
parent
2d56b1ae89
commit
6f36b425d1
@ -5,6 +5,7 @@
|
||||
- @cosmjs/cli: Import `encodeBech32Pubkey` and `decodeBech32Pubkey` by default.
|
||||
- @cosmjs/launchpad: Add ed25519 support to `encodeBech32Pubkey`.
|
||||
- @cosmjs/launchpad: Add `encodeAminoPubkey` and `decodeAminoPubkey`.
|
||||
- @cosmjs/utils: Add `arrayContentEquals`.
|
||||
|
||||
## 0.22.0 (2020-08-03)
|
||||
|
||||
|
||||
33
packages/utils/src/array.spec.ts
Normal file
33
packages/utils/src/array.spec.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { arrayContentEquals } from "./arrays";
|
||||
|
||||
describe("array", () => {
|
||||
describe("arrayContentEquals", () => {
|
||||
it("can compare number arrays", () => {
|
||||
expect(arrayContentEquals([1, 2, 3], [1, 2, 3])).toEqual(true);
|
||||
expect(arrayContentEquals([1, 2, 3], [1, 2, 3, 4])).toEqual(false);
|
||||
expect(arrayContentEquals([1, 2, 3], [3, 2, 1])).toEqual(false);
|
||||
});
|
||||
|
||||
it("can compare string arrays", () => {
|
||||
expect(arrayContentEquals(["a", "b"], ["a", "b"])).toEqual(true);
|
||||
expect(arrayContentEquals(["a", "b"], ["a", "b", "c"])).toEqual(false);
|
||||
expect(arrayContentEquals(["a", "b"], ["b", "a"])).toEqual(false);
|
||||
});
|
||||
|
||||
it("can compare bool arrays", () => {
|
||||
expect(arrayContentEquals([true, false], [true, false])).toEqual(true);
|
||||
expect(arrayContentEquals([true, false], [true, false, true])).toEqual(false);
|
||||
expect(arrayContentEquals([true, false], [false, true])).toEqual(false);
|
||||
});
|
||||
|
||||
it("can compare different array types", () => {
|
||||
expect(arrayContentEquals([1, 2, 3], new Uint8Array([1, 2, 3]))).toEqual(true);
|
||||
expect(arrayContentEquals([1, 2, 3], new Uint8Array([3, 2, 1]))).toEqual(false);
|
||||
});
|
||||
|
||||
it("works for empty arrays", () => {
|
||||
expect(arrayContentEquals([], [])).toEqual(true);
|
||||
expect(arrayContentEquals([], new Uint8Array([]))).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
18
packages/utils/src/arrays.ts
Normal file
18
packages/utils/src/arrays.ts
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Compares the content of two arrays-like objects for equality.
|
||||
*
|
||||
* Equality is defined as having equal length and 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 arrayContentEquals<T extends string | number | boolean>(
|
||||
a: ArrayLike<T>,
|
||||
b: ArrayLike<T>,
|
||||
): 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;
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
export { arrayContentEquals } from "./arrays";
|
||||
export { assert } from "./assert";
|
||||
export { sleep } from "./sleep";
|
||||
export { isNonNullObject, isUint8Array } from "./typechecks";
|
||||
|
||||
12
packages/utils/types/arrays.d.ts
vendored
Normal file
12
packages/utils/types/arrays.d.ts
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Compares the content of two arrays-like objects for equality.
|
||||
*
|
||||
* Equality is defined as having equal length and 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 declare function arrayContentEquals<T extends string | number | boolean>(
|
||||
a: ArrayLike<T>,
|
||||
b: ArrayLike<T>,
|
||||
): boolean;
|
||||
1
packages/utils/types/index.d.ts
vendored
1
packages/utils/types/index.d.ts
vendored
@ -1,3 +1,4 @@
|
||||
export { arrayContentEquals } from "./arrays";
|
||||
export { assert } from "./assert";
|
||||
export { sleep } from "./sleep";
|
||||
export { isNonNullObject, isUint8Array } from "./typechecks";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user