Skip to content

Commit

Permalink
fix(filter): Support narrowing by type predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
4513ECHO committed Aug 20, 2024
1 parent 85fd83b commit ec71a49
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions async/filter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export function filter<T>(
iterable: Iterable<T> | AsyncIterable<T>,
fn: (value: T, index: number) => boolean | Promise<boolean>,
): AsyncIterable<T>;
export function filter<T, U extends T>(
iterable: Iterable<T> | AsyncIterable<T>,
fn: (value: T, index: number) => value is U,
): AsyncIterable<U>;
/**
* Filters an iterable based on a function.
*
Expand Down
8 changes: 8 additions & 0 deletions filter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export function filter<T>(
iterable: Iterable<T>,
fn: (value: T, index: number) => boolean,
): Iterable<T>;
export function filter<T, U extends T>(
iterable: Iterable<T>,
fn: (value: T, index: number) => value is U,
): Iterable<U>;
/**
* Filters an iterable based on a function.
*
Expand Down
6 changes: 6 additions & 0 deletions pipe/async/filter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { filter as base } from "../../async/filter.ts";

export function filter<T>(
fn: (value: T, index: number) => boolean | Promise<boolean>,
): (iterable: Iterable<T> | AsyncIterable<T>) => AsyncIterable<T>;
export function filter<T, U extends T>(
fn: (value: T, index: number) => value is U,
): (iterable: Iterable<T> | AsyncIterable<T>) => AsyncIterable<U>;
/**
* Returns an operator that filters an iterable based on a function.
*
Expand Down
6 changes: 6 additions & 0 deletions pipe/filter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { filter as base } from "../filter.ts";

export function filter<T>(
fn: (value: T, index: number) => boolean,
): (iterable: Iterable<T>) => Iterable<T>;
export function filter<T, U extends T>(
fn: (value: T, index: number) => value is U,
): (iterable: Iterable<T>) => Iterable<U>;
/**
* Returns an operator that filters an iterable based on a function.
*
Expand Down

0 comments on commit ec71a49

Please sign in to comment.