Package detail

gensequence

streetsidesoftware3.9mMIT8.0.8

Small library to simplify working with Generators and Iterators in Javascript / Typescript

generators, generator, iterator, iterators

readme

GenSequence

Coverage Status

Small library to simplify working with Generators and Iterators in Javascript / Typescript

Javascript Iterators and Generators are very exciting and provide some powerful new ways to solve programming problems.

The purpose of this library is to make using the results of a generator function easier. It is not intended as a replacement for arrays and the convenient [...genFn()] notation. GenSequence is useful for cases where you might not want an array of all possible values. GenSequence deals efficiently with large sequences because only one element at a time is evaluated. Intermediate arrays are not created, saving memory and cpu cycles.

Installation

npm install -S gensequence

Usage

import { genSequence } from 'gensequence';

Examples

Fibonacci

The Fibonacci sequence can be very simply expressed using a generator. Yet using the result of a generator can be a bit convoluted. GenSequence provides a wrapper to add familiar functionality similar to arrays.

import { genSequence, Sequence } from '../index.js';

export function fibonacci(): Sequence<number> {
  function* fib() {
    let [a, b] = [0, 1];
    while (true) {
      yield b;
      [a, b] = [b, a + b];
    }
  }
  return genSequence(fib());
}

export function fib(n: number) {
  return fibonacci()
    .take(n) // Take n from the fibonacci sequence
    .toArray(); // Convert it into an array
}

export const fib5 = fib(5); // [1, 1, 2, 3, 5]

RegEx Match

Regular expressions are wonderfully powerful. Yet, working with the results can sometimes be a bit of a pain.

import { genSequence } from '../GenSequence.js';

function matchWords(text: string) {
  return genSequence(text.matchAll(/\w+/g)).map((a) => a[0]);
}

export function toSetOfWords(text: string) {
  return new Set(matchWords(text));
}

export const text = 'Some long bit of text with many words, duplicate words...';
export const setOfWords = toSetOfWords(text);
// Walk through the set of words and pull out the 4 letter one.
export const setOf4LetterWords = new Set(genSequence(setOfWords).filter((a) => a.length === 4));

Reference

  • genSequence(Iterable|Array|()=>Iterable) -- generate a new Iterable from an Iterable, Array or function with the following functions.

Filters

  • .filter(fn) -- just like array.filter, filters the sequence
  • .skip(n) -- skip n entries in the sequence
  • .take(n) -- take the next n entries in the sequence.

Extenders

  • .concat(iterable) -- this will extend the current sequence with the values from iterable
  • .concatMap(fnMap) -- this is used to flatten the result of a map function.

Mappers

  • .combine(fnCombiner, iterable) -- is used to combine values from two different lists.
  • .map(fn) -- just like array.map, allows you to convert the values in a sequence.
  • .pipe(...operatorFns) -- pipe any amount of operators in sequence.
  • .scan(fn, init?) -- similar to reduce, but returns a sequence of all the results of fn.

Reducers

  • .all(fn) -- true if all values in the sequence return true for fn(value) or the sequence is empty.
  • .any(fn) -- true if any value in the sequence exists where fn(value) returns true.
  • .count() -- return the number of values in the sequence.
  • .first() -- return the next value in the sequence.
  • .first(fn) -- return the next value in the sequence where fn(value) return true.
  • .forEach(fn) -- apply fn(value, index) to all values.
  • .max() -- return the largest value in the sequence.
  • .max(fn) -- return the largest value of fn(value) in the sequence.
  • .min() -- return the smallest value in the sequence.
  • .min(fn) -- return the smallest value of fn(value) in the sequence.
  • .reduce(fn, init?) -- just like array.reduce, reduces the sequence into a single result.
  • .reduceAsync(fn, init?) -- just like array.reduce, reduces promises into the sequence into a single result chaining the promises, fn/init can be async or not, it will work, the previousValue, and currentValue will never be a promise.
  • .reduceToSequence(fn, init) -- return a sequence of values that fn creates from looking at all the values and the initial sequence.

Cast

  • .toArray() -- convert the sequence into an array. This is the same as [...iterable].
  • .toIterable() -- Casts a Sequence into an IterableIterator - used in cases where type checking is too strict.

changelog

Release Notes

8.0.8 (2025-11-07)

Bug Fixes

  • Try to fix trigger when a release is created (33d2ee2)

8.0.7 (2025-11-06)

Bug Fixes

  • Use release published to trigger publish to NPM (1b63e5a)

8.0.6 (2025-11-06)

Bug Fixes

  • correct permissions (1247bfa)
  • Fix release please to use publish workflow (cec39c3)

8.0.5 (2025-11-06)

Bug Fixes

  • Try to get release-please to publish (054a3d7)

8.0.4 (2025-11-06)

Bug Fixes

  • edit to force version change. (5222ea5)

8.0.3 (2025-11-06)

Bug Fixes

8.0.2 (2025-11-06)

Bug Fixes

8.0.1 (2025-11-06)

Bug Fixes

8.0.0 (2025-07-11)

⚠ BREAKING CHANGES

  • drop support for Node 18 (#717)

Features

7.0.0 (2024-02-27)

⚠ BREAKING CHANGES

  • Drop support for CommonJS (#487)
  • Drop support for Node 16 (#485)

Features

6.0.0 (2023-08-24)

⚠ BREAKING CHANGES

  • Drop support for Node 14 (#380)

Features

5.0.2 (2023-02-28)

Bug Fixes

  • Do not package redundant .js and .d.ts files (cd5afd0)

5.0.1 (2023-02-28)

Bug Fixes

  • do not package .tsbuildInfo (4ae236f)

5.0.0 (2023-02-28)

⚠ BREAKING CHANGES

  • Support ESM (#283)

Features

4.0.3 (2022-12-08)

Bug Fixes

4.0.2 (2022-08-30)

Bug Fixes

4.0.1 (2022-08-30)

Bug Fixes

  • clean up the async reducers (#21) (0bf7525)
  • correctly handle undefined initial value. (0bf7525)
  • Sequence<T extends S> is type compatible with Sequence (#25) (2834953)
  • Update Publication Process (483732c)
  • Update Typescript (#228) (ae21653)

Miscellaneous Chores

[4.0.0]

  • Drop support for Node 10 and 12.

[3.1.0]

[3.0.0]

  • Supports Node >= 10
  • Add the ability to pipe operators

[2.2.0]

  • Move operator functions out of GenSequence
  • Update to TypeScript

[2.1.3]

  • Update dev packages to address issues with code coverage generation in Node 10

2.1.1

  • Update dev packages
  • add forEach function

2.1.0

  • fix a function signature issue surfaced by typescript 2.4.2

2.0.1

  • minor update to README.md.
  • added test showing how it is not possible to reuse some iterators.

2.0.0

  • sequences are now reusable as long as the original iterator is reusable.
  • it is not possible to initialize a sequence with a function that returns an iterator This is very powerful.
  • .count() added.
  • special thanks to @sea1jxr for all of the above.

1.3.0

  • Refactor the methods to give them a logical grouping - thanks to @sea1jxr

1.2.0

  • Added min and max - thanks to @sea1jxr

1.1.0

  • Added all and any - thanks to @sea1jxr

1.0.0

  • Added full test coverage
  • Fix an issue with scan and working with arrays.
  • Fix the .next() function to correctly work with arrays.
  • Sequence supports both Iterable<T> and IterableIterator<T> interfaces

0.1.0 - 0.2.4

  • These were the initial set of releases