import { PipeBody } from "../../pipables"; import { IntermediateLinqWrapper } from "../internal"; import { asLinq, LinqWrapper } from "../linqWrapper"; import { BuiltInLinqTraits, TryGetCountDirectSymbol } from "../traits"; import { tryGetCountDirect } from "./count"; export function concat(another: Iterable): PipeBody, LinqWrapper>; export function concat(another: Iterable): PipeBody, LinqWrapper>; export function concat(another: Iterable): PipeBody, LinqWrapper> { return target => { if (target instanceof ConcatLinqWrapper) { const state = target.__state; return new ConcatLinqWrapper({ iterables: [...state.iterables, another], }); } return new ConcatLinqWrapper({ iterables: [target, another], }); }; } interface ConcatLinqWrapperState { readonly iterables: ReadonlyArray>; } class ConcatLinqWrapper extends IntermediateLinqWrapper> implements BuiltInLinqTraits { public override *[Symbol.iterator](): Iterator { const { iterables } = this.__state; for (const it of iterables) { for (const e of it) { yield e; } } } public [TryGetCountDirectSymbol](): number | undefined { const { iterables } = this.__state; let count = 0; for (const it of iterables) { const itCount = asLinq(it).$(tryGetCountDirect()); if (itCount == null) return undefined; count += itCount; } return count; } }