gengenerator
gengenerator is a useless generator. Don't be serious. Here is also a repository to collect examples for ES6 generator.
Install
To install the stable version
$ npm install gengenerator
Examples
Define a global gengenerator
const gengenerator = require('gengenerator');
gengenerator(any, generator)
any
can be any primitive data types
generator
is a generator function with one parameter
const counter = gengenerator({ count: 0 }, function * f(init) {
while(1) {
yield init.count++;
}
});
console.log(counter.next().value);
console.log(counter.next().value);
console.log(counter.next().value);
const ticker = gengenerator({
index: 1,
day: new Date().toISOString().substring(0, 10)
}, function * f(init) {
while(1) {
let today = new Date().toISOString().substring(0, 10);
if(init.day !== today) {
init.day = today;
init.index = 1;
}
yield `${init.day}-${init.index++}`;
}
});
console.log(ticker.next().value);
console.log(ticker.next().value);
console.log(ticker.next().value);
console.log(ticker.next().value);
Intercept a value with .next
. Guess a number between 1 and 100.
const bingo = gengenerator({
min: 0,
max: 101,
bonus: 30,
}, function * f(init) {
const io = (min, max) => `${min} - ? - ${max}`;
while(1) {
let input = yield io(init.min, init.max);
if(input <= init.min || input >= init.max) {
console.log('Wrong input, input again');
}
if(input === init.bonus) {
return 'Bingo!';
}
if(input < init.bonus && input > init.min) {
init.min = input;
} else if(input > init.bonus && input < init.max) {
init.max = input;
}
}
});
bingo.next();
console.log(bingo.next(31).value);
console.log(bingo.next(29).value);
console.log(bingo.next(30).value);
Have fun !
Contribute
We're happy to discuss and see suggestions. Feel free to contribute in order to see the power of generator.
License
MIT
Copyright
Copyright (C) 2016 Tony Ngan, released under the MIT License.