Provides utility functions (map, reduce, and so on) that work on top of ES2015's generators. Requires node 4 or above.
npm install js-itertoolsmap, reduce, filter, and so on) that work on top of ES2015's generators, and a few others. Similar to .NET's System.Linq and Java's Stream API. These use the for of construct semantics.
npm install js-itertools
PredicateFunction = (item: T, index?: number) => boolean
SelectorFunction = (item: T, index?: number) => U
UnitFunction = () => T
ReducerFunction = (accumulated: U, item: T) => U
KeyFunction = (item: T) => K
Grouping = { key: K, [Symbol.iterator](): Iterator }
Action = (item: T) => void
(source: Iterable) : The default function. Takes an existing source, such as an Array, and allows chained usage of the functions below.
repeat(item: T, count?: number) : Returns a generator that returns the item count times.
range(start: number, count?: number): Returns count numbers, starting from start.
between(start: number, end: number, inclusive?: boolean): Returns numbers between start and end.
map(fn: SelectorFunction): ChainableIterable : Similar to Array.prototype.map, returns a generator that calls fn for each item in the generator's source, and yields the result.
flatMap(fn: SelectorFunction>): ChainableIterable : Similar to map, but flattens the resulting iterable (non-recursive).
reduce(fn: ReducerFunction, defaultValue?: U): U : Similar to Array.prototype.reduce, reduces a generator to a single value.
zip(...iterables: Iterable[]): ChainableIterable> : Combines this iterable with the supplied iterables into a single one.
groupBy(keySelector: KeyFunction): ChainableIterable> : Groups items by a key.
sortedGroupBy(keySelector: KeyFunction): ChainableIterable> : Alternate version of groupBy, optimized for sources that are known to be sorted by the key.
filter(fn: PredicateFunction): ChainableIterable : Similar to Array.prototype.filter, returns a generator whose items are those that pass the predicate.
take(count: number): ChainableIterable: Takes at most N items from the source (or less, if the source doesn't contain enough items).
takeWhile(fn: PredicateFunction): ChainableIterable : Similar to filter, but only yields items until the first item that fails the predicate.
skip(count: number): ChainableIterable: The opposite of take.
skipWhile(fn: PredicateFunction): ChainableIterable : The opposite of takeWhile.
some(fn?: PredicateFunction): boolean : Similar to Array.prototype.some, returns whether the source contains any matching item. If the predicate function is ommited, the source is tested for the existance of any item.
none(fn?: PredicateFunction): boolean : The opposite of some.
every(fn: PredicateFunction): boolean : Similar to Array.prototype.every, returns true if all items pass the predicate function.
includes(item: T): boolean: Similar to Array.prototype.includes.
single(predicate?: PredicateFunction): T : Returns the only item that matches the predicate function. Throws if more than one item is found.
first(fn?: PredicateFunction): T : Returns the first item that matches the predicate function.
last(fn?: PredicateFunction): T : Returns the last item that matches the predicate function.
count(fn?: PredicateFunction): number : Returns the number of items in the source.
toArray(): Array: Calls Array.from with the source.
toSet(): Set: Calls the Set constructor with the source.
toMap(keySelector: KeyFunction, valueSelector: KeyFunction): Map : Calls the Map selector with the result of the key selector function and the value selector function.
forEach(fn: Action): void : Similar to Array.prototype.forEach, calls the function for each item on the source.
for of: The for of loop can be used to iterate over the resulting iterable as well.
Promise synchronization (see: co and others). They can also be used for data processing.
Array's methods allocate intermediate arrays at each call. By using generators, object creation can be reduced, which can (haven't tested it) improve performance and reduce memory usage in some scenarios.