Skip to content

Commit

Permalink
refactor: change predicate's return type to just Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinsiva committed Jan 30, 2024
1 parent 3d7b0fe commit 876463b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
21 changes: 6 additions & 15 deletions src/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,37 +192,28 @@ describe("array", () => {
});

describe("asyncSome", () => {
it("returns true if any element matches async predicate (even number)", async () => {
it("returns true if any element matches predicate (even number)", async () => {
// given an array of even and odd numbers
const a = [1, 2, 3, 4, 5];
// when we call asyncSome with an async predicate that checks for even numbers
// when we call asyncSome with a predicate that checks for even numbers
const result = await a.asyncSome((el) => Promise.resolve(el % 2 === 0));
// then expect asyncSome to return true
expect(result).toBe(true);
});

it("returns true if any element matches sync/async predicate (1 or even number)", async () => {
// given an array of even and odd numbers which includes 1
const a = [1, 2, 3, 4, 5];
// when we call asyncSome with a sync/async predicate that checks for 1 or even numbers
const result = await a.asyncSome((el) => el === 1 || Promise.resolve(el % 2 === 0));
// then expect asyncSome to return true
expect(result).toBe(true);
});

it("returns false if no elements match sync/async predicate (7 or even number)", async () => {
it("returns false if no elements match predicate (even number)", async () => {
// given an array of odd numbers
const a = [1, 3, 5];
// when we call asyncSome with a sync/async predicate that checks for 7 or even numbers
const result = await a.asyncSome((el) => el === 7 || Promise.resolve(el % 2 === 0));
// when we call asyncSome with a predicate that checks for even numbers
const result = await a.asyncSome((el) => Promise.resolve(el % 2 === 0));
// then expect asyncSome to return false
expect(result).toBe(false);
});

it("returns false if the array is empty", async () => {
// given an empty array
const a: number[] = [];
// when we call asyncSome with an async predicate that checks for even numbers
// when we call asyncSome with a predicate that checks for even numbers
const result = await a.asyncSome((el) => Promise.resolve(el % 2 === 0));
// then expect asyncSome to return false
expect(result).toBe(false);
Expand Down
9 changes: 2 additions & 7 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,9 @@ Array.prototype.asyncFilter = async function <T>(

Array.prototype.asyncSome = async function <T>(
this: Array<T>,
predicate: (v: T) => boolean | Promise<boolean>,
predicate: (v: T) => Promise<boolean>,
): Promise<boolean> {
const asyncResults: Promise<boolean>[] = [];
for (const el of this) {
const result = predicate(el);
if (result === true) return true;
if (result instanceof Promise) asyncResults.push(result.then((r) => r || Promise.reject()));
}
const asyncResults = this.map((el) => predicate(el).then((result) => result || Promise.reject()));
return await Promise.any(asyncResults).catch(() => false);
};

Expand Down

0 comments on commit 876463b

Please sign in to comment.