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

new-generator

v0.0.5

Published

This new Generator is general purpose iterable generator.

Downloads

6

Readme

new-generator - new Generator class for ES2015 (ES6 Harmony) generators and iteration

This new Generator is general purpose iterable generator.

An generator is an object with a next() method that conforms to the iterable generator protocol.

The iterable generator protocol is for the next method to return the object with next value in value property in an iteration each time that is called. At the end of iteration, return the object with true in done property.

This Generator has filter, map or reduce methods like Array.

new-generator does not directly use any ES2015 (ES6 Harmony) features, but it is designed to work well with ES2015 (ES6) generators and iteration, a control flow library based on ES2015 (ES6) generators.

Japanese version/■日本語版はこちら■

Installation

NPM NPM

$ npm install new-generator

npm

Usage

The following example requires node v4/v5.

example using new Generator

var Generator = require('new-generator');

// new Generator with ES2015 (ES6) iteration feature (node v0.11.x)
for (var value of new Generator([11, 22, 33])
  console.log(value);
// -> 11, 22, 33

// new Generator without ES2015 (ES6) feature (node v0.8.x)
for (var gtor = new Generator([11, 22, 33]),
         n = gtor.next(); !n.done; n = gtor.next())
  console.log(n.value);
// -> 11, 22, 33

// conbination
console.log(
  new Generator(1, 20, true)                    // -> 1, 2, 3, ... 19, 20
  .filter(function (x) { return x % 2 === 0; }) // -> 2, 4, 6, ... 18, 20
  .map(function (x) { return x / 2; })          // -> 1, 2, 3, ...  9, 10
  .reduce(function (x, y) { return x + y; }));  // => 1 + 2 + ... + 10 = 55

Generator Class

new Generator(generator)

var Generator = require('new-generator');

// Number
var g1 = new Generator(3);          // -> 0, 1, 2

// String
var g2 = new Generator('abc');      // -> 'a', 'b', 'c'

// Array
var g3 = new Generator([1, 2, 3]);  // -> 1, 2, 3

// Arguments
(function() {
  var g4 = new Generator(arguments);  // -> 10, 20, 30
})(10, 20, 30);

// other Generator
var g5 = new Generator(g1);

// ES2015 (ES6) Generator
function gtorEx() {
  for (var i = 0; i < 3; ++i)
    yield i;
}
// -> 0, 1, 2
var g6 = new Generator(gtorEx);    // called with no arguments by constructor
var g7 = new Generator(gtorEx());  // normal generator

new Generator([from,] to, [step=1,] [boundary=false])

var Generator = require('new-generator');

// without boundary value (exclude last value, start from 0)
var g1 = new Generator(3);         // -> 0, 1, 2
var g2 = new Generator(0, 3);      // -> 0, 1, 2
var g3 = new Generator(0, 5, 2);   // -> 0, 2, 4
var g4 = new Generator(3, 0);      // -> 3, 2, 1
var g5 = new Generator(5, 0, -2);  // -> 5, 3, 1

// with boundary value (include last value, start from 1)
var g6 = new Generator(3, true);          // -> 1, 2, 3
var g7 = new Generator(1, 3, true);       // -> 1, 2, 3
var g8 = new Generator(1, 5, 2, true);    // -> 1, 3, 5
var g9 = new Generator(3, 1, true);       // -> 3, 2, 1
var g10 = new Generator(5, 1, -2, true);  // -> 5, 3, 1

// you can omit `new`
var g11 = Generator(3);

filter or map type of methods

Generator#filter(fn(value), this)

var Generator = require('new-generator');

console.log(new Generator(10).filter(function (x) {
  return x % 2 === 0;
}).toArray());
// -> [0, 2, 4, 6, 8]

Generator#map(fn(value), this)

var Generator = require('new-generator');

console.log(new Generator(5).map(function (x) {
  return x * 3;
}).toArray());
// -> [0, 3, 6, 9, 12]

reduce type of methods

Generator#reduce(fn(cumulative, value), initial, this)

var Generator = require('new-generator');

console.log(new Generator(10).reduce(function (x, y) {
  return x + y;
}));
// -> 55

Generator#toArray()

var Generator = require('new-generator');

console.log(new Generator(5).toArray());
// -> [0, 1, 2, 3, 4]

Generator class methods

Generator.range([from,] to, [step=1,] [boundary=false])

new Generator

Generator.count(start, [step=1])

new Generator

Generator.chain(...generators)

new Generator

etc

License

MIT

Git Repository

LightSpeedWorks/new-generator