/** @module iterable/filterIterable.ts */ import { untypedCurry } from '../function/untypedCurry' import { Predicate } from '../helper-types' export type FilterIterable = A extends Node ? ( itr: NodeListOf ) => A[] : ( itr: Iterable ) => A[] export function filterIterable( fn: Predicate, itr: Iterable ): A[] export function filterIterable( fn: Predicate, itr: NodeListOf ): A[] export function filterIterable( fn: Predicate ): FilterIterable export function filterIterable( fn: Predicate ): FilterIterable /** * Filter an iterable, return an array with filtered items * @param fn Predicate takes an item from the list and the items index * @param itr Iterable containing items * @sig ( item -> bool ) -> iterable -> n * @example * indexOf( {a: 10}, [ {a: 9}, {a: 10}, {a: 11}] ) * //=> 1 */ export function filterIterable( ...arg ) { return untypedCurry( ( fn, itr ) => { const arr: any[] = [] for ( const i of itr ) { if ( fn( i ) === true ) { arr.push( i ) } } return arr } ) }