-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathduplicates.js
38 lines (31 loc) · 979 Bytes
/
duplicates.js
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
37
38
'use strict';
module.exports = function duplicates() {
const occuredValues = [];
const duplicateValues = {};
const stringifiedValue = (value) => {
if (Array.isArray(value) || typeof value === 'object') {
return JSON.stringify(value);
}
return value;
};
if (Array.isArray(this.items)) {
this.items.forEach((value, index) => {
const valueAsString = stringifiedValue(value);
if (occuredValues.indexOf(valueAsString) === -1) {
occuredValues.push(valueAsString);
} else {
duplicateValues[index] = value;
}
});
} else if (typeof this.items === 'object') {
Object.keys(this.items).forEach((key) => {
const valueAsString = stringifiedValue(this.items[key]);
if (occuredValues.indexOf(valueAsString) === -1) {
occuredValues.push(valueAsString);
} else {
duplicateValues[key] = this.items[key];
}
});
}
return new this.constructor(duplicateValues);
};