import { IterableX } from '../iterablex.js'; import { MonoTypeOperatorFunction } from '../../interfaces.js'; /** @ignore */ export class TakeLastIterable extends IterableX { private _source: Iterable; private _count: number; constructor(source: Iterable, count: number) { super(); this._source = source; this._count = count; } *[Symbol.iterator]() { if (this._count > 0) { const q = [] as TSource[]; for (const item of this._source) { if (q.length >= this._count) { q.shift(); } q.push(item); } while (q.length > 0) { yield q.shift()!; } } } } /** * Returns a specified number of contiguous elements from the end of an iterable sequence. * * @template TSource The type of the elements in the source sequence. * @param {number} count Number of elements to take from the end of the source sequence. * @returns {MonoTypeOperatorFunction} An iterable sequence containing the specified * number of elements from the end of the source sequence. */ export function takeLast(count: number): MonoTypeOperatorFunction { return function takeLastOperatorFunction(source: Iterable): IterableX { return new TakeLastIterable(source, count); }; }