iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
41 lines • 1.32 kB
JavaScript
import toIterator from '../toIterator';
import isIterable from '../isIterable';
import isIterator from '../isIterator';
/** Maps and flattens an iterator by a depth of 1. String values returned from `iteratee` are not split into characters. */
export class FlatMapIterator {
constructor(iterator, iteratee) {
this.iterator = iterator;
this.iteratee = iteratee;
this.inner = null;
}
[Symbol.iterator]() {
return this;
}
next(...args) {
if (this.inner) {
const next = this.inner.next(...args);
if (next.done) {
this.inner = null;
return this.next(...args);
}
return next;
}
const next = this.iterator.next(...args);
if (next.done)
return next;
const value = this.iteratee(next.value);
if (typeof value !== 'string') {
if (isIterator(value)) {
this.inner = value;
return this.next(...args);
}
else if (isIterable(value)) {
this.inner = toIterator(value);
return this.next(...args);
}
}
return { value, done: false };
}
}
export default FlatMapIterator;
//# sourceMappingURL=FlatMapIterator.js.map