fast-iterable
Version:
Provides LINQ-like fluent api operations for iterables and async iterables (ES2018+).
94 lines (93 loc) • 3.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.partitionRecipe = exports.partIterateFactory = exports.othersFactory = void 0;
const get_partition_comparer_1 = require("./get-partition-comparer");
function othersFactory(resolver) {
return (wrapper) => {
const { iterator, comparer } = wrapper;
return () => {
return resolver(iterator.next(), (next) => {
const previous = wrapper.next.value;
wrapper.next = next;
return wrapper.next.done || !comparer(previous, wrapper.next.value)
? { done: true }
: wrapper.next;
});
};
};
}
exports.othersFactory = othersFactory;
function partIterateFactory(symbol, getOthers) {
return (wrapper) => {
let call;
const others = getOthers(wrapper);
function first() {
call = others;
return wrapper.next;
}
call = first;
return {
[symbol]: () => ({
next: () => call(),
}),
};
};
}
exports.partIterateFactory = partIterateFactory;
function getValueFactory(wrapper, partIterate) {
return () => {
return wrapper.next.done
? { done: true }
: {
value: partIterate(wrapper),
};
};
}
function firstValueFactory(wrapper, callback) {
return (v) => {
wrapper.next = v;
return callback();
};
}
function nextFactory(resolver, wrapper, getValue) {
let first = true;
const { iterator } = wrapper;
return () => {
if (first) {
first = false;
return resolver(iterator.next(), firstValueFactory(wrapper, getValue));
}
return getValue();
};
}
function partitionIterateRecipe(symbol, resolver) {
return (arr, criteria) => {
const iterator = arr[symbol]();
const wrapper = {
iterator,
comparer: (0, get_partition_comparer_1.getPartitionComparer)(criteria),
};
const partIterate = partIterateFactory(symbol, othersFactory(resolver));
const getValue = getValueFactory(wrapper, partIterate);
return {
[symbol]() {
var _a, _b;
return {
next: nextFactory(resolver, wrapper, getValue),
return: (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.bind(iterator),
throw: (_b = iterator.throw) === null || _b === void 0 ? void 0 : _b.bind(iterator),
};
},
};
};
}
function partitionRecipe(symbol, resolver) {
const partitioning = partitionIterateRecipe(symbol, resolver);
return function (criteria) {
if (criteria < 1) {
throw new Error(`Validation failed, size (${criteria}) expected to be bigger than 0`);
}
return partitioning(this, criteria);
};
}
exports.partitionRecipe = partitionRecipe;