UNPKG

@lou.codes/iterables

Version:
26 lines (25 loc) 815 B
import type { Predicate, ReadOnlyIterable, Single, Unary, } from "@lou.codes/types"; /** * Filters items in an iterable against a predicate and returns items that * evaluated to `true`. * * @category Generators * @example * ```typescript * const filterEven = filter((number: number) => number % 2 === 0); * * iterableToArray(filterEven([1, 2, 3, 4])); // [2, 4] * iterableToArray(filterEven([1, 3, 5, 7])); // [] * ``` * @param predicate Predicate function to evaluate each item. * @returns Curried function with `predicate` set in context. */ export declare const filter: <Item, Filtered extends Item = never>( predicate: Single<Filtered> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Filtered>, ) => (iterable: ReadOnlyIterable<Item>) => Readonly<IterableIterator<Filtered>>;