Skip to content

Commit

Permalink
refactor: replace lodash - step 2: replaces remaining array methods u…
Browse files Browse the repository at this point in the history
…niqBy and uniqWith
  • Loading branch information
sverweij committed Jul 20, 2024
1 parent 969de94 commit be85d85
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/utl/array-util.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { default as uniqBy } from "lodash/uniqBy.js";
export { default as uniqWith } from "lodash/uniqWith.js";
/**
* returns true if there's at least one element in pLeftArray that's also
* in pRightArray
Expand All @@ -15,3 +13,27 @@ export function intersects(pLeftArray, pRightArray) {
export function uniq(pArray) {
return [...new Set(pArray)];
}

/**
* @param {any[]} pArray
* @param {function} pIteratee
* @returns {any[]}
*/
export function uniqBy(pArray, pIteratee) {
return pArray.filter(
(pElement, pIndex, pSelf) =>
pIndex === pSelf.findIndex((pY) => pIteratee(pElement) === pIteratee(pY)),
);
}

/**
* @param {any[]} pArray
* @param {function} pComparator
* @returns {any[]}
*/
export function uniqWith(pArray, pComparator) {
return pArray.filter(
(pElement, pIndex, pSelf) =>
pIndex === pSelf.findIndex((pY) => pComparator(pElement, pY)),
);
}

0 comments on commit be85d85

Please sign in to comment.