-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueKey.js
60 lines (56 loc) · 1.71 KB
/
UniqueKey.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
*
* @class
* @classdesc Factory class that exposes utility method for object comparison within an array of objects.
* @exports {Class} uniqueKey
*/
class UniqueKey {
/**
* Gets detailed comparison information from an array of objects.
*
* @static
* @param {Object} an array of objects to be compared.
* @returns {Object} Returns a verbose informational object with propertes :
* is_unique {Bool}, all_values {Array} and unique_values {Array}
* for any key identified in any of the objects within the argument array
*/
static getInfoAboutKeys(objArr) {
let key_info = { "unique_keys" : [] }
for (var i = objArr.length - 1; i >= 0; i--) {
const prop = objArr[i]
for (const key in prop) {
const value = prop[key]
if (!key_info[key]) {
key_info[key] = {
"is_unique" : true,
"all_values" : [],
"unique_values" : []
}
key_info[key].all_values.push(value)
key_info[key].unique_values.push(value)
key_info.unique_keys.push(key)
} else {
if (key_info[key].all_values.includes(value)){
key_info[key].is_unique = false
} else {
key_info[key].unique_values.push(value)
}
key_info[key].all_values.push(value)
}
}
}
return key_info
}
/**
* Gets an array of keys where each object has a unique valueue.
*
* @static
* @param {Object} an array of objects to be compared.
* @returns {Array} Returns an array of keys.
*/
static getKeysWithUniqueValues(objArr) {
const unique_keys = this.getInfoAboutKeys(objArr).unique_keys
return unique_keys
}
}
module.exports = UniqueKey