UNPKG

@quinck/collections

Version:

Allows extra operations on JavaScript collections: Array, Map and Set.

38 lines (37 loc) 2.72 kB
export type Case<T, U> = [ (value: T, index: number, array: T[]) => unknown, (value: T, index: number, array: T[]) => U ]; export type CaseS<T, U, S extends T> = [ (value: T, index: number, array: T[]) => value is S, (value: S, index: number, array: T[]) => U ]; export declare function match<T, U, S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, matchedMapper: (value: S, index: number, array: T[]) => U): Case<T, U>; declare global { interface Array<T> { /** * Perform filter and map operations in a unique one. * @param filter used to identify the items to be mapped with the matched function * @param matchedMapper used to map the items when the filter is satisfied * @param otherwiseMapper if specified mpas the items that does not satisfy the filter, if not specified these items will be discarded * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value * @returns a new Array with the resulting items */ singleCollect<U>(predicate: (value: T, index: number, array: T[]) => unknown, matchedMapper: (value: T, index: number, array: T[]) => U, otherwiseMapper?: (value: T, index: number, array: T[]) => U, thisArg?: unknown): Array<U>; /** * Perform filter and map operations in a unique one. * @param filter used to identify the items to be mapped with the matched function * @param matchedMapper used to map the items when the filter is satisfied * @param otherwiseMapper if specified mpas the items that does not satisfy the filter, if not specified these items will be discarded * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value * @returns a new Array with the resulting items */ singleCollect<U, S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, matchedMapper: (value: S, index: number, array: T[]) => U, otherwiseMapper?: (value: S, index: number, array: T[]) => U, thisArg?: unknown): Array<U>; /** * Perform filter and map operations in a unique one accepting multiple filter cases. * @param matchCases if a condition is satisfied the associeted mapping function is applied, if no condition is satisfied the element will be discarded * @returns a new RichArray with the resulting items */ collect<U>(matchCases: Case<T, U>[], otherwiseMapper?: (value: T, index: number, array: T[]) => U, thisArg?: unknown): Array<U>; } }