super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
84 lines (83 loc) • 2.19 kB
JavaScript
import { isEqual } from '../utils/is';
/**
* Creates an array of unique values, in order, from all given arrays.
*
* @param arrays - The arrays to inspect
* @returns The new array of combined unique values
*
* @example
* ```ts
* union([2], [1, 2]);
* // => [2, 1]
* ```
*/
export function union(...arrays) {
if (!arrays.length) {
return [];
}
return [...new Set(arrays.flat())];
}
/**
* Creates an array of unique values, in order, from all given arrays
* using deep equality comparison.
*
* @param arrays - The arrays to inspect
* @returns The new array of combined unique values
*
* @example
* ```ts
* unionDeep([{ 'x': 1 }], [{ 'x': 1 }, { 'x': 2 }]);
* // => [{ 'x': 1 }, { 'x': 2 }]
* ```
*/
export function unionDeep(...arrays) {
if (!arrays.length) {
return [];
}
const result = [];
const flattened = arrays.flat();
for (const item of flattened) {
// Only add if not already in result using deep equality
if (!result.some(resultItem => isEqual(resultItem, item))) {
result.push(item);
}
}
return result;
}
/**
* Creates an array of unique values, in order, from all given arrays
* using a custom iteratee function.
*
* @param arrays - The arrays to inspect with the last argument being a function or property name
* @returns The new array of combined unique values
*
* @example
* ```ts
* unionBy([2.1], [1.2, 2.3], Math.floor);
* // => [2.1, 1.2]
*
* unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
* // => [{ 'x': 1 }, { 'x': 2 }]
* ```
*/
export function unionBy(...arrays) {
if (arrays.length <= 1) {
return [];
}
const iteratee = arrays.pop();
const iterateeFn = typeof iteratee === 'function'
? iteratee
: (obj) => obj[iteratee];
const flattened = arrays.flat();
const result = [];
const seen = new Set();
for (const item of flattened) {
const transformed = iterateeFn(item);
// Check if we've seen this transformed value before
if (!seen.has(transformed)) {
seen.add(transformed);
result.push(item);
}
}
return result;
}