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.
Installation
$ npm install new-generator
Usage
The following example requires node v4/v5
.
example using new Generator
var Generator = ; // new Generator with ES2015 (ES6) iteration feature (node v0.11.x)for var value of 11 22 33 console;// -> 11, 22, 33 // new Generator without ES2015 (ES6) feature (node v0.8.x)for var gtor = 11 22 33 n = gtornext; !ndone; n = gtornext console;// -> 11, 22, 33 // conbinationconsole; // => 1 + 2 + ... + 10 = 55
Generator Class
new Generator(generator)
var Generator = ; // Numbervar g1 = 3; // -> 0, 1, 2 // Stringvar g2 = 'abc'; // -> 'a', 'b', 'c' // Arrayvar g3 = 1 2 3; // -> 1, 2, 3 // Arguments { var g4 = arguments; // -> 10, 20, 30}10 20 30; // other Generatorvar g5 = g1; // ES2015 (ES6) Generator { for var i = 0; i < 3; ++i i;}// -> 0, 1, 2var g6 = gtorEx; // called with no arguments by constructorvar g7 = ; // normal generator
new Generator([from,] to, [step=1,] [boundary=false])
var Generator = ; // without boundary value (exclude last value, start from 0)var g1 = 3; // -> 0, 1, 2var g2 = 0 3; // -> 0, 1, 2var g3 = 0 5 2; // -> 0, 2, 4var g4 = 3 0; // -> 3, 2, 1var g5 = 5 0 -2; // -> 5, 3, 1 // with boundary value (include last value, start from 1)var g6 = 3 true; // -> 1, 2, 3var g7 = 1 3 true; // -> 1, 2, 3var g8 = 1 5 2 true; // -> 1, 3, 5var g9 = 3 1 true; // -> 3, 2, 1var g10 = 5 1 -2 true; // -> 5, 3, 1 // you can omit `new`var g11 = Generator3;
filter or map type of methods
Generator#filter(fn(value), this)
var Generator = ; console;// -> [0, 2, 4, 6, 8]
Generator#map(fn(value), this)
var Generator = ; console;// -> [0, 3, 6, 9, 12]
reduce type of methods
Generator#reduce(fn(cumulative, value), initial, this)
var Generator = ; console;// -> 55
Generator#toArray()
var Generator = ; console;// -> [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