npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

gentoo

v3.0.0

Published

Generator tools

Readme

Gentoo - Generator tools

Tools for ES6 generators.

accum

accum(gen) -> Generator

Returns a new generator which yields an array accumulating the values from gen. Every time next() is called, the newest value from gen is added to the end of the array the generator yields.


function * genInfinite () {
  let i = 0
  while (true) {
    yield ++i
  }
}

const accumGen = gentoo.accum(genInfinite())

accumGen.next().value
// [1]

accumGen.next().value
// [1, 2]

accumGen.next().value
// [1, 2, 3]

compose

compose(...generators) -> Generator

Compose any number of generators. The returned generator will yield the values of the first generator until it's done, then the values of the second until it's done, etc.


function * gen1 () {
  yield '🍕'
}

function * gen2 () {
  yield '🍤'
}

function * gen3 () {
  yield '🍓'
}

const composed = gentoo.compose(gen1(), gen2(), gen3())

[...composed]

// ['🍕', '🍤', '🍓']

dedupe

dedupe(gen [, eqFn]) -> Generator

Returns a generator which iterates over the values from gen and yields values which are different than the previous value. eqFn may optionally be passed, to evaluate the equality of two values. By default, === is used.

function dupeGenerator () {
  yield '😎'
  yield '😎'
  yield '😳'
  yield '😳'
  yield '😅'
  yield '😅'
}

const dedupeGen = gentoo.dedupe(dupeGenerator())

[...dedupeGen]
// ['😎', '😳', '😅']

filter

filter(gen, fn [, thisValue]) -> Generator

Returns a generator which will iterate over values from gen until fn returns a truthy result.

Every time next() is called, a value is retrieved fromgen and passed to fn, until the result of calling fn is truthy. At that point, the value from gen will be yielded.

thisValue can optionally be passed in, for the context fn is called in.


function gen () {
  yield 1 
  yield 2 
  yield 3 
}

function even (n) {
  return n % 2 === 0
}

const filterGen = gentoo.filter(gen(), even)

filterGen.next().value
// 2

forEach

forEach(gen, fn [, thisValue]) -> void

Calls fn for each value of gen.

thisValue can optionally be passed in, for the context fn is called in.

function gen () {
  yield '🍉'
  yield '🍜'
  yield '🍔'
}

gentoo.forEach(gen(), (n) => console.log(n))

// logs "🍉🍜🍔"

lastValue

lastValue(gen) -> [any]

Returns the last value from gen. NOTE: you should only pass lastValue a generator which will end. If gen has an infinite number of values, lastValue will never finish.


function gen () {
  yield '🚤'
  yield '🚁'
  yield '👑'
}

gentoo.lastValue(gen())
// '👑'

map

map(gen, fn [, thisValue]) -> Generator

Returns a generator that maps fn over the values of gen. Every time next() is called, the next value of gen will be passed to fn, and the result will be yielded.

thisValue can optionally be passed in, for the context fn is called in.

function gen () {
  yield '🍪'
  yield '🍩'
  yield '🍟'
}

function repeat (n) {
  return n + n
}

const repeatGen = gentoo.map(gen(), repeat)

[...repeatGen]
// ['🍪🍪', '🍩🍩', '🍟🍟']

nthValue

nthValue(gen, n) -> [any]

Returns the nth value (zero-based) from gen.

function gen () {
  yield '📀'
  yield '📹'
  yield '🎈'
}

gentoo.nthValue(gen(), 1)
// '📹'

partition

partition(gen, fn) -> Generator

Returns a generator that partitions the values from gen into two arrays: those for which fn returns a truthy value, and those for which fn returns a falsey value.

function * gen () {
  yield 1
  yield 2
  yield 3
}

function even (n) {
  return n % 2 === 0
}

const partitionGen = gentoo.partition(gen(), even)

partitionGen.next().value
// [[], [1]]

partitionGen.next().value
// [[2], [1]]

partitionGen.next().value
// [[2], [1, 3]]

pluck

pluck(gen, name) -> Generator

Returns a generator that plucks the property name from each of gen's values. Every time next() is called, the next value from gen is retrieved, and the name property of that value is yielded.

function * gen () {
  yield {animal: '🐮', flower: '🌷', tree: '🌲'}
  yield {animal: '🐗', flower: '🌹', tree: '🌳'}
  yield {animal: '🐵', flower: '🌺', tree: '🌴'}
}

const pluckGen = gentoo.pluck(gen(), 'flower')

pluckGen.next().value
// '🌷'

pluckGen.next().value
// '🌹'

pluckGen.next().value
// '🌺'

skip

skip(gen, n) -> void

Reads n values from gen and throws them away.

function * genInfinite () {
  let i = 0
  while (true) {
    yield ++i
  }
}

const gen = genInfinite()

gentoo.skip(gen, 2)

gen.next().value
// 2

gen.next().value
// 3

gen.next().value
// 4

take

take(gen, n) -> Array

Takes n values from gen and returns the values in an array.

function * gen () {
  yield '🐍'
  yield '🌀'
  yield '🌊'
}

gentoo.take(gen(), 2)
// ['🐍', '🌀']

loop

loop(gen) -> Generator

Turns gen into an infinite loop. Yields the values from gen until gen is done, then yields those values forever in a loop.

function * gen () {
  yield 1
  yield 2
  yield 3
}

const looped = gentoo.loop(gen())

looped.next().value
// 1

looped.next().value
// 2

looped.next().value
// 3

looped.next().value
// 1

looped.next().value
// 2

looped.next().value
// 3

looped.next().value
// 1