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

laziness

v1.0.8

Published

Lazy evaluation utilities for JavaScript powered by ES6 iterators and generators

Downloads

10

Readme

laziness

Lazy evaluation utilities for JavaScript powered by ES6 iterators and generators.

Well-tested and with Flow type definitions included.

Compatible with Node v6.11.2 LTS or later.

Installation

npm install --save laziness

Usage example

const { default: Laziness, range } = require('laziness');

// logs numbers 0, 1, ..., 9 one per line
for (const x of range(0, 10)) {
  console.log(x);
}

// the same
Laziness.from(range(0, 10))
  .forEach(x => console.log(x));
function* fib() {
  yield 1;
  yield 1;
  const [fib1, fib2] = Laziness.from(fib()).tee();
  yield* fib1.map2((x, y) => x + y, fib2.tail());
}
fib() // 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

API

Table of Contents

INFINITE ITERATORS

Infinite Iterators

cycle

Generates all elements of the iterable. Once the original iterable is exhausted, yields all elements again. Repeats indefinitely.

Parameters

  • iter Iterable<T>

Examples

cycle('ABC') // 'A', 'B', 'C', 'A', 'B', 'C', ...

Returns Generator<T, void, void>

range

Parameters

Examples

range(0, Infinity) // 0, 1, 2, ...

Returns Generator<number, void, void>

repeat

Repeats value endlessly or up to limit times.

Parameters

  • value T
  • limit number (optional, default Infinity)

Examples

repeat(5) // 5, 5, 5, 5, ...
repeat(10, 3) // 10, 10, 10

Returns Generator<T, void, void>

UTILITIES

Basic functions

filter

Analogical to Array.prototype.filter

Parameters

  • iter Iterable<T>
  • callback function (T): boolean

Returns Generator<T, void, void>

forEach

Analogical to Array.prototype.forEach

Parameters

  • iter Iterable<T>
  • callback function (T): void

Returns void

map

Analogical to Array.prototype.map

Parameters

  • iter Iterable<T>
  • callback function (T): U

Returns Generator<U, void, void>

map2

Generates values of the function when applied arguments from each of the iterables. Stops when the shortest iterable is exhausted.

Parameters

  • callback function (U, V): T
  • iter1 Iterable<U>
  • iter2 Iterable<V>

Examples

map2((x, y) => x + y, range(1, 4), range(10, 40, 10)) // 11, 22, 33

Returns Generator<T, void, void>

slice

Analogical to Array.prototype.slice

Parameters

Returns Generator<T, void, void>

tail

Skips first element of iterable

Parameters

  • iter Iterable<T>

Returns Generator<T, void, void>

tee

Clone interable into n independent instances

Parameters

  • iter Iterable<T>
  • n number (optional, default 2)

Returns Array<Generator<T, void, void>>

LAZINESS WRAPPER

Handy wrapper for common operations on iterables.

Laziness

Convenient wrapper for chaining calls to library functions.

You can also use it as iterable.

Parameters

  • iter Iterable<T>

Examples

Array.from(new Laziness([1, 2, 3])) // 1, 2, 3
function* fib() {
  yield 1;
  yield 1;
  const [fib1, fib2] = Laziness.from(fib()).tee();
  yield* fib1.map2((x, y) => x + y, fib2.tail());
}
fib() // 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

filter

See filter

Parameters

Returns Laziness<T>

forEach

See forEach

Parameters

  • callback function (T): void

map

See map

Parameters

  • callback function (T): U

Returns Laziness<U>

map2

See map2

Parameters

  • callback function (T): U
  • iter2 Iterable<V>

Returns Laziness<U>

slice

See slice

Parameters

Returns Laziness<T>

tail

See tail

Returns Laziness<T>

tee

See tee

Parameters

  • n number (optional, default 2)

Returns Array<Laziness<T>>

toArray

Returns Array<T>

from

Same as new Laziness(iter)

Parameters

  • iter Iterable<T>

Returns Laziness<T>