/** @module iterable/filterIterableWithIndex.ts */ import { untypedCurry } from '../function/untypedCurry' import { FilterIterable } from './filterIterable' type PredicateWithIndex = ( item: A, index: number ) => boolean export function filterIterableWithIndex( fn: PredicateWithIndex, itr: Iterable ): A[] export function filterIterableWithIndex( fn: PredicateWithIndex, itr: NodeListOf ): A[] export function filterIterableWithIndex( fn: PredicateWithIndex ): FilterIterable export function filterIterableWithIndex( fn: PredicateWithIndex ): 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 filterIterableWithIndex( ...arg ) { return untypedCurry( ( fn, itr ) => { const arr: any[] = [] let index = 0 for ( const i of itr ) { if ( fn( i, index ) === true ) { arr.push( i ) } index++ } return arr } ) }