linqts
Version:
LinQ + TypeScript
317 lines (316 loc) • 12.1 kB
TypeScript
type PredicateType<T> = (value: T, index?: number, list?: T[]) => boolean;
declare class List<T> {
protected _elements: T[];
/**
* Make the List iterable and Spreadable
*/
[Symbol.iterator](): Generator<T, void, unknown>;
/**
* property represents the Object name
*/
get [Symbol.toStringTag](): string;
/**
* Defaults the elements of the list
*/
constructor(elements?: T[]);
/**
* Adds an object to the end of the List<T>.
*/
Add(element: T): void;
/**
* Appends an object to the end of the List<T>.
*/
Append(element: T): void;
/**
* Add an object to the start of the List<T>.
*/
Prepend(element: T): void;
/**
* Adds the elements of the specified collection to the end of the List<T>.
*/
AddRange(elements: T[]): void;
/**
* Applies an accumulator function over a sequence.
*/
Aggregate<U>(accumulator: (accum: U, value: T, index: number, list?: T[]) => any, initialValue: U): any;
/**
* Determines whether all elements of a sequence satisfy a condition.
*/
All(predicate: PredicateType<T>): boolean;
/**
* Determines whether a sequence contains any elements.
*/
Any(predicate?: PredicateType<T>): boolean;
/**
* Computes the average of a sequence of number values that are obtained by invoking
* a transform function on each element of the input sequence.
*/
Average(transform?: (value?: T, index?: number, list?: T[]) => any): number;
/**
* Casts the elements of a sequence to the specified type.
*/
Cast<U>(): List<U>;
/**
* Removes all elements from the List<T>.
*/
Clear(): void;
/**
* Concatenates two sequences.
*/
Concat(list: List<T>): List<T>;
/**
* Determines whether an element is in the List<T>.
*/
Contains(element: T): boolean;
/**
* Returns the number of elements in a sequence.
*/
Count(predicate?: PredicateType<T>): number;
/**
* Returns the elements of the specified sequence or the type parameter's default value
* in a singleton collection if the sequence is empty.
*/
DefaultIfEmpty(defaultValue?: T): List<T>;
/**
* Returns distinct elements from a sequence by using the default equality comparer to compare values.
*/
Distinct(): List<T>;
/**
* Returns distinct elements from a sequence according to specified key selector.
*/
DistinctBy(keySelector: (key: T) => string | number): List<T>;
/**
* Returns the element at a specified index in a sequence.
*/
ElementAt(index: number): T;
/**
* Returns the element at a specified index in a sequence or a default value if the index is out of range.
*/
ElementAtOrDefault(index: number): T | null;
/**
* Produces the set difference of two sequences by using the default equality comparer to compare values.
*/
Except(source: List<T>): List<T>;
/**
* Returns the first element of a sequence.
*/
First(predicate?: PredicateType<T>): T;
/**
* Returns the first element of a sequence, or a default value if the sequence contains no elements.
*/
FirstOrDefault(defaultValue: T): T;
/**
* Performs the specified action on each element of the List<T>.
*/
ForEach(action: (value?: T, index?: number, list?: T[]) => any): void;
/**
* Groups the elements of a sequence according to a specified key selector function.
*/
GroupBy<TResult = T>(grouper: (key: T) => string | number, mapper?: (element: T) => TResult): {
[key: string]: TResult[];
};
/**
* Correlates the elements of two sequences based on equality of keys and groups the results.
* The default equality comparer is used to compare keys.
*/
GroupJoin<U, R>(list: List<U>, key1: (k: T) => any, key2: (k: U) => any, result: (first: T, second: List<U>) => R): List<R>;
/**
* Returns the index of the first occurence of an element in the List.
*/
IndexOf(element: T): number;
/**
* Inserts an element into the List<T> at the specified index.
*/
Insert(index: number, element: T): void | Error;
/**
* Produces the set intersection of two sequences by using the default equality comparer to compare values.
*/
Intersect(source: List<T>): List<T>;
/**
* Correlates the elements of two sequences based on matching keys. The default equality comparer is used to compare keys.
*/
Join<U, R>(list: List<U>, key1: (key: T) => any, key2: (key: U) => any, result: (first: T, second: U) => R): List<R>;
/**
* Returns the last element of a sequence.
*/
Last(predicate?: PredicateType<T>): T;
/**
* Returns the last element of a sequence, or a default value if the sequence contains no elements.
*/
LastOrDefault(defaultValue: T): T;
private static readonly comparers;
/**
* Retrieves a default comparer function based on the type of the provided sample value.
* Only supports primitive types: number, string, and boolean.
*
* @param sample - A sample value used to determine its type.
* @returns A comparison function suitable for the type of the sample, or undefined if unsupported.
*/
private static getComparer;
/**
* Gets the maximum value in a generic sequence.
* @returns The maximum value in the sequence, or undefined if the sequence is empty.
*/
Max(): T | undefined;
/**
* Gets the maximum value in a generic sequence.
* @returns The maximum value in the sequence, or undefined if the sequence is empty.
* @param selector - A function to select a value from each element for comparison.
*/
Max<R>(selector: (e: T) => R): R | undefined;
/**
* Gets the maximum value in a generic sequence.
* @returns The maximum value in the sequence, or undefined if the sequence is empty.
* @param comparer - A custom comparison function
*/
Max(comparer: (a: T, b: T) => number): T | undefined;
/**
* Returns the maximum value in a generic sequence.
* @param elements - The array of elements to find the maximum from.
* @param customComparer - An optional custom comparison function.
*/
private getMaxElement;
/**
* Gets the minimum value in a generic sequence.
* @returns The minimum value in the sequence, or undefined if the sequence is empty.
*/
Min(): T | undefined;
/**
* Gets the minimum value in a generic sequence.
* @returns The minimum value in the sequence, or undefined if the sequence is empty.
* @param selector - A function to select a value from each element for comparison.
*/
Min<R>(selector: (e: T) => R): R | undefined;
/**
* Gets the minimum value in a generic sequence.
* @returns The minimum value in the sequence, or undefined if the sequence is empty.
* @param comparer - A custom comparison function
*/
Min(comparer: (a: T, b: T) => number): T | undefined;
/**
* Returns the minimum value in a generic sequence.
* @param elements - The array of elements to find the minimum from.
* @param customComparer - An optional custom comparison function.
*/
private getMinElement;
/**
* Filters the elements of a sequence based on a specified type.
*/
OfType<U>(type: any): List<U>;
/**
* Sorts the elements of a sequence in ascending order according to a key.
*/
OrderBy(keySelector: (key: T) => any, comparer?: (a: T, b: T) => number): List<T>;
/**
* Sorts the elements of a sequence in descending order according to a key.
*/
OrderByDescending(keySelector: (key: T) => any, comparer?: (a: T, b: T) => number): List<T>;
/**
* Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
*/
ThenBy(keySelector: (key: T) => any): List<T>;
/**
* Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
*/
ThenByDescending(keySelector: (key: T) => any): List<T>;
/**
* Removes the first occurrence of a specific object from the List<T>.
*/
Remove(element: T): boolean;
/**
* Removes all the elements that match the conditions defined by the specified predicate.
*/
RemoveAll(predicate: PredicateType<T>): List<T>;
/**
* Removes the element at the specified index of the List<T>.
*/
RemoveAt(index: number): void;
/**
* Reverses the order of the elements in the entire List<T>.
*/
Reverse(): List<T>;
/**
* Projects each element of a sequence into a new form.
*/
Select<TOut>(selector: (element: T, index: number) => TOut): List<TOut>;
/**
* Projects each element of a sequence to a List<any> and flattens the resulting sequences into one sequence.
*/
SelectMany<TOut extends List<any>>(selector: (element: T, index: number) => TOut): TOut;
/**
* Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.
*/
SequenceEqual(list: List<T>): boolean;
/**
* Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
*/
Single(predicate?: PredicateType<T>): T;
/**
* Returns the only element of a sequence, or a default value if the sequence is empty;
* this method throws an exception if there is more than one element in the sequence.
*/
SingleOrDefault(defaultValue: T): T;
/**
* Bypasses a specified number of elements in a sequence and then returns the remaining elements.
*/
Skip(amount: number): List<T>;
/**
* Omit the last specified number of elements in a sequence and then returns the remaining elements.
*/
SkipLast(amount: number): List<T>;
/**
* Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
*/
SkipWhile(predicate: PredicateType<T>): List<T>;
/**
* Computes the sum of the sequence of number values that are obtained by invoking
* a transform function on each element of the input sequence.
*/
Sum(transform?: (value?: T, index?: number, list?: T[]) => number): number;
/**
* Returns a specified number of contiguous elements from the start of a sequence.
*/
Take(amount: number): List<T>;
/**
* Returns a specified number of contiguous elements from the end of a sequence.
*/
TakeLast(amount: number): List<T>;
/**
* Returns elements from a sequence as long as a specified condition is true.
*/
TakeWhile(predicate: PredicateType<T>): List<T>;
/**
* Copies the elements of the List<T> to a new array.
*/
ToArray(): T[];
/**
* Creates a Dictionary<TKey, TValue> from a List<T> according to a specified key selector function.
*/
ToDictionary<TKey, TValue>(key: (key: T) => TKey, value?: (value: T) => TValue): List<{
Key: TKey;
Value: T | TValue;
}>;
/**
* Creates a List<T> from an Enumerable.List<T>.
*/
ToList(): List<T>;
/**
* Creates a Lookup<TKey, TElement> from an IEnumerable<T> according to specified key selector and element selector functions.
*/
ToLookup<TResult>(keySelector: (key: T) => string | number, elementSelector: (element: T) => TResult): {
[key: string]: TResult[];
};
/**
* Produces the set union of two sequences by using the default equality comparer.
*/
Union(list: List<T>): List<T>;
/**
* Filters a sequence of values based on a predicate.
*/
Where(predicate: PredicateType<T>): List<T>;
/**
* Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
*/
Zip<U, TOut>(list: List<U>, result: (first: T, second: U) => TOut): List<TOut>;
}
export default List;