-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff-checker-REST.js
72 lines (57 loc) · 1.8 KB
/
diff-checker-REST.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
.factory('$helper', [function() {
var $helper = {};
/**
* @param {Object} original
* @param {Object} toCompare
* @param boolean (optional)
* - default is false
- false checks all keys of object
- true checks all keys that are own properties
* @return {Object}
*/
$helper.diff = function (original, toCompare, ownProp) {
var objectDiff = {}, diffObj;
// If ownProp is not defined
// Then default to using standard object key check
if (ownProp === undefined) { ownProp = false; }
/**
* @param {Object} a
* @param {Object} b
* @return {Object}
*/
objectDiff.diff = function diff(a, b) {
if (a === b) {
return false;
}
var toSave = {};
var equal = true;
for (var key in a) {
if (a[key] !== b[key]) {
var typeA = typeof a[key];
var typeB = typeof b[key];
// Currently using _.isArray func from loDash libarary
// TODO: Swap out to make more modular / independent
if (typeA == 'object' && typeB == 'object' && !_.isArray(a[key]) && !_.isArray(b[key])) {
var valueDiff = diff(a[key], b[key]);
if (valueDiff !== false) {
equal = false;
toSave[key] = valueDiff;
}
} else if (!(typeA == 'function') && !(typeB == 'function') && !_.isArray(a[key]) && !_.isArray(b[key])) {
equal = false;
toSave[key] = b[key];
}
}
}
if (equal) {
return false;
} else {
return toSave;
}
};
// Check for changes in object
// If ownProp is true use ownProperties function
return diffObj = ( ownProp ? diffOwnProperties.diff(original, toCompare) : objectDiff.diff(original, toCompare) );
};
return $helper;
}]);