iter-tools-es
Version:
The iterable toolbox
57 lines (42 loc) • 1.06 kB
JavaScript
const {
ensureIterable
} = require('../../internal/iterable.js');
const {
Exchange
} = require('../../internal/fork.js');
const {
Peekerator
} = require('../../internal/peekerator.js');
const _ = Symbol.for('_');
class Forkerator extends Peekerator {
static from(source) {
const exchange = new Exchange(ensureIterable(source)[Symbol.iterator]());
return super.from(exchange.fork(), exchange);
}
constructor(iterator, first, exchange) {
super(iterator, first);
this[_].exchange = exchange;
}
advance(n = 1) {
for (let i = 0; i < n; i++) {
super.advance();
this[_].exchange.advance();
}
return this;
}
fork() {
return this[_].exchange.fork();
}
[Symbol.iterator]() {
return this[_].exchange.fork();
}
}
function __forkerate(source) {
return Forkerator.from(source);
}
exports.__forkerate = __forkerate;
function wrapWithEnsureIterable(fn) {
return source => fn(ensureIterable(source));
}
const forkerate = wrapWithEnsureIterable(__forkerate);
exports.forkerate = forkerate;