UNPKG

geninq

Version:

A JavaScript version of the Linq library for Generators

81 lines (80 loc) 3.47 kB
"use strict"; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; Object.defineProperty(exports, "__esModule", { value: true }); const safe_type_1 = require("@paulpopat/safe-type"); const fake_hash_1 = require("../fake-hash"); const utils_1 = require("./utils"); function CreateBuilder(builder) { var _a; return (0, safe_type_1.IsFunction)(builder) ? { key: builder, element: (i) => i, } : { key: builder.key, element: (_a = builder.element) !== null && _a !== void 0 ? _a : ((i) => i), }; } (0, utils_1.Build)("group_by", function* (builder, comparitor = (a, b) => a === b) { const internal = this.array(); const b = CreateBuilder(builder); const hash = (0, fake_hash_1.FakeHash)(comparitor); for (const item of internal) { const key = b.key(item); if (hash.contains(key)) { continue; } hash.add(key); yield [ key, (function* () { for (let i = 0; i < internal.length; i++) { const item = internal[i]; const bkey = b.key(item); if (comparitor(key, bkey)) { yield b.element(item); } } })(), ]; } }, function (builder, comparitor = (a, b) => a === b) { return __asyncGenerator(this, arguments, function* () { const internal = yield __await(this.array()); const b = CreateBuilder(builder); const hash = (0, fake_hash_1.FakeHash)(comparitor); for (const item of internal) { const key = yield __await(Promise.resolve(b.key(item))); if (hash.contains(key)) { continue; } hash.add(key); yield yield __await([ key, (function () { return __asyncGenerator(this, arguments, function* () { for (let i = 0; i < internal.length; i++) { const item = internal[i]; const bkey = b.key(item); if (comparitor(key, bkey)) { yield yield __await(yield __await(Promise.resolve(b.element(item)))); } } }); })(), ]); } }); });