Add asyncFilter

This commit is contained in:
Florian Duros
2024-11-14 17:54:54 +01:00
parent 9a126795a8
commit 6533a6b642
2 changed files with 22 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ import {
concat,
asyncEvery,
asyncSome,
asyncFilter,
} from "../../../src/utils/arrays";
type TestParams = { input: number[]; output: number[] };
@@ -460,4 +461,15 @@ describe("arrays", () => {
expect(predicate).toHaveBeenCalledWith(2);
});
});
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]);
});
});
});