package.dist.collection.d.ts Maven / Gradle / Ivy
The newest version!
export type Matcher =
((e: T) => boolean) |
((e: T, idx: number) => boolean) |
((e: T, key: string) => boolean) |
any;
export type Extractor = ((e: T) => U) | string | number;
export type ArrayCollection = Array;
export type StringKeyValueCollection = { [key: string]: T };
export type NumberKeyValueCollection = { [key: number]: T };
export type KeyValueCollection = StringKeyValueCollection | NumberKeyValueCollection;
export type Collection = KeyValueCollection | ArrayCollection | null | undefined;
/**
* Find element in collection.
*
* @param collection
* @param matcher
*
* @return
*/
export function find(collection: Collection, matcher: Matcher): T | undefined;
/**
* Find element index in collection.
*
* @param collection
* @param matcher
*
* @return
*/
export function findIndex(collection: Collection, matcher: Matcher): number | undefined;
/**
* Find element in collection.
*
* @param collection
* @param matcher
*
* @return result
*/
export function filter(collection: Collection, matcher: Matcher): T[];
/**
* Iterate over collection; returning something
* (non-undefined) will stop iteration.
*
* @param collection
* @param iterator
*
* @return return result that stopped the iteration
*/
export function forEach(collection: Collection, iterator: (item: T, convertKey: any /* TODO */) => boolean | void): T;
/**
* Return collection without element.
*
* @param arr
* @param matcher
*
* @return
*/
export function without(arr: T[], matcher: Matcher): T[];
/**
* Reduce collection, returning a single result.
*
* @param collection
* @param iterator
* @param result
*
* @return result returned from last iterator
*/
export function reduce(collection: Collection, iterator: (result: V, entry: T, index: any) => V, result: V): V;
/**
* Return true if every element in the collection
* matches the criteria.
*
* @param collection
* @param matcher
*
* @return
*/
export function every(collection: Collection, matcher: Matcher): boolean;
/**
* Return true if some elements in the collection
* match the criteria.
*
* @param collection
* @param matcher
*
* @return
*/
export function some(collection: Collection, matcher: Matcher): boolean;
/**
* Transform a collection into another collection
* by piping each member through the given fn.
*
* @param collection
* @param fn
*
* @return transformed collection
*/
export function map(collection: Collection, fn: (value: T, key: number) => U): U[];
/**
* Get the collections keys.
*
* @param collection
*
* @return
*/
export function keys(collection: Collection): T extends Array ? number[] : (keyof T)[];
/**
* Shorthand for `keys(o).length`.
*
* @param collection
*
* @return
*/
export function size(collection: Collection): number;
/**
* Get the values in the collection.
*
* @param collection
*
* @return
*/
export function values(collection: Collection): T[];
/**
* Group collection members by attribute.
*
* @param collection
* @param extractor
*
* @return map with { attrValue => [ a, b, c ] }
*/
export function groupBy(collection: Collection, extractor: Extractor, grouped?: any): { [attrValue: string]: any[] };
export function uniqueBy(extractor: Extractor, ...collections: Collection[]): T[];
export function unionBy(extractor: Extractor, ...collections: Collection[]): T[];
/**
* Sort collection by criteria.
*
* @param collection
* @param extractor
*
* @return
*/
export function sortBy(collection: Collection, extractor: Extractor): T[];
/**
* Create an object pattern matcher.
*
* @example
*
* const matcher = matchPattern({ id: 1 });
*
* let element = find(elements, matcher);
*
* @param pattern
*
* @return matcherFn
*/
export function matchPattern(pattern: T): (e: any) => boolean;