-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmerger.js
130 lines (113 loc) · 4.41 KB
/
merger.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const Helpers = require("./helpers");
module.exports = class Merger extends Helpers {
/**
* @param propertyToCompare - {String} Property name on which to preform existence comparison for merge determination
* @param compareProcessor - {Function} Function to pre-process value comparing if present
*/
constructor(propertyToCompare, compareProcessor) {
super();
if (!propertyToCompare) {
this.propertyToCompare = "address";
} else {
this.propertyToCompare = propertyToCompare;
}
if (!compareProcessor) {
this.compareProcessor = this.defaultProcessor;
} else {
this.compareProcessor = compareProcessor;
}
};
/**
* @param tokenListBase {Array<Object>} - base array which will be merged into
* @param tokenListMerging {Array<Object>} - merging array which will merged from
*/
doMerge(tokenListBase, tokenListMerging){
return this.process(tokenListBase, tokenListMerging, false);
}
/**
* @param tokenListBase {Array<Object>} - base array which will be merged into
* @param tokenListMerging {Array<Object>} - merging array which will merged from
*/
getMergingDiff(tokenListBase, tokenListMerging){
return this.process(tokenListBase, tokenListMerging, true);
}
/**
* @param tokenListBase {Array<Object>} - base array which will be merged into
* @param tokenListMerging {Array<Object>} - merging array which will merged from
* @param getDiff {Boolean} - boolean switch to determine whether to merge or return the missing entries
*/
process(tokenListBase, tokenListMerging, getDiff){
let all = this.collectAll(tokenListBase, tokenListMerging);
let unique = this.collectUnique(all);
console.log(all.length);
console.log(unique.size);
let mergeList = this.extractUnique(tokenListBase, unique);
let toMerge = this.extractMergeList(tokenListMerging, mergeList);
if(getDiff){
return toMerge;
}
return tokenListBase.concat(toMerge)
}
/**
* @param tokenListBase {Array<Object>} - base array which will be merged into
* @param tokenListMerging {Array<Object>} - merging array which will merged from
*/
collectAll(tokenListBase, tokenListMerging){
let masterList = [];
for(let i=0; i<tokenListBase.length; i++){
masterList.push(this.compareProcessor(tokenListBase[i][this.propertyToCompare]));
}
for(let i=0; i<tokenListMerging.length; i++){
masterList.push(this.compareProcessor(tokenListMerging[i][this.propertyToCompare]));
}
return masterList;
}
/**
* @param masterList {Array<String>} - Array of all values to compare from both input sources combined
* @returns {Set<String>} - Set of all unique values present from both input sources combined
*/
collectUnique(masterList){
let collect = [];
masterList.forEach((val) => {
collect.push(this.compareProcessor(val));
});
return new Set(collect);
}
/**
* @param tokenList {Array<Object>} - base array used to remove entries from the unique set that are not to be merged from the merging array
* @param unique {Set<String>} - Set of all unique values present from both input sources combined
* @returns {Set<String>} - Set of identified values to be merged from the merging array
*/
extractUnique(tokenList, unique){
for(let i=0; i<tokenList.length; i++){
if(unique.has(this.compareProcessor(tokenList[i][this.propertyToCompare]))){
unique.delete(this.compareProcessor(tokenList[i][this.propertyToCompare]))
}
}
return unique;
}
/**
* @param tokenListMerging {Array<Object>} - merging array used to collect entries from the unique set that are not to be merged from the merging array
* @param unique {Set<String>} - Set of identified values from the merging array to be extracted
* @returns {Array<Object>} - Array of objects to be merged into the base array
*/
extractMergeList(tokenListMerging, unique){
let collected = [];
for(let i=0; i<tokenListMerging.length; i++){
if(unique.has(this.compareProcessor(tokenListMerging[i][this.propertyToCompare]))){
unique.delete(this.compareProcessor(tokenListMerging[i][this.propertyToCompare]));
collected.push(tokenListMerging[i]);
}
}
return collected;
}
/**
* Default comparison pre-processor function
* @param entry {String} - Value of the property on which merging determination will be made
* @returns {string} - processed value which will be compared to determine inclusion in merging
*/
defaultProcessor(entry) {
return entry.toLowerCase().trim();
// return entry["address"].toLowerCase().trim();
};
};