-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject-tools.js
202 lines (185 loc) · 4.6 KB
/
object-tools.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
export function isPlainObject(value){
if(typeof(value) !== "object" || value === null){
return false;
}
if(value.nodeType){
return false;
}
if(value.constructor && !Object.prototype.hasOwnProperty.call(value.constructor.prototype, "isPrototypeOf" )){
return false;
}
return true;
}
//Takes object of form { a : [], b : [], c : [] } and converts to [{a, b, c},...]
export function unpivot(pivotObject){
var maxLength = 0;
for(let key in pivotObject){
maxLength = Math.max(pivotObject[key].length, maxLength);
}
var unpivotObjs = [];
for(let i = 0; i < maxLength; i++){
var unpivotObj = {};
for(let key in pivotObject){
if(pivotObject[key][i]){
unpivotObj[key] = pivotObject[key][i];
}
}
unpivotObjs.push(unpivotObj);
}
return unpivotObjs;
}
export function searchMap(map, key){
for(var currentKey in map){
if(currentKey == key){
return map[key];
}
}
return null;
}
export function isEmpty(obj){
if(Array.isArray(obj)) {
return obj.length === 0;
}else if(typeof(obj) === "object"){
return Object.keys(obj).length === 0;
}
return false;
}
export function deepClone(obj) {
if(Array.isArray(obj)){
const temp = [];
for (let i = 0; i < obj.length; i++) {
temp.push(deepClone(obj[i]));
}
return temp;
}
if(typeof(obj) === "object" && obj !== null){
const temp = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
temp[key] = deepClone(obj[key]);
}
}
return temp;
}
return obj;
}
export function objectToArray(object, keyProp){
var array = [];
for(var key in object){
var newObject = shallowClone(object[key]);
if(keyProp){
newObject[keyProp] = key;
}
array.push(newObject);
}
return array;
}
export function accessProperty(obj, accessor, defaultValue = null){
if(!obj || !accessor){
return defaultValue;
}
const accessorParts = typeof(accessor) === "string" ? accessor.split(".") : accessor;
if(accessorParts.length === 1){
return obj[accessorParts[0]] || defaultValue;
}
return accessProperty(obj[accessorParts[0]], accessorParts.slice(1), defaultValue);
}
//old
export function diff(oldObject, newObject){
var diffObject;
if(typeof(oldObject) === "object"){
diffObject = {};
for(var key in oldObject){
diffObject[key] = diff(oldObject[key], newObject[key]);
if(!diffObject[key]){
delete diffObject[key];
}
}
if(isEmpty(diffObject)){
return null;
}
return diffObject;
}else if(Array.isArray(oldObject)){
diffObject = [];
var largest = oldObject.length > newObject.length ? oldObject.length : newObject.length;
for(var i = 0; i < oldObject.length; i++){
diffObject[i] = diff(oldObject[i], newObject[i]);
}
if(isEmpty(diffObject)) return null;
return diffObject;
}else{
if(oldObject == newObject){
return null;
}else{
if(!oldObject && newObject){
return newObject;
}else if(!newObject && oldObject){
return oldObject;
}else{
return [oldObject, newObject];
}
}
}
}
export function diffObject(baseObject, testObject, path = ""){
const added = [];
const missing = [];
const different = [];
if(typeof(baseObject) !== typeof(testObject)){
different.push({ path, baseValue: baseObject, testValue: testObject });
} else if(typeof(baseObject) === "object" && typeof(testObject) === "object"){
for(const key in baseObject){
const newPath = path + "." + key;
if(!testObject.hasOwnProperty(key)){
missing.push(newPath);
} else {
const result = diffObject(baseObject[key], testObject[key], newPath);
added.push(...result.added);
missing.push(...result.missing);
different.push(...result.different);
}
}
for(const key in testObject){
const newPath = path + "." + key;
if(!baseObject.hasOwnProperty(key)){
added.push(newPath);
}
}
} else if(baseObject !== testObject){
different.push({ path, baseValue: baseObject, testValue: testObject });
}
return {
added,
missing,
different
};
}
export function objectIsSuperset(objectTest, objectControl){
if(objectTest instanceof Array && objectControl instanceof Array){
for(var i = 0; i < objectControl.length; i++){
if(!objectIsSuperset(objectTest[key], objectControl[key])){
return false;
}
}
}else if(objectTest instanceof Object && objectControl instanceof Object){
for(var key in objectControl){
if(!objectIsSuperset(objectTest[key], objectControl[key])){
return false;
}
}
}else{
if(objectTest != objectControl){
return false;
}
}
return true;
}
export function pruneObject(obj){
const newObj = {};
for(let key in obj){
if(obj[key] !== undefined){
newObj[key] = obj[key];
}
}
return newObj;
}