mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-23 17:07:49 +08:00
16 lines
382 B
TypeScript
16 lines
382 B
TypeScript
|
/**
|
||
|
* Removes duplicated values within an array.
|
||
|
* Supports numbers, strings and objects.
|
||
|
*/
|
||
|
export default function removeDuplicates<T>(array: T[], by = JSON.stringify): T[] {
|
||
|
const seen: Record<any, any> = {}
|
||
|
|
||
|
return array.filter(item => {
|
||
|
const key = by(item)
|
||
|
|
||
|
return Object.prototype.hasOwnProperty.call(seen, key)
|
||
|
? false
|
||
|
: (seen[key] = true)
|
||
|
})
|
||
|
}
|