0.0.2 • Published 6 years ago

lazy-transform v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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

0.0.2

6 years ago

0.0.1

6 years ago