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 🙏

© 2024 – Pkg Stats / Ryan Hefner

generator-utils

v2.1.0

Published

Functions to work with JavaScript generators.

Downloads

13

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]