From be85d85c8212712a83df4518969f80d4f504ce79 Mon Sep 17 00:00:00 2001 From: sverweij Date: Sat, 20 Jul 2024 10:51:27 +0200 Subject: [PATCH] refactor: replace lodash - step 2: replaces remaining array methods uniqBy and uniqWith --- src/utl/array-util.mjs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/utl/array-util.mjs b/src/utl/array-util.mjs index ffa7bf634..e040e3ba3 100644 --- a/src/utl/array-util.mjs +++ b/src/utl/array-util.mjs @@ -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 @@ -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)), + ); +}