ix
Version:
The Interactive Extensions for JavaScript
33 lines (28 loc) • 905 B
text/typescript
import { IterableX } from '../iterablex.js';
import { MonoTypeOperatorFunction } from '../../interfaces.js';
/** @ignore */
export class ReverseIterable<TSource> extends IterableX<TSource> {
private _source: Iterable<TSource>;
constructor(source: Iterable<TSource>) {
super();
this._source = source;
}
*[Symbol.iterator]() {
const results = [] as TSource[];
for (const item of this._source) {
results.unshift(item);
}
yield* results;
}
}
/**
* Reverses the iterable instance.
*
* @template TSource The type of the elements in the source sequence.
* @returns {MonoTypeOperatorAsyncFunction<TSource>} The iterable in reversed sequence.
*/
export function reverse<TSource>(): MonoTypeOperatorFunction<TSource> {
return function reverseOperatorFunction(source: Iterable<TSource>): IterableX<TSource> {
return new ReverseIterable<TSource>(source);
};
}