Function filterIterator

  • Given an IteratorArg and a filtering function it will return a new IterableIterator that when polled will eagerly pull values from iterator and pass them to filterer until filterer returns true for an item, or we reach the end of iterator; it will then yield this value itself.

    NOTE: if iterator relies on values being passed to next, the returned IterableIterator will re-use the same next until a value passes the filterer. value until an item is raised that passes the filterer. This can lead to strange behaviours.

    Type Parameters

    • TValue

      The value returned from the iterator

    • TReturn = undefined | TValue

      If your iterator has a return type it must be the same as TIteratorValue, otherwise undefined

    • TNext = any

      The type your iterator is expecting as what's passed to its next function. For generators this is the type returned after a yield.

    Parameters

    • iterator: IteratorArg<TValue, TReturn, TNext>

      The iterator you wish to map elements from

    • filterer: ((arg) => boolean)

      Only values for which this function returns true are yielded.

        • (arg): boolean
        • Parameters

          • arg: TValue

          Returns boolean

    Returns AugmentedIterator<TValue, TReturn, TNext>

    Example

    const set = new Set([1, 2, 3, 4, 5]);

    const filtered = filterIterator(set, (item) => item % 2 === 0);

    console.log(Array.from(filtered)); // logs out [2, 4]

Generated using TypeDoc