@signaldb/core
Version:
SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON in
20 lines (19 loc) • 905 B
JavaScript
//#region src/utils/uniqueBy.ts
/**
* Filters an array to ensure unique values based on a specified key or transformation function.
* @template T - The type of the elements in the array.
* @param array - The array to filter for unique values.
* @param fn - A key or transformation function to determine uniqueness.
* - If a key is provided, it will use the corresponding property of each element for uniqueness.
* - If a function is provided, it will use the return value of the function applied to each element for uniqueness.
* @returns A new array containing only unique elements based on the specified key or transformation.
*/
function uniqueBy(array, fn) {
const set = /* @__PURE__ */ new Set();
return array.filter((element) => {
const value = typeof fn === "function" ? fn(element) : element[fn];
return !set.has(value) && set.add(value);
});
}
//#endregion
exports.default = uniqueBy;