ix
Version:
The Interactive Extensions for JavaScript
64 lines (62 loc) • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.distinct = exports.DistinctAsyncIterable = void 0;
const tslib_1 = require("tslib");
const asynciterablex_js_1 = require("./../asynciterablex.js");
const identity_js_1 = require("../../util/identity.js");
const arrayindexof_js_1 = require("../../util/arrayindexof.js");
const comparer_js_1 = require("../../util/comparer.js");
const withabort_js_1 = require("./withabort.js");
const aborterror_js_1 = require("../../aborterror.js");
/** @ignore */
class DistinctAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(source, keySelector, comparer) {
super();
this._source = source;
this._keySelector = keySelector;
this._comparer = comparer;
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
(0, aborterror_js_1.throwIfAborted)(signal);
const set = [];
try {
for (var _e = true, _f = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(this._source, signal)), _g; _g = yield tslib_1.__await(_f.next()), _b = _g.done, !_b; _e = true) {
_d = _g.value;
_e = false;
const item = _d;
const key = yield tslib_1.__await(this._keySelector(item, signal));
if ((yield tslib_1.__await((0, arrayindexof_js_1.arrayIndexOfAsync)(set, key, this._comparer))) === -1) {
set.push(key);
yield yield tslib_1.__await(item);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_e && !_b && (_c = _f.return)) yield tslib_1.__await(_c.call(_f));
}
finally { if (e_1) throw e_1.error; }
}
});
}
}
exports.DistinctAsyncIterable = DistinctAsyncIterable;
/**
* Returns an async-iterable sequence that contains only distinct elements according to the keySelector and comparer.
*
* @template TSource The type of the elements in the source sequence.
* @template TKey The type of the discriminator key computed for each element in the source sequence.
* @param {DistinctOptions<TSource, TKey = TSource>} [options] The optional arguments for a key selector and comparer function.
* @returns {MonoTypeOperatorAsyncFunction<TSource>} An operator that returns distinct elements according to the keySelector and options.
*/
function distinct(options) {
return function distinctOperatorFunction(source) {
const { ['keySelector']: keySelector = identity_js_1.identityAsync, ['comparer']: comparer = comparer_js_1.comparerAsync } = options || {};
return new DistinctAsyncIterable(source, keySelector, comparer);
};
}
exports.distinct = distinct;
//# sourceMappingURL=distinct.js.map