UNPKG

underarm

Version:

Transducers for JavaScript with Underscore API and Async extensions

31 lines (27 loc) 1.02 kB
'use strict' var symIterator = require('transduce/core').protocols.iterator module.exports = function(_r){ _r.generate = generate // Transduces the current chained object by using the chained trasnformation // and an iterator created with the callback _r.prototype.generate = function(callback, callToInit){ return this.withSource(generate(callback, callToInit)) } // Creates an (duck typed) iterator that calls the provided next callback repeatedly // and uses the return value as the next value of the iterator. // Marks iterator as done if the next callback returns undefined (returns nothing) // Can be used to as a source obj to reduce, transduce etc function generate(callback, callToInit){ var gen = {} gen[symIterator] = function(){ var next = callToInit ? callback() : callback return { next: function(){ var value = next() return (value === void 0) ? {done: true} : {done: false, value: value} } } } return gen } }