A minimalist library that offers lazy functions to work with JavaScript collections (e.g., arrays, strings).
There are many libraries that reinvent how you transform collections of data in JS by using so many patterns and functions1 which are challenging to master, costly to execute, and alien to the Standard JavaScript.
Iterated offers a distinct and minimalist approach by providing:
map
, filter
, group
, etc.import it from 'iterated'
it.map([1, 2, 3], x => x * 2)
it.count('AAAABBBCCD')
pipe
function to transform data in a functional way.const result: Iterator<number> = it.pipe(
[
{ foo: 1, baz: 'x' },
{ foo: 2, baz: 'x' },
],
it.map(item => item['foo']) // Functions are auto-curried to be used in pipes
)
map
)
works exactly the same for arrays
, strings
, sets
, maps
,
and any future or custom data collection.it.find([1, 2, 3], 3)
it.find('123', '3')
async
support.
For example, you can fetch data from an API in the middle of a
pipe, and let Iterated internally transform your Iterator
to an
AsyncIterator
and continue processing the pipe.const result: AsyncIterator<{ ... }> = it.pipe(
[true, false, true],
it.map(async (x) => await fetch(...)),
it.await, // await each promise, returning an async iterator
it.filter(({ statusCode }) => statusCode == 200), // iterated is transparently handling the promise for you
)
Checkout the introduction to JavaScript Iterables with Iterated.
npm install iterated
Then, run it as follows:
import it from 'iterated'
it.map([1, 2, 3], x => x * 2)
it.count('AAAABBBCCD')
With pipes:
const result: Iterable<number> = it.pipe(
it.range(47),
it.pairs,
it.map(([a, b]) => a + b),
)
const aSet: Set<number> = it.pipe(
result,
it.set
)
You can create functions to be used with pipe
:
function doStuffWithDevices(devices: Iterable<Device>): Iterable<Device> {
// Loop devices and so something with them
}
const result = it.pipe([{ id: 'device-1' }], doStuffWithDevices)
Functions with multiple arguments require adaptation to work in a pipe (i.e. curry). You can achieve it simply by doing:
import { toPipe } from 'iterated'
function doStuffWithDevices(devices: Iterable<Device>, aParam: () => number): Iterable<Device> {
// Loop devices and so something with them
}
const doStuffWithDevicesPipe = toPipe(doStuffWithDevices)
const result = it.pipe([{ id: 'device-1' }], doStuffWithDevicesPipe(() => 5))
Checkout how we curry in our code for more intricate examples, like auto-handling
Iterator
and AsyncIterator
.
First, we want to validate the usefulness of the project with some engagement, like having a few users and feedback.
We should find together where to improve the API, ironing out inconsistencies and
adding more functions like tee
and zip
.
We are looking for the first contributors! Be an integral part of this project. Fixes, improvements, and issues are welcomed.
Functions should allow currying and working with sync and async iterables transparently, and have good Typescript support.
todo: CI/CD, github actions...
In order to publish this repo:
npm test && npm run test:type
package.json
npm run build
npm publish
npm run doc
This work is licensed under CC BY 4.0. The authors are:
Although there are many libraries with a similar purpose, none fully match our purpose: underscore, ramda, iterate-iterator, iterare, the new built-in methods.