Skip to content

Commit

Permalink
test: Add tests for filter with type predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
4513ECHO committed Aug 20, 2024
1 parent ec71a49 commit ed13790
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions async/filter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ await test("filter with iterable with promise", async () => {
assertEquals(indices, [0, 1, 2, 3, 4]);
assertType<IsExact<typeof result, AsyncIterable<number>>>(true);
});

await test("filter with type predicate", async () => {
const predicate = (v: number | string): v is number => typeof v === "number";
const result = filter([1, "a", 2, "b", 3], predicate);
const expected = [1, 2, 3];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number>>>(true);
});
8 changes: 8 additions & 0 deletions filter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ test("filter", () => {
assertEquals(indices, [0, 1, 2, 3, 4]);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});

test("filter with type predicate", () => {
const predicate = (v: number | string): v is number => typeof v === "number";
const result = filter([1, "a", 2, "b", 3], predicate);
const expected = [1, 2, 3];
assertEquals(Array.from(result), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});
11 changes: 11 additions & 0 deletions pipe/async/filter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ test("filter usage", async () => {
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number>>>(true);
});

test("filter usage with type predicate", async () => {
const predicate = (v: number | string): v is number => typeof v === "number";
const result = pipe(
[1, "a", 2, "b", 3],
filter(predicate),
);
const expected = [1, 2, 3];
assertEquals(await Array.fromAsync(result), expected);
assertType<IsExact<typeof result, AsyncIterable<number>>>(true);
});
11 changes: 11 additions & 0 deletions pipe/filter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ test("filter usage", () => {
assertEquals(Array.from(result), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});

test("filter usage with type predicate", () => {
const predicate = (v: number | string): v is number => typeof v === "number";
const result = pipe(
[1, "a", 2, "b", 3],
filter(predicate),
);
const expected = [1, 2, 3];
assertEquals(Array.from(result), expected);
assertType<IsExact<typeof result, Iterable<number>>>(true);
});

0 comments on commit ed13790

Please sign in to comment.