@rxap/utilities
Version:
A collection of utility functions, types and interfaces.
36 lines (35 loc) • 1.94 kB
TypeScript
/**
* Represents a function that compares two values of the same type and determines their equality.
*
* @callback CompareTo
* @template T - The type of the elements to compare.
* @param {T} a - The first element to compare.
* @param {T} b - The second element to compare.
* @returns {boolean} - A boolean indicating whether the two elements are equal based on the comparison logic.
*/
export type CompareTo<T> = (a: T, b: T) => boolean;
/**
* Creates a function that can be used as a predicate to filter unique elements in an array.
* It checks if the current element's index matches its first occurrence index within the array.
*
* @return A function that determines whether a given element is unique in an array.
*/
export declare function unique<T>(): (value: T, index: number, self: T[]) => boolean;
/**
* Creates a function that can be used to filter an array of objects, ensuring
* that objects with the same combination of specified property values appear only once.
*
* @template T The type of elements in the array. It can be any valid TypeScript type.
*
* @param {Array<keyof T>} propertyKeys - An array of property keys used to determine uniqueness.
* @return {(value: T, index: number, self: T[]) => boolean} A predicate function that returns true for the first occurrence of an object
* with unique values for the specified properties and false for subsequent occurrences.
*/
export declare function unique<T>(propertyKeys: Array<keyof T>): (value: T, index: number, self: T[]) => boolean;
/**
* Generates a predicate function to filter unique elements in an array based on a comparison function.
*
* @param compareTo - A comparison function that determines if two elements are considered equal.
* @return A predicate function that can be used in array filtering to retain unique elements.
*/
export declare function unique<T>(compareTo: CompareTo<T>): (value: T, index: number, self: T[]) => boolean;