UNPKG

@rxap/utilities

Version:

A collection of utility functions, types and interfaces.

25 lines 1.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unique = unique; /** * Creates a predicate function to determine uniqueness of elements in an array. * The uniqueness can be determined either by a comparison function or by specific keys of the objects. * * @template T The type of elements in the array. It can be any valid TypeScript type. * * @param {CompareTo<T> | Array<keyof T>} [compareToOrKeys=((a, b) => a === b)] - A comparison function that defines how two elements are compared * for equality or an array of keys to compare when working with objects. * @return {(value: T, index: number, self: T[]) => boolean} A predicate function that can be used to filter unique elements from an array. */ function unique(compareToOrKeys = ((a, b) => a === b)) { const compareTo = typeof compareToOrKeys === 'function' ? compareToOrKeys : (a, b) => { for (const key of compareToOrKeys) { if (a[key] !== b[key]) { return false; } } return true; }; return (value, index, self) => self.findIndex(item => compareTo(value, item)) === index; } //# sourceMappingURL=unique.js.map