UNPKG

@rxap/utilities

Version:

A collection of utility functions, types and interfaces.

29 lines 1.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unique = unique; /** * The `unique` function is a higher-order function that returns a callback function. This callback function is used to filter out duplicate values from an array. * The function is generic and can work with arrays of any type. * * @template T The type of elements in the array. It can be any valid TypeScript type. * * @returns {function} A callback function that takes three parameters: `value`, `index`, and `self`. * - `value` (T): The current element being processed in the array. * - `index` (number): The index of the current element being processed in the array. * - `self` (T[]): The array `filter` was called upon. * This callback function returns `true` if the current `value` is the first occurrence in the `self` array (i.e., there are no duplicates before the current index), and `false` otherwise. * * @example * const numbers = [1, 2, 2, 3, 4, 4, 5]; * const uniqueNumbers = numbers.filter(unique()); * console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5] * * @example * const strings = ['a', 'b', 'b', 'c', 'd', 'd', 'e']; * const uniqueStrings = strings.filter(unique()); * console.log(uniqueStrings); // Output: ['a', 'b', 'c', 'd', 'e'] */ function unique() { return (value, index, self) => self.indexOf(value) === index; } //# sourceMappingURL=unique.js.map