UNPKG

tamda

Version:

Practical functional programming library for TypeScript

33 lines (32 loc) 1.4 kB
/** * Filters duplicate items in an `array` using the keys extracted from each item by a function `keyFn`. * @note When keys are equal, the last item is kept. * @param array Array to filter. * @param keyFn Optional function that extracts a key from each item. Default: `identity()`. * @example * const array = [{ id: 4 }, { id: 1 }, { id: 4, last: true }]; * unique(array, x => x.id); * // [{ id: 1 }, { id: 4, last: true }] */ export declare function unique<T>(array: T[], keyFn?: KeyFn<T>): T[]; /** * Returns a function that * filters duplicate items in an `array` using the keys extracted from each item by a function `keyFn`. * @note When keys are equal, the last item is kept. * @param keyFn Optional function that extracts a key from each item. Default: `identity()`. * @example * const array = [{ id: 4 }, { id: 1 }, { id: 4, last: true }]; * const uniqId = unique(x => x.id); * uniqId(array); * // [{ id: 1 }, { id: 4, last: true }] */ export declare function unique<T>(keyFn?: KeyFn<T>): typeof deferred; /** * Filters duplicate items in an `array` using the keys extracted * from each item by a previously specified function `keyFn`. * @note When keys are equal, the last item is kept. * @param array Array to filter. */ declare function deferred<T>(array: T[]): T[]; declare type KeyFn<T> = (item: T, index: number) => any; export {};