@adamburgess/linq
Version:
A linq library.
135 lines (134 loc) • 5.57 kB
TypeScript
/**
* Some handy methods to create repeatable iterables.
* ```js
* // you can import _all_ of them:
* import Enumerable from '@adamburgess/linq/enumerable'
* // or just the one you want:
* import { repeat } from '@adamburgess/linq/enumerable'
* Array.from(repeat([1, 2], 3))
* // => [1, 2, 1, 2, 1, 2]
* ```
* Best to import exactly what you need, as this can be tree-shooken.
* @module
*/
/** Creates a repeatable iterable from a generator. */
export declare function createLazyGenerator<T>(generator: () => Generator<T>): Iterable<T>;
/** An empty iterable. */
export declare function empty<T = unknown>(): Iterable<T>;
/** An iterable with _count_ numbers starting from _start_, counting up. */
export declare function range(start: number, count: number): Iterable<number>;
/** An iterable that repeats _input_ _count_ times. If _count_ is -1, repeat indefinitely. */
export declare function repeat<T>(input: Iterable<T>, count: number): Iterable<T>;
/** An iterable that reverses _input_.
*
* See {@link AnySequence.reverse} for examples.
*/
export declare function reverse<T>(input: Iterable<T>): Iterable<T>;
/** An iterable that maps each element of _input_ to another value with _convert_.
*
* See {@link AnySequence.map} for examples.
*/
export declare function map<T, TOut>(input: Iterable<T>, convert: (arg: T, index: number) => TOut): Iterable<TOut>;
/** An iterable that filters _input_ to only elements that are truthy in _predicate_.
*
* See {@link AnySequence.where} for examples.
*/
export declare function where<T>(input: Iterable<T>, predicate: (arg: T, index: number) => any): Iterable<T>;
/** Groups the input and returns a map.
*
* See {@link AnySequence.groupBy} for examples.
*/
export declare function groupByMap<T, TKey>(input: Iterable<T>, keySelector: (arg: T) => TKey): Map<TKey, T[]>;
export declare function groupByMap<T, TKey, TValue>(input: Iterable<T>, keySelector: (arg: T) => TKey, elementSelector: (arg: T) => TValue): Map<TKey, TValue[]>;
/** Groups the input into an iterable with the key and values.
*
* See {@link AnySequence.groupBy} for examples.
*/
export declare function groupBy<T, TKey>(input: Iterable<T>, keySelector: (arg: T) => TKey): Iterable<[TKey, T[]]>;
export declare function groupBy<T, TKey, TValue>(input: Iterable<T>, keySelector: (arg: T) => TKey, elementSelector: (arg: T) => TValue): Iterable<[TKey, TValue[]]>;
/** Takes X elements from input.
*
* See {@link AnySequence.take} for examples.
*/
export declare function take<T>(input: Iterable<T>, count: number): Iterable<T>;
/** Takes X elements from input while a predicate is true.
*
* See {@link AnySequence.takeWhile} for examples.
*/
export declare function takeWhile<T>(input: Iterable<T>, predicate: (arg: T) => boolean): Iterable<T>;
/** Skips X elements from input.
*
* See {@link AnySequence.skip} for examples.
*/
export declare function skip<T>(input: Iterable<T>, count: number): Iterable<T>;
/** Skips X elements from input while a predicate is true.
*
* See {@link AnySequence.skipWhile} for examples.
*/
export declare function skipWhile<T>(input: Iterable<T>, predicate: (arg: T) => boolean): Iterable<T>;
/** Concatenates two iterables together.
*
* See {@link AnySequence.append} and {@link AnySequence.prepend} for examples.
*/
export declare function concat<T>(a: Iterable<T>, b: Iterable<T>): Iterable<T>;
/** Finds the distinct values in a sequence
*
* See {@link AnySequence.distinct} for examples.
*/
export declare function distinct<T, TKey = T>(input: Iterable<T>, keySelector?: (arg: T) => TKey): Iterable<T>;
/** Flattens a sequence.
*
* See {@link ArraySequence.flat} for examples.
*/
export declare function flat<T>(input: Iterable<Iterable<T>>): Iterable<T>;
/** Finds the min of a number sequence
*
* See {@link NumberSequence.min} for examples.
*/
export declare function min(input: Iterable<number>): number;
/** Finds the max of a number sequence
*
* See {@link NumberSequence.max} for examples.
*/
export declare function max(input: Iterable<number>): number;
/** Finds the value of a sequence that is the min of a projected value.
*
* See {@link AnySequence.minBy} for more info.
*/
export declare function minBy<T>(input: Iterable<T>, selector: (arg: T) => number): T;
/** Finds the value of a sequence that is the max of a projected value.
*
* See {@link AnySequence.maxBy} for more info.
*/
export declare function maxBy<T>(input: Iterable<T>, selector: (arg: T) => number): T;
/** Finds the value of a sequence, or the value in the sequence, that is the min, or the max.
*
* Depending on the booleans at the end or if you pass a selector or not.
*/
export declare function byMin_byMax_min_max<T>(input: Iterable<T>, selector: ((arg: T) => number) | undefined, isMin: boolean, isBy: true): T;
export declare function byMin_byMax_min_max<T>(input: Iterable<T>, selector: ((arg: T) => number) | undefined, isMin: boolean, isBy: false): number;
declare const Enumerable: {
empty: typeof empty;
range: typeof range;
repeat: typeof repeat;
reverse: typeof reverse;
map: typeof map;
where: typeof where;
groupBy: typeof groupBy;
take: typeof take;
takeWhile: typeof takeWhile;
skip: typeof skip;
skipWhile: typeof skipWhile;
concat: typeof concat;
distinct: typeof distinct;
flat: typeof flat;
min: typeof min;
max: typeof max;
minBy: typeof minBy;
maxBy: typeof maxBy;
byMin_byMax_min_max: typeof byMin_byMax_min_max;
};
/**
* @ignore
*/
export default Enumerable;