geninq
Version:
A JavaScript version of the Linq library for Generators
42 lines (41 loc) • 2.33 kB
JavaScript
;
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 utils_1 = require("./utils");
require("./geninq");
(0, utils_1.Build)("zip", function* (that, selector = (a, b) => [a, b]) {
that = Array.isArray(that) ? that.geninq() : that;
let this_part = this.next();
let that_part = that.next();
while (!this_part.done || !that_part.done) {
if (this_part.done || that_part.done)
throw new Error("Sequences are unequal in length");
yield selector(this_part.value, that_part.value);
this_part = this.next();
that_part = that.next();
}
}, function (that, selector = (a, b) => [a, b]) {
return __asyncGenerator(this, arguments, function* () {
that = Array.isArray(that) ? that.geninq() : that;
let this_part = yield __await(this.next());
let that_part = yield __await(that.next());
while (!this_part.done || !that_part.done) {
if (this_part.done || that_part.done)
throw new Error("Sequences are unequal in length");
yield yield __await(selector(this_part.value, that_part.value));
this_part = yield __await(this.next());
that_part = yield __await(that.next());
}
});
});