data-forge-beta
Version:
JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
40 lines • 1.38 kB
JavaScript
;
//
// An iterator that iterates the only distinct elements of another iterable.
//
Object.defineProperty(exports, "__esModule", { value: true });
var DistinctIterator = /** @class */ (function () {
function DistinctIterator(iterable, selector) {
this.valuesAlreadySeen = new Set();
this.iterator = iterable[Symbol.iterator]();
this.selector = selector;
}
DistinctIterator.prototype.next = function () {
while (true) {
var result = this.iterator.next();
if (result.done) {
return { done: true };
}
var potentialOutput = void 0;
if (this.selector) {
potentialOutput = this.selector(result.value);
}
else {
potentialOutput = result.value;
}
if (this.valuesAlreadySeen.has(potentialOutput)) {
// Already seen this value.
// Skip it and continue to next item.
continue;
}
this.valuesAlreadySeen.add(potentialOutput);
return {
done: false,
value: result.value,
};
}
};
return DistinctIterator;
}());
exports.DistinctIterator = DistinctIterator;
//# sourceMappingURL=distinct-iterator.js.map