iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
47 lines • 1.72 kB
JavaScript
/** Groups values in the input iterator by some key identifier function or property. */
var GroupByIterator = /** @class */ (function () {
function GroupByIterator(iterator, key) {
this.iterator = iterator;
this.key = key;
this.currGroup = [];
this.done = false;
}
GroupByIterator.prototype[Symbol.iterator] = function () {
return this;
};
GroupByIterator.prototype.next = function () {
if (this.done)
return { done: true, value: undefined };
var next;
if (this.currKey === undefined) {
next = this.iterator.next();
if (next.done) {
this.done = true;
return { done: true, value: undefined };
}
this.currKey = this.getKey(next.value);
this.currGroup.push(next.value);
}
while (!(next = this.iterator.next()).done) {
this.currGroup.push(next.value);
var nextKey = this.getKey(next.value);
if (nextKey !== this.currKey) {
var value = [
this.currKey,
this.currGroup.splice(0, this.currGroup.length - 1),
];
this.currKey = nextKey;
return { done: false, value: value };
}
}
this.done = true;
return { done: false, value: [this.currKey, this.currGroup] };
};
GroupByIterator.prototype.getKey = function (value) {
return typeof this.key === 'function' ? this.key(value) : value[this.key];
};
return GroupByIterator;
}());
export { GroupByIterator };
export default GroupByIterator;
//# sourceMappingURL=GroupByIterator.js.map