generator-extensions
v1.0.6
Published
_Like Reactive Extensions for Generators_
Downloads
2
Readme
Generator Extensions
Like Reactive Extensions for Generators
Installation
Add the library to your project
npm install --save generator-extensions
Activate the extensions
require('generator-extensions')
Use the synchronous extensions
function* range(a, b) {
for (let i = a; i < b; i++) {
yield i
}
}
const zero = range(10,20)
.flatMap(x => [x, -x])
.map(x => x * 2)
.reduce((a, b) => a + b)
Use the asynchronous extensions
async function* range(a, b) {
for (let i = a; i < b; i++) {
yield i
}
}
const zero = range(10,20)
.flatMap(x => [x, -x])
.map(x => x * 2)
.reduce((a, b) => a + b)
Operators
toArray
Collects the generator into a single array.
flatMap
The flatMap() method first maps each element using a mapping function, then flattens the result into a new generator.
flat
The flat([depth = 1]) flattens each element to a specified depth into a new generator.
map
The map() method creates a new generator with the results of calling a provided function on every element in the calling generator.
entries
The entries() method returns a new generator that contains the key/value pairs for each index in the generator.
filter
The filter() method creates a new generator with all elements that pass the test implemented by the provided function.
find
The find() method returns the value of the first element in the provided generator that satisfies the provided testing function.
findIndex
The findIndex() method returns the index of the first element in the generator that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
keys
The keys() method returns a new generator that contains the keys for each index in the generator.
reduce
The reduce() method executes a reducer function (that you provide) on each element of the generator, resulting in a single output value.
some
The some() method tests whether at least one element in the generator passes the test implemented by the provided function. It returns a Boolean value.
every
The every() method tests whether all elements in the generator pass the test implemented by the provided function. It returns a Boolean value.
tap
Execute a function for each element without changing the value emitted.
count
forEach
The forEach() method executes a provided function once for each generator element.
parallel (async only)
The parallel() method executes a provided async function in parallel with other elements, up to a limit provided by the second parameter (default: Infinity). The resulting generator may be in a different order than the source generator.