2021-04-22 22:24:41 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-04-22 22:24:41 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-04-22 22:24:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import {
|
|
|
|
arrayDiff,
|
|
|
|
arrayFastClone,
|
|
|
|
arrayFastResample,
|
|
|
|
arrayHasDiff,
|
|
|
|
arrayHasOrderChange,
|
2021-12-13 10:57:51 +00:00
|
|
|
arrayUnion,
|
2021-05-08 03:06:07 +00:00
|
|
|
arrayRescale,
|
2021-04-22 22:24:41 +00:00
|
|
|
arraySeed,
|
2021-05-08 03:06:07 +00:00
|
|
|
arraySmoothingResample,
|
2021-04-21 03:30:21 +00:00
|
|
|
arrayTrimFill,
|
2021-12-13 10:57:51 +00:00
|
|
|
arrayIntersection,
|
2021-04-22 22:24:41 +00:00
|
|
|
ArrayUtil,
|
|
|
|
GroupedArray,
|
2022-09-21 18:06:05 +00:00
|
|
|
concat,
|
2022-12-22 10:37:07 +00:00
|
|
|
asyncEvery,
|
2023-04-24 13:19:46 +00:00
|
|
|
asyncSome,
|
2024-11-19 10:09:25 +00:00
|
|
|
asyncSomeParallel,
|
2024-11-20 14:27:09 +00:00
|
|
|
asyncFilter,
|
2024-10-15 13:57:26 +00:00
|
|
|
} from "../../../src/utils/arrays";
|
2021-04-22 22:24:41 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
type TestParams = { input: number[]; output: number[] };
|
2022-02-21 16:57:44 +00:00
|
|
|
type TestCase = [string, TestParams];
|
|
|
|
|
|
|
|
function expectSample(input: number[], expected: number[], smooth = false) {
|
2021-05-08 03:06:07 +00:00
|
|
|
const result = (smooth ? arraySmoothingResample : arrayFastResample)(input, expected.length);
|
2021-04-22 22:24:41 +00:00
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(expected.length);
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrays", () => {
|
|
|
|
describe("arrayFastResample", () => {
|
2022-02-21 16:57:44 +00:00
|
|
|
const downsampleCases: TestCase[] = [
|
2022-12-12 11:24:14 +00:00
|
|
|
["Odd -> Even", { input: [1, 2, 3, 4, 5], output: [1, 4] }],
|
|
|
|
["Odd -> Odd", { input: [1, 2, 3, 4, 5], output: [1, 3, 5] }],
|
|
|
|
["Even -> Odd", { input: [1, 2, 3, 4], output: [1, 2, 3] }],
|
|
|
|
["Even -> Even", { input: [1, 2, 3, 4], output: [1, 3] }],
|
2022-02-21 16:57:44 +00:00
|
|
|
];
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(downsampleCases)("downsamples correctly from %s", (_d, { input, output }) =>
|
2022-02-21 16:57:44 +00:00
|
|
|
expectSample(input, output),
|
|
|
|
);
|
2021-04-22 22:24:41 +00:00
|
|
|
|
2022-02-21 16:57:44 +00:00
|
|
|
const upsampleCases: TestCase[] = [
|
2022-12-12 11:24:14 +00:00
|
|
|
["Odd -> Even", { input: [1, 2, 3], output: [1, 1, 2, 2, 3, 3] }],
|
|
|
|
["Odd -> Odd", { input: [1, 2, 3], output: [1, 1, 2, 2, 3] }],
|
|
|
|
["Even -> Odd", { input: [1, 2], output: [1, 1, 1, 2, 2] }],
|
|
|
|
["Even -> Even", { input: [1, 2], output: [1, 1, 1, 2, 2, 2] }],
|
2022-02-21 16:57:44 +00:00
|
|
|
];
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(upsampleCases)("upsamples correctly from %s", (_d, { input, output }) => expectSample(input, output));
|
2021-04-22 22:24:41 +00:00
|
|
|
|
2022-02-21 16:57:44 +00:00
|
|
|
const maintainSampleCases: TestCase[] = [
|
2022-12-12 11:24:14 +00:00
|
|
|
["Odd", { input: [1, 2, 3], output: [1, 2, 3] }], // Odd
|
|
|
|
["Even", { input: [1, 2], output: [1, 2] }], // Even
|
2022-02-21 16:57:44 +00:00
|
|
|
];
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(maintainSampleCases)("maintains samples for %s", (_d, { input, output }) =>
|
2022-02-21 16:57:44 +00:00
|
|
|
expectSample(input, output),
|
|
|
|
);
|
2021-04-22 22:24:41 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arraySmoothingResample", () => {
|
2022-02-21 16:57:44 +00:00
|
|
|
// Dev note: these aren't great samples, but they demonstrate the bare minimum. Ideally
|
|
|
|
// we'd be feeding a thousand values in and seeing what a curve of 250 values looks like,
|
|
|
|
// but that's not really feasible to manually verify accuracy.
|
|
|
|
const downsampleCases: TestCase[] = [
|
2022-12-12 11:24:14 +00:00
|
|
|
["Odd -> Even", { input: [4, 4, 1, 4, 4, 1, 4, 4, 1], output: [3, 3, 3, 3] }],
|
|
|
|
["Odd -> Odd", { input: [4, 4, 1, 4, 4, 1, 4, 4, 1], output: [3, 3, 3] }],
|
|
|
|
["Even -> Odd", { input: [4, 4, 1, 4, 4, 1, 4, 4], output: [3, 3, 3] }],
|
|
|
|
["Even -> Even", { input: [4, 4, 1, 4, 4, 1, 4, 4], output: [3, 3] }],
|
2022-02-21 16:57:44 +00:00
|
|
|
];
|
2021-05-08 03:06:07 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(downsampleCases)("downsamples correctly from %s", (_d, { input, output }) =>
|
2022-02-21 16:57:44 +00:00
|
|
|
expectSample(input, output, true),
|
|
|
|
);
|
2021-05-08 03:06:07 +00:00
|
|
|
|
2022-02-21 16:57:44 +00:00
|
|
|
const upsampleCases: TestCase[] = [
|
2022-12-12 11:24:14 +00:00
|
|
|
["Odd -> Even", { input: [2, 0, 2], output: [2, 2, 0, 0, 2, 2] }],
|
|
|
|
["Odd -> Odd", { input: [2, 0, 2], output: [2, 2, 0, 0, 2] }],
|
|
|
|
["Even -> Odd", { input: [2, 0], output: [2, 2, 2, 0, 0] }],
|
|
|
|
["Even -> Even", { input: [2, 0], output: [2, 2, 2, 0, 0, 0] }],
|
2022-02-21 16:57:44 +00:00
|
|
|
];
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(upsampleCases)("upsamples correctly from %s", (_d, { input, output }) =>
|
2022-02-21 16:57:44 +00:00
|
|
|
expectSample(input, output, true),
|
|
|
|
);
|
|
|
|
|
|
|
|
const maintainCases: TestCase[] = [
|
2022-12-12 11:24:14 +00:00
|
|
|
["Odd", { input: [2, 0, 2], output: [2, 0, 2] }],
|
|
|
|
["Even", { input: [2, 0], output: [2, 0] }],
|
2022-02-21 16:57:44 +00:00
|
|
|
];
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each(maintainCases)("maintains samples for %s", (_d, { input, output }) => expectSample(input, output));
|
2021-05-08 03:06:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrayRescale", () => {
|
|
|
|
it("should rescale", () => {
|
2021-05-08 03:06:07 +00:00
|
|
|
const input = [8, 9, 1, 0, 2, 7, 10];
|
|
|
|
const output = [80, 90, 10, 0, 20, 70, 100];
|
|
|
|
const result = arrayRescale(input, 0, 100);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(output.length);
|
|
|
|
expect(result).toEqual(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrayTrimFill", () => {
|
|
|
|
it("should shrink arrays", () => {
|
2021-04-21 03:30:21 +00:00
|
|
|
const input = [1, 2, 3];
|
|
|
|
const output = [1, 2];
|
|
|
|
const seed = [4, 5, 6];
|
|
|
|
const result = arrayTrimFill(input, output.length, seed);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(output.length);
|
|
|
|
expect(result).toEqual(output);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should expand arrays", () => {
|
2021-04-21 03:30:21 +00:00
|
|
|
const input = [1, 2, 3];
|
|
|
|
const output = [1, 2, 3, 4, 5];
|
|
|
|
const seed = [4, 5, 6];
|
|
|
|
const result = arrayTrimFill(input, output.length, seed);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(output.length);
|
|
|
|
expect(result).toEqual(output);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should keep arrays the same", () => {
|
2021-04-21 03:30:21 +00:00
|
|
|
const input = [1, 2, 3];
|
|
|
|
const output = [1, 2, 3];
|
|
|
|
const seed = [4, 5, 6];
|
|
|
|
const result = arrayTrimFill(input, output.length, seed);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(output.length);
|
|
|
|
expect(result).toEqual(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arraySeed", () => {
|
|
|
|
it("should create an array of given length", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const val = 1;
|
|
|
|
const output = [val, val, val];
|
|
|
|
const result = arraySeed(val, output.length);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(output.length);
|
|
|
|
expect(result).toEqual(output);
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should maintain pointers", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const val = {}; // this works because `{} !== {}`, which is what toEqual checks
|
|
|
|
const output = [val, val, val];
|
|
|
|
const result = arraySeed(val, output.length);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(output.length);
|
|
|
|
expect(result).toEqual(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrayFastClone", () => {
|
|
|
|
it("should break pointer reference on source array", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const val = {}; // we'll test to make sure the values maintain pointers too
|
|
|
|
const input = [val, val, val];
|
|
|
|
const result = arrayFastClone(input);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(input.length);
|
|
|
|
expect(result).toEqual(input); // we want the array contents to match...
|
|
|
|
expect(result).not.toBe(input); // ... but be a different reference
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrayHasOrderChange", () => {
|
|
|
|
it("should flag true on B ordering difference", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [3, 2, 1];
|
|
|
|
const result = arrayHasOrderChange(a, b);
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should flag false on no ordering difference", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2, 3];
|
|
|
|
const result = arrayHasOrderChange(a, b);
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should flag true on A length > B length", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3, 4];
|
|
|
|
const b = [1, 2, 3];
|
|
|
|
const result = arrayHasOrderChange(a, b);
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should flag true on A length < B length", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2, 3, 4];
|
|
|
|
const result = arrayHasOrderChange(a, b);
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrayHasDiff", () => {
|
|
|
|
it("should flag true on A length > B length", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3, 4];
|
|
|
|
const b = [1, 2, 3];
|
|
|
|
const result = arrayHasDiff(a, b);
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should flag true on A length < B length", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2, 3, 4];
|
|
|
|
const result = arrayHasDiff(a, b);
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should flag true on element differences", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [4, 5, 6];
|
|
|
|
const result = arrayHasDiff(a, b);
|
|
|
|
expect(result).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should flag false if same but order different", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [3, 1, 2];
|
|
|
|
const result = arrayHasDiff(a, b);
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should flag false if same", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2, 3];
|
|
|
|
const result = arrayHasDiff(a, b);
|
|
|
|
expect(result).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrayDiff", () => {
|
|
|
|
it("should see added from A->B", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2, 3, 4];
|
|
|
|
const result = arrayDiff(a, b);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result.added).toBeDefined();
|
|
|
|
expect(result.removed).toBeDefined();
|
|
|
|
expect(result.added).toHaveLength(1);
|
|
|
|
expect(result.removed).toHaveLength(0);
|
|
|
|
expect(result.added).toEqual([4]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should see removed from A->B", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2];
|
|
|
|
const result = arrayDiff(a, b);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result.added).toBeDefined();
|
|
|
|
expect(result.removed).toBeDefined();
|
|
|
|
expect(result.added).toHaveLength(0);
|
|
|
|
expect(result.removed).toHaveLength(1);
|
|
|
|
expect(result.removed).toEqual([3]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should see added and removed in the same set", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2, 4]; // note diff
|
|
|
|
const result = arrayDiff(a, b);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result.added).toBeDefined();
|
|
|
|
expect(result.removed).toBeDefined();
|
|
|
|
expect(result.added).toHaveLength(1);
|
|
|
|
expect(result.removed).toHaveLength(1);
|
|
|
|
expect(result.added).toEqual([4]);
|
|
|
|
expect(result.removed).toEqual([3]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrayIntersection", () => {
|
|
|
|
it("should return the intersection", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2, 4]; // note diff
|
2021-12-13 10:57:51 +00:00
|
|
|
const result = arrayIntersection(a, b);
|
2021-04-22 22:24:41 +00:00
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(2);
|
|
|
|
expect(result).toEqual([1, 2]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should return an empty array on no matches", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [4, 5, 6];
|
2021-12-13 10:57:51 +00:00
|
|
|
const result = arrayIntersection(a, b);
|
2021-04-22 22:24:41 +00:00
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("arrayUnion", () => {
|
|
|
|
it("should union 3 arrays with deduplication", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 2, 3];
|
|
|
|
const b = [1, 2, 4, 5]; // note missing 3
|
|
|
|
const c = [6, 7, 8, 9];
|
2021-12-13 10:57:51 +00:00
|
|
|
const result = arrayUnion(a, b, c);
|
2021-04-22 22:24:41 +00:00
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(9);
|
|
|
|
expect(result).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should deduplicate a single array", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
// dev note: this is technically an edge case, but it is described behaviour if the
|
2021-12-13 10:57:51 +00:00
|
|
|
// function is only provided one array (it'll merge the array against itself)
|
2021-04-22 22:24:41 +00:00
|
|
|
const a = [1, 1, 2, 2, 3, 3];
|
2021-12-13 10:57:51 +00:00
|
|
|
const result = arrayUnion(a);
|
2021-04-22 22:24:41 +00:00
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(3);
|
|
|
|
expect(result).toEqual([1, 2, 3]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("ArrayUtil", () => {
|
|
|
|
it("should maintain the pointer to the given array", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const input = [1, 2, 3];
|
|
|
|
const result = new ArrayUtil(input);
|
|
|
|
expect(result.value).toBe(input);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should group appropriately", () => {
|
|
|
|
const input = [
|
|
|
|
["a", 1],
|
|
|
|
["b", 2],
|
|
|
|
["c", 3],
|
|
|
|
["a", 4],
|
|
|
|
["a", 5],
|
|
|
|
["b", 6],
|
|
|
|
];
|
2021-04-22 22:24:41 +00:00
|
|
|
const output = {
|
2022-12-12 11:24:14 +00:00
|
|
|
a: [
|
|
|
|
["a", 1],
|
|
|
|
["a", 4],
|
|
|
|
["a", 5],
|
|
|
|
],
|
|
|
|
b: [
|
|
|
|
["b", 2],
|
|
|
|
["b", 6],
|
|
|
|
],
|
|
|
|
c: [["c", 3]],
|
2021-04-22 22:24:41 +00:00
|
|
|
};
|
2022-12-12 11:24:14 +00:00
|
|
|
const result = new ArrayUtil(input).groupBy((p) => p[0]);
|
2021-04-22 22:24:41 +00:00
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result.value).toBeDefined();
|
|
|
|
|
2021-07-23 17:40:22 +00:00
|
|
|
const asObject = Object.fromEntries(result.value.entries());
|
2021-04-22 22:24:41 +00:00
|
|
|
expect(asObject).toMatchObject(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("GroupedArray", () => {
|
|
|
|
it("should maintain the pointer to the given map", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const input = new Map([
|
2022-12-12 11:24:14 +00:00
|
|
|
["a", [1, 2, 3]],
|
|
|
|
["b", [7, 8, 9]],
|
|
|
|
["c", [4, 5, 6]],
|
2021-04-22 22:24:41 +00:00
|
|
|
]);
|
|
|
|
const result = new GroupedArray(input);
|
|
|
|
expect(result.value).toBe(input);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("should ordering by the provided key order", () => {
|
2021-04-22 22:24:41 +00:00
|
|
|
const input = new Map([
|
2022-12-12 11:24:14 +00:00
|
|
|
["a", [1, 2, 3]],
|
|
|
|
["b", [7, 8, 9]], // note counting diff
|
|
|
|
["c", [4, 5, 6]],
|
2021-04-22 22:24:41 +00:00
|
|
|
]);
|
|
|
|
const output = [4, 5, 6, 1, 2, 3, 7, 8, 9];
|
2022-12-12 11:24:14 +00:00
|
|
|
const keyOrder = ["c", "a", "b"]; // note weird order to cause the `output` to be strange
|
2021-04-22 22:24:41 +00:00
|
|
|
const result = new GroupedArray(input).orderBy(keyOrder);
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result.value).toBeDefined();
|
|
|
|
expect(result.value).toEqual(output);
|
|
|
|
});
|
|
|
|
});
|
2022-09-21 18:06:05 +00:00
|
|
|
|
|
|
|
describe("concat", () => {
|
|
|
|
const emptyArray = () => new Uint8Array(0);
|
|
|
|
const array1 = () => new Uint8Array([1, 2, 3]);
|
|
|
|
const array2 = () => new Uint8Array([4, 5, 6]);
|
|
|
|
const array3 = () => new Uint8Array([7, 8, 9]);
|
|
|
|
|
|
|
|
it("should work for empty arrays", () => {
|
|
|
|
expect(concat(emptyArray(), emptyArray())).toEqual(emptyArray());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should concat an empty and non-empty array", () => {
|
|
|
|
expect(concat(emptyArray(), array1())).toEqual(array1());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should concat an non-empty and empty array", () => {
|
|
|
|
expect(concat(array1(), emptyArray())).toEqual(array1());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should concat two arrays", () => {
|
|
|
|
expect(concat(array1(), array2())).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6]));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should concat three arrays", () => {
|
|
|
|
expect(concat(array1(), array2(), array3())).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]));
|
|
|
|
});
|
|
|
|
});
|
2022-12-22 10:37:07 +00:00
|
|
|
|
|
|
|
describe("asyncEvery", () => {
|
|
|
|
it("when called with an empty array, it should return true", async () => {
|
|
|
|
expect(await asyncEvery([], jest.fn().mockResolvedValue(true))).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("when called with some items and the predicate resolves to true for all of them, it should return true", async () => {
|
|
|
|
const predicate = jest.fn().mockResolvedValue(true);
|
|
|
|
expect(await asyncEvery([1, 2, 3], predicate)).toBe(true);
|
|
|
|
expect(predicate).toHaveBeenCalledTimes(3);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(1);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(2);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("when called with some items and the predicate resolves to false for all of them, it should return false", async () => {
|
|
|
|
const predicate = jest.fn().mockResolvedValue(false);
|
|
|
|
expect(await asyncEvery([1, 2, 3], predicate)).toBe(false);
|
|
|
|
expect(predicate).toHaveBeenCalledTimes(1);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("when called with some items and the predicate resolves to false for one of them, it should return false", async () => {
|
|
|
|
const predicate = jest.fn().mockResolvedValueOnce(true).mockResolvedValueOnce(false);
|
|
|
|
expect(await asyncEvery([1, 2, 3], predicate)).toBe(false);
|
|
|
|
expect(predicate).toHaveBeenCalledTimes(2);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(1);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(2);
|
|
|
|
});
|
|
|
|
});
|
2023-04-24 13:19:46 +00:00
|
|
|
|
|
|
|
describe("asyncSome", () => {
|
|
|
|
it("when called with an empty array, it should return false", async () => {
|
|
|
|
expect(await asyncSome([], jest.fn().mockResolvedValue(true))).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("when called with some items and the predicate resolves to false for all of them, it should return false", async () => {
|
|
|
|
const predicate = jest.fn().mockResolvedValue(false);
|
|
|
|
expect(await asyncSome([1, 2, 3], predicate)).toBe(false);
|
|
|
|
expect(predicate).toHaveBeenCalledTimes(3);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(1);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(2);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("when called with some items and the predicate resolves to true, it should short-circuit and return true", async () => {
|
|
|
|
const predicate = jest.fn().mockResolvedValueOnce(false).mockResolvedValueOnce(true);
|
|
|
|
expect(await asyncSome([1, 2, 3], predicate)).toBe(true);
|
|
|
|
expect(predicate).toHaveBeenCalledTimes(2);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(1);
|
|
|
|
expect(predicate).toHaveBeenCalledWith(2);
|
|
|
|
});
|
|
|
|
});
|
2024-11-19 10:09:25 +00:00
|
|
|
|
|
|
|
describe("asyncSomeParallel", () => {
|
|
|
|
it("when called with an empty array, it should return false", async () => {
|
|
|
|
expect(await asyncSomeParallel([], jest.fn().mockResolvedValue(true))).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("when all the predicates return false", async () => {
|
|
|
|
expect(await asyncSomeParallel([1, 2, 3], jest.fn().mockResolvedValue(false))).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("when all the predicates return true", async () => {
|
|
|
|
expect(await asyncSomeParallel([1, 2, 3], jest.fn().mockResolvedValue(true))).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("when one of the predicate return true", async () => {
|
|
|
|
const predicate = jest.fn().mockImplementation((value) => Promise.resolve(value === 2));
|
|
|
|
expect(await asyncSomeParallel([1, 2, 3], predicate)).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2024-11-20 14:27:09 +00:00
|
|
|
|
|
|
|
describe("asyncFilter", () => {
|
|
|
|
it("when called with an empty array, it should return an empty array", async () => {
|
|
|
|
expect(await asyncFilter([], jest.fn().mockResolvedValue(true))).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should filter the content", async () => {
|
|
|
|
const predicate = jest.fn().mockImplementation((value) => Promise.resolve(value === 2));
|
|
|
|
expect(await asyncFilter([1, 2, 3], predicate)).toEqual([2]);
|
|
|
|
});
|
|
|
|
});
|
2021-04-22 22:24:41 +00:00
|
|
|
});
|