mingo
Version:
MongoDB query language for in-memory objects
91 lines (90 loc) • 2.91 kB
TypeScript
import { Any, Callback } from "./types";
/**
* A value produced by a generator
*/
export interface IteratorResult {
readonly value?: Any;
readonly done: boolean;
}
/**
* Represents a stream interface that provides a method to retrieve the next item in a sequence.
*/
export interface Generator {
next: () => IteratorResult;
}
export type Source = Generator | Callback<IteratorResult> | Iterable<Any>;
/**
* Creates a lazy iterator from the given source.
*/
export declare function Lazy(source: Source): Iterator;
/**
* Concatenate multiple iterators and return a new iterator.
*
* @param iterators The iterators to concatenate
* @returns {Iterator} A new iterator
*/
export declare function concat(...iterators: Iterator[]): Iterator;
/**
* A lazy collection iterator yields a single value at a time upon request.
*/
export declare class Iterator {
#private;
constructor(source: Source);
/**
* Add an iteratee to this lazy sequence
*/
private push;
next(): IteratorResult;
/**
* Transform each item in the sequence to a new value
* @param {Function} f
*/
map<R, T = R>(f: (o: T, n: number) => R): Iterator;
/**
* Select only items matching the given predicate
* @param {Function} f
*/
filter<T>(f: (o: T, n: number) => boolean): Iterator;
/**
* Take given numbe for values from sequence
* @param {Number} n A number greater than 0
*/
take(n: number): Iterator;
/**
* Drop a number of values from the sequence
* @param {Number} n Number of items to drop greater than 0
*/
drop(n: number): Iterator;
/**
* Returns a new lazy object with results of the transformation
* The entire sequence is realized.
*
* @param {Callback<Source, Any[]>} f Tranform function of type (Array) => (Any)
*/
transform(f: Callback<Source, Any[]>): Iterator;
/**
* Retrieves all remaining values from the lazy evaluation and returns them as an array.
* This method processes the underlying iterator until it is exhausted, storing the results
* in an internal buffer to ensure subsequent calls return the same data.
*/
collect<T>(): T[];
/**
* Execute the callback for each value.
* @param f The callback function.
* @returns {Boolean} Returns false if the callback returned false to break the loop, otherwise true.
*/
each<T>(f: Callback<T>): boolean;
/**
* Returns the reduction of sequence according the reducing function
*
* @param f The reducing function
* @param initialValue The initial value
*/
reduce<T>(f: Callback<T>, initialValue?: T): T;
/**
* Returns the number of matched items in the sequence.
* The stream is realized and buffered for later retrieval with {@link collect}.
*/
size(): number;
[Symbol.iterator](): Iterator;
}