JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 9
  • Score
    100M100P100Q30519F
  • License MIT

Functions to work with JavaScript generators.

Package Exports

  • generator-utils

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (generator-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

generator-utils

Utility functions for manipulating generators as lazy sequences.

Install

# via yarn
$ yarn add generator-utils
# via npm
$ npm install generator-utils

Usage

In a node.js environment, e.g. import { filter } from 'generator-utils'; or const { filter } = require('generator-utils');.

# combine(generators)

Creates a new generator yielding all in-order combinations of values from the given generators. All values from all generators will be read, so do not use this with infinite generators.

toArray(combine([range(0, 1), range(4, 5)]))
// [[0, 4], [0, 5], [1, 4], [1, 5]]

# concat(generators)

Returns a generator yielding all the values from the given generators in order. If any generators are infinite generators none of the values from subsequent generators will be read.

toArray(concat([range(0, 1), range(4, 5)]))
// [0, 1, 4, 5]

# filter(generator, transform)

Returns a generator that yields values from another generator passing a predicate. This may be used with infinite generators, though care should be taken to ensure that the predicate does not return false for all values.

toArray(filter(range(0, 5), x => x % 2 === 0))
// [0, 2, 4]

# filterMap(generator, transform)

Combines filter and map in one transform. Instead of returning false, filtering is done by calling the skip function passed as the second argument to transform.

toArray(filter(range(0, 5), (x, skip) => (x % 2 === 0) ? skip() : x * x))
// [1, 9, 25]

# flatten(generator)

Makes a generator from a generator producing other generators.

toArray(flatten(fromArray([range(2, 5), range(6, 7))))
// [2, 3, 4, 5, 6, 7]

# forEach(generator, iterator)

Calls a function for each value in a generator.

forEach(range(1, 4), console.log)
// prints "1\n2\n3\n4\n"

# fromArray(array)

Returns a generator yielding the values from the given array in order.

toArray(fromArray([1, 2, 3]))
// [1, 2, 3]

# map(map, transform)

Maps one generator to another by passing all values through a transformer.

toArray(map(range(2, 5), x => x * 2))
// [4, 6, 8, 10]

# range(min, max)

Returns a generator yielding values from min up to and including max.

toArray(range(8, 10))
// [8, 9, 10]

# take(generator, count)

Returns up to the first count values of a generator.

take(range(10, 20), 3)
// [10, 11, 12]

# toArray(generator)

Reads all values from a generator and returns an array containing them. Be careful not to use this with infinite generators, as it will never return and your program will eventually run out of memory.

toArray(range(7, 9))
// [7, 8, 9]