Skip to content
This repository was archived by the owner on Jul 5, 2020. It is now read-only.

nieltg/lazy-transform-js

Repository files navigation

lazy-transform-js

Build Status Coverage Status code style: prettier npm

Ever wanted to use async functions in Array.map? This library may help you to do that.

import { transform } from "lazy-transform"

const iterable = transform([1, 2, 3])
  .mapAwait(async value => {
    // Some API calls here
    // ...

    return valueFromApi
  })

for await (const value of iterable) {
  console.log(value)
}

Installation

$ yarn add lazy-transform

API

transform(iterable)

Wrap an Iterable or AsyncIterable so library methods can be called to them. The wrapper itself is also an iterable.

const iterable = transform([1, 2, 3])
function* generator() {
  yield* [1, 2, 3]
}

const iterable = transform(generator())

for (const value of iterable) {
  // ...
}
async function* asyncGenerator() {
  yield* [1, 2, 3]
}

const asyncIterable = transform(asyncGenerator())

for await (const value of asyncIterable) {
  // ...
}

Wrapper.map(func) or .mapAwait(asyncFunc)

Create a new iterable with the results of calling func or asyncFuncon every item in the previous iterable.

const iterable = transform([1, 2, 3]).map(value => value + 5)
const asyncIterable = transform([1, 2, 3]).mapAwait(async value => value + 5)

Improvements

  • Add .collect method which returns an array or something.
  • Add more methods, like .reduce, .peek, etc.
  • Add Scheduler concept for every awaiters so passed async functions can be called not just in serial.

License

MIT

About

Transform an iterable into another iterable with different return value lazily.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published