0.2.1 • Published 7 years ago

generator-utilities v0.2.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Generator Utilities

A utility library for generating and processing sequences based on ES6 generators.

Usage

Install the package in your project

npm install --save generator-utilities

Use require or import to use the generator functions you need.

import { iterate, range } from "generator-utilities";

Or

const { iterate, range } = require("generator-utilities");

Alternatively, if you use the default import, you can take advantage of the chainable iterator functionality.

import { default as chain } from "generator-utilities";

Or

const chain = require("generator-utilities").default;

See below for explaination of how chaining works.

Purpose

This contains many generator functions for a range of utilities that are primarily divided into two categories: sequence generators and chainable generators. The sequence generators will take their input, if any, and use it to produce a sequence of values. The chainable generators take as input an iterable object and other parameters and yield (possibly modified) values until either some condition has been met, or the source iterator stops yielding values.

For example, the range generator yields a sequence of values between a start and end value.

for (value of range(-10, 11)) {
    console.log(value);
}

This will log the integers from -10 up to but not including 11. i.e. -10, -9, -8, ..., 8, 9, 10.

The take generator is a chainable generator that takes as input an iterable object, such as another generator or an Array, and yields the specified number of values. The counter sequence generator in this example will yield an infinite series of values from 0.

[...take(counter(), 10)]; // --> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The reason these are called chainable generators is because they can be used together via the chain export.

let it = chain.range(1, 10).map(n => n**2);
[...it]; // --> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

The chain export provides access to all sequence and chainable generators, and automatically exposes all chainable generators on each iterator returned in the chain.

Base Iterators

The base iterators simply yield values from a given source object without modifying them in any way.

GeneratorPurpose
iterateYeilds all values from the given object. Works with iterators, Arrays or Array-like objects.
reverseYields values in reverse from the given object. Works with Arrays or Array-like objects.
safeIterateLike iterate, but wraps a given iterator in a way that prevents a call to return() from being propagated to the wrapped iterator.

Illustrating how safeIterate works

e.g. Withinout safeIterate:

let a = counter();
for (let value of a) {
    if (value > 10) break;
}
a.next(); // --> { done: true }

With safeIterate:

let b = counter();
for (let value of safeIterate(b)) {
    if (value >= 10) break;
}
b.next(); // --> { value: 11, done: false }

Note: Take care with safeIterate. Use it only where you are sure that you don't want a call to return() to be propagated to the wrapped iterator, which ordinarily allows for cleanup procedures to occur when you are finished with it, such as closing open files.

Sequence Generators

GeneratorPurpose
combinationGiven a finite sequence of values, yields arrays with unique combinations of the specified size.
counterYields values starting from the given start value, or 0, incrementing by the specified step.
randomYields random integers or floating point numbers, optionally in a given range.
rangeYields values from start up to but not including end, optionally incrementing by the specified step.
repeatGiven a generator function or iterable object, yields all values from it and then repeats.
permuteGiven an Array or Array-like or finitely iterable object, yields arrays with unique permutations of all items.
permutationGiven an Array or Array-like or finitely iterable object, yields arrays of the specified size, k, with unique permutations of k items.
seriesGiven an initial set of n values and a function, yields values in a series that can be calculated from the n previous values. (see fibonacci series).
shuffleGiven an array, yields each value in a random order.
symbolsYields unique Symbol values.
thueMorseYields binary values in the Thue-Morse sequence.
collatzGiven an initial starting value, yields all values in the Collatz sequence until it reaches 1.
fibonacciGenerates values in the fibonacci sequence, not exceeding the MAX_SAFE_INTEGER.
fizzBuzzStarting from 1, yields values in the FizzBuzz sequence.

Chainable Generators

GeneratorPurpose
chunkYields arrays with n values from the given iterator.
compactRemoves falsey (false, 0, NaN, "", null, undefined) values and yeilds all other values from the given iterator.
concatYields all values from all given iterators in order
dedupeRemoves duplicate values that equal the previously yielded value
differenceYield values that are not included in a given reference array
differenceByLike difference, but maps each value in the given reference array before comparing with values from the generator.
differenceWithYields values that are not included in the given reference array, compared using the given comparison function.
dropIgnore n values from the given iterator and yield all subsequent values.
dropUntilIgnore values from the given iterator until a specified condition function returns true, and yield all subsequent values.
dropWhileIgnore values from the given iterator while a given condition function returns true
everyNthYield every nth value from the given iteratable.
filterYield values from the given iterable that pass a given condition.
flattenRecursively iterate and yield values from nested iterables, up to a specified depth.
forEachInvoke a callback for each value prior to yielding the original value.
fuzzYield values from the given iterator that have been mapped according to the rules of FizzBuzz.
interlaceAlternately yield values from each of the given iterators.
linkLink a custom generator function into a chainable sequence. The custom generator function must accept an iterable as its first parameter.
mapInvokes a callback with each value from the given iteratable and yields the results.
moduloYields the value (mod n) from the given iterable using Euclidian division. The result is always positive.
pluckYield values of the specified property from objects yielded by the given iteratable.
takeYield n values from the given iterable.
takeUntilYield values from the given iterable until a given condition returns true.
takeWhileYield values from the given iterable while a given condition returns true.
unzipYields arrays ungrouped values from a finite set of groups.
zipGroups values from multiple iterables.