-
Notifications
You must be signed in to change notification settings - Fork 2
/
duplicates.ts
36 lines (32 loc) · 1.15 KB
/
duplicates.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* ### duplicates(array, identity?)
*
* Create a version of an array, in which only the duplicated elements are kept.
* The order of result values is determined by the order they occur in the array.
* Can be passed an optional `identity` function to select the identifying part of objects.
*
* ```js
* flocky.duplicates([1, 1, 2, 4, 2, 1, 6])
* // -> [1, 2, 1]
*
* flocky.duplicates(['foo', 'bar', 'foo', 'foobar'])
* // -> ['foo']
*
* const input = [{ id: 1, a: 1 }, { id: 1, a: 2 }, { id: 2, a: 3 }, { id: 1, a: 4 }]
* flocky.duplicates(input, (element) => element.id)
* // -> [{ id: 1, a: 2 }, { id: 1, a: 4 }]
* ```
*/
export function duplicates<T>(array: Array<T>, identity?: (x: T) => unknown): Array<T> {
if (!identity) {
return primitiveDuplicates(array)
}
return objectDuplicates(array, identity)
}
function primitiveDuplicates<T>(array: Array<T>): Array<T> {
return array.filter((x, i, self) => self.indexOf(x) !== i)
}
function objectDuplicates<T>(array: Array<T>, identity: (x: T) => unknown): Array<T> {
const identities = array.map((x) => identity(x))
return array.filter((_, i) => identities.indexOf(identities[i]) !== i)
}