space-lift
Version:
TypeScript Array, Object, Map, Set, Union, Enum utils
149 lines (148 loc) • 6.28 kB
TypeScript
import { Lifted, Pipe } from './lift';
import { Wrapper } from './wrapper';
import { MapWrapper } from './map';
import { SetWrapper } from './set';
/** An Array wrapper providing extra functionalities and more chaining opportunities */
export declare class ArrayWrapper<T extends ReadonlyArray<unknown>> {
private _value;
constructor(_value: T);
private _isLiftWrapper;
value(): T;
private _clone;
/**
* Appends one item at the end of the Array.
*/
append(item: T[number]): ArrayWrapper<T>;
/**
* Appends an Iterable of items at the end of the Array.
*/
appendAll(items: Iterable<T[number]>): ArrayWrapper<T>;
/**
* Filters all the falsy elements out of this Array.
* All occurences of false, null, undefined, 0, "" will be removed.
*/
compact(): ArrayWrapper<ArrayOf<T, Compacted<T[number]>>>;
/**
* Counts the items satisfying a predicate.
*/
count(predicate: (item: T[number], index: number) => boolean): number;
/**
* Maps this Array's items, unless void or undefined is returned, in which case the item is filtered.
* This is effectively a `filter` + `map` combined in one.
*/
collect<B>(iterator: (item: T[number], index: number) => B | undefined | void): ArrayWrapper<ArrayOf<T, B>>;
/**
* Creates an array without any duplicate item.
* If a key function is passed, items will be compared based on the result of that function;
* if not, they will be compared using strict equality.
*/
distinct(getKey?: (item: T[number], index: number) => string | number): ArrayWrapper<T>;
/**
* Drops the first 'count' items from this Array.
*/
drop(count: number): ArrayWrapper<T>;
/**
* Drops the last 'count' items from this Array.
*/
dropRight(count: number): ArrayWrapper<T>;
/**
* Filters this array by aplying a predicate to all items and refine its type.
*/
filter<A extends T[number]>(predicate: (item: T[number], index: number) => item is A): ArrayWrapper<ArrayOf<T, A>>;
/**
* Filters this array by aplying a predicate.
*/
filter(predicate: (item: T[number], index: number) => boolean): ArrayWrapper<T>;
/**
* Returns the first element in this Array or undefined.
*/
first(): T[number] | undefined;
/**
* Maps this Array to an Array of Array | ArrayWrapper using a mapper function then flattens it.
*/
flatMap<B extends ReadonlyArray<any>>(fun: (item: T[number], index: number) => B): ArrayWrapper<B>;
/**
* Maps this Array to an Array of Array | ArrayWrapper using a mapper function then flattens it.
*/
flatMap<B extends ReadonlyArray<any>>(fun: (item: T[number], index: number) => ArrayWrapper<B>): ArrayWrapper<B>;
/**
* Flattens this Array of Arrays.
*/
flatten<A>(this: ArrayWrapper<ReadonlyArray<ReadonlyArray<A>>>): ArrayWrapper<ArrayOf<T, A>>;
flatten<A>(this: ArrayWrapper<A[][]>): ArrayWrapper<ArrayOf<T, A>>;
/**
* Reduces this Array into a single value, using a starting value.
*/
reduce<V>(startValue: V, func: (acc: V, value: T[number], index: number) => V): Lifted<V>;
/**
* Returns the item found at the provided index or undefined.
*/
get(index: number): T[number] | undefined;
/**
* Creates a Map where keys are the results of running each element through a discriminator function.
* The corresponding value of each key is an array of the elements responsible for generating the key.
*/
groupBy<K extends string | number>(discriminator: (item: T[number], index: number) => K): MapWrapper<K, T, MapFromArray<T, K, T>>;
/**
* Creates a new Array where each sub array contains at most 'bySize' elements.
*/
grouped(bySize: number): ArrayWrapper<T[]>;
/**
* Inserts an item at a specified index.
*/
insert(index: number, item: T[number]): ArrayWrapper<T>;
/**
* Returns the item found at the last index or undefined.
*/
last(): T[number] | undefined;
/**
* Maps this Array using a mapper function.
*/
map<B>(fun: (item: T[number], index: number) => B): ArrayWrapper<ArrayOf<T, B>>;
/**
* Removes the item found at the specified index.
*/
removeAt(index: number): ArrayWrapper<T>;
/**
* Reverses the Array.
*/
reverse(): ArrayWrapper<T>;
/**
* Sorts the Array in ascending order, using one or more iterators specifying which field to compare.
* For strings, localCompare is used.
* The sort is stable if the browser uses a stable sort (all modern engines do)
*/
sort(...fields: Array<SortOnField<T[number]>>): ArrayWrapper<T>;
/**
* Takes the first 'count' items from this Array.
*/
take(count: number): ArrayWrapper<T>;
/**
* Takes the last 'count' items from this Array.
*/
takeRight(count: number): ArrayWrapper<T>;
/**
* Converts this Array to a Set.
*/
toSet(): SetWrapper<T[number], SetFromArray<T>>;
/**
* Updates an item at the specified index.
*/
updateAt(index: number, updater: (item: T[number]) => Wrapper<T[number]>): ArrayWrapper<T>;
/**
* Updates an item at the specified index.
*/
updateAt(index: number, updater: (item: T[number]) => T[number]): ArrayWrapper<T>;
/**
* Pipes this Array with an arbitrary transformation function.
*/
pipe: typeof import("./lift").pipe;
}
export declare function setArrayPipe(_pipe: Pipe): void;
export declare function range(start: number, stop?: number, step?: number): ArrayWrapper<ReadonlyArray<number>>;
declare type SortOnField<T> = ((field: T) => string | null | undefined) | ((field: T) => number | null | undefined);
declare type Compacted<T> = T extends null | undefined | 0 | false ? never : T;
declare type SetFromArray<T extends ReadonlyArray<unknown>> = T extends Array<infer E> ? Set<E> : T extends ReadonlyArray<infer E> ? ReadonlySet<E> : never;
declare type ArrayOf<T extends ReadonlyArray<unknown>, ITEM> = T extends Array<any> ? Array<ITEM> : ReadonlyArray<ITEM>;
declare type MapFromArray<T extends ReadonlyArray<unknown>, K, V> = T extends Array<any> ? Map<K, V> : ReadonlyMap<K, V>;
export {};