@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
73 lines (72 loc) • 1.77 kB
JavaScript
/**
* Removes duplicates from an array. Optionally provide a callback for calculating a unique id for
* entries.
*
* @category Array
* @category Package : @augment-vir/common
* @example No callback
*
* ```ts
* import {removeDuplicates} from '@augment-vir/common';
*
* const result = removeDuplicates([
* 1,
* 1,
* 1,
* 1,
* 1,
* 2,
* 4,
* ]);
* // result is `[1, 2, 4]`
*
* const exampleEntry = {id: 5};
*
* const result2 = removeDuplicates([
* {id: 1},
* // this entry will not get filtered out because it's a new object reference
* {id: 1},
* exampleEntry,
* // this `exampleEntry` will get filtered out because it's the same reference as the one above
* exampleEntry,
* {id: 4},
* ]);
* // result2 is `[{id: 1}, {id: 1}, exampleEntry, {id: 4}]`
* ```
*
* @example With callback
*
* ```ts
* import {removeDuplicates} from '@augment-vir/common';
*
* const exampleEntry = {id: 5};
*
* const result2 = removeDuplicates(
* [
* {id: 1},
* {id: 1},
* exampleEntry,
* exampleEntry,
* {id: 4},
* ],
* (entry) => entry.id,
* );
* // result2 is `[{id: 1}, exampleEntry, {id: 4}]`
* ```
*
* @returns A new array (does not mutate).
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export function removeDuplicates(originalArray, calculateUniqueId = (entry) => entry) {
const grouped = new Map();
return originalArray.filter((entry) => {
const uniqueId = calculateUniqueId(entry);
if (grouped.get(uniqueId)) {
return false;
}
else {
grouped.set(uniqueId, entry);
return true;
}
});
}