UNPKG

@adamburgess/linq

Version:
205 lines (204 loc) 4.33 kB
export function createLazyGenerator(generator) { return { [Symbol.iterator]() { return generator()[Symbol.iterator](); } }; } export function* empty() { } export function range(start, count) { function* range2() { let value = start; let i = 0; while (i < count) { yield value++; i++; } } return createLazyGenerator(range2); } export function repeat(input, count) { function* repeat2() { for (let start = 0; start < count || count === -1; start++) { yield* input; } } return createLazyGenerator(repeat2); } export function reverse(input) { function* reverse2() { const items = Array.from(input); let position = items.length; while (position !== 0) { yield items[--position]; } } return createLazyGenerator(reverse2); } export function map(input, convert) { function* map2() { let i = 0; for (const x of input) { yield convert(x, i++); } } return createLazyGenerator(map2); } export function where(input, predicate) { function* where2() { let i = 0; for (const x of input) { if (predicate(x, i++)) yield x; } } return createLazyGenerator(where2); } export function groupByMap(input, keySelector, elementSelector) { const map2 = /* @__PURE__ */ new Map(); for (const x of input) { const key = keySelector(x); const value = elementSelector ? elementSelector(x) : x; const bucket = map2.get(key); if (bucket) bucket.push(value); else map2.set(key, [value]); } return map2; } export function groupBy(input, keySelector, elementSelector) { return { [Symbol.iterator]() { return groupByMap(input, keySelector, elementSelector)[Symbol.iterator](); } }; } export function take(input, count) { function* take2() { let i = 0; for (const x of input) { if (i < count) yield x; else break; i++; } } return createLazyGenerator(take2); } export function takeWhile(input, predicate) { function* takeWhile2() { for (const x of input) { if (predicate(x)) yield x; else break; } } return createLazyGenerator(takeWhile2); } export function skip(input, count) { function* skip2() { let i = 0; for (const x of input) { i++; if (i > count) yield x; } } return createLazyGenerator(skip2); } export function skipWhile(input, predicate) { function* skipWhile2() { let skipping = true; for (const x of input) { if (skipping) { if (predicate(x)) continue; skipping = false; } yield x; } } return createLazyGenerator(skipWhile2); } export function concat(a, b) { function* concat2() { yield* a; yield* b; } return createLazyGenerator(concat2); } export function distinct(input, keySelector) { function* distinct2() { const set = /* @__PURE__ */ new Set(); for (const x of input) { const key = keySelector ? keySelector(x) : x; if (!set.has(key)) { set.add(key); yield x; } } } return createLazyGenerator(distinct2); } export function flat(input) { function* flat2() { for (const x of input) { yield* x; } } return createLazyGenerator(flat2); } export function min(input) { return byMin_byMax_min_max(input, void 0, true, false); } export function max(input) { return byMin_byMax_min_max(input, void 0, false, false); } export function minBy(input, selector) { return byMin_byMax_min_max(input, selector, true, true); } export function maxBy(input, selector) { return byMin_byMax_min_max(input, selector, false, true); } export function byMin_byMax_min_max(input, selector, isMin, isBy) { let element; let current; for (const x of input) { const val = selector ? selector(x) : x; if (current === void 0 || (isMin ? val < current : val > current)) { current = val; element = x; } } if (current === void 0) throw new Error("empty"); if (isBy) return element; return current; } const Enumerable = { empty, range, repeat, reverse, map, where, groupBy, take, takeWhile, skip, skipWhile, concat, distinct, flat, min, max, minBy, maxBy, byMin_byMax_min_max }; export default Enumerable;