awesome-iter
Gives iterables & iterators a facelift by allowing you to manipulate them like an array or stream of values.
npm install @aloreljs/awesome-iter
![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Alorel/awesome-iter-js.svg?logo=lgtm&logoWidth=18)
Table of Contents
Motivation
Arrays have useful functions, iterables don't. While you could just transform an iterable into an array, that results
in multiple iterations:
const someSet: Set<string> = getSomeSet();
const uppercased: Set<string> = new Set(
[...someSet]
.filter(str => str !== 'foo')
.map(str => str.toUpperCase())
);
This library allows you to work with iterables directly:
import {AwesomeIter} from '@aloreljs/awesome-iter';
import {concat, filter, map} from '@aloreljs/awesome-iter/pipes';
import {toSet} from '@aloreljs/awesome-iter/consumers';
const someSet: Set<string> = getSomeSet();
const someMap: Map<number, string> = getSomeMap();
const someArray: string[] = getSomeArray();
const uppercasedSet: Set<string> = new AwesomeIter(someSet)
.pipe(
concat(someMap.values(), someArray),
filter(str => str !== 'foo'),
map(str => str.toUpperCase())
)
.collect(toSet());
Alternatives
- For any async data just use Rxjs - it's awesome & it has more functionality.
- I'm unaware of similar libraries for synchronous iterables & iterators. This one is very lean & tree-shakeable!
API
AwesomeIter
constructor()
Just pass an Iterable
or Iterator
and AwesomeIter
will be able to start managing it.
pipe()
Pipes manipulate the source Iterable
or Iterator
in some way - filter results, map them to different values etc.
Each pipe has documentation & examples written for it in the
projects/awesome-iter/pipes
directory and is imported from @aloreljs/awesome-iter/pipes
, e.g.
import {filter} from '@aloreljs/awesome-iter/pipes';
The current list of pipes is as follows:
chunk
concat
distinct
filter
map
sequentiallyDistinct
skip
take
tap
consume()
Consumers consume the source Iterable
or Iterator
& produce a single result - these include functions like those
in Array.prototype
that don't necessarily return an array & functions for collecting the source into some JS
collection. Each consumer has documentation & examples written for it in the
projects/awesome-iter/consumers
directory and is imported from @aloreljs/awesome-iter/consumers
, e.g.
import {some} from '@aloreljs/awesome-iter/consumers';
The current list of consumers is as follows:
count
find
first
includes
last
reduce
some
split
toArray
toMap
toObject
toSet