Last active
March 16, 2022 13:37
-
-
Save GlauberF/ab51d13cb80abed5a78fc01bccdd88a7 to your computer and use it in GitHub Desktop.
deepDiffObj, Function to analyze two objects and return if there were changes, if there were not, if something was deleted etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deepDiffMapper = function () { | |
return { | |
VALUE_CREATED: 'created', | |
VALUE_UPDATED: 'updated', | |
VALUE_DELETED: 'deleted', | |
VALUE_UNCHANGED: 'unchanged', | |
map: function (obj1, obj2) { | |
if (this.isFunction(obj1) || this.isFunction(obj2)) { | |
throw 'Invalid argument. Function given, object expected.'; | |
} | |
if (this.isValue(obj1) || this.isValue(obj2)) { | |
let returnObj = { | |
type: this.compareValues(obj1, obj2), | |
original: obj1, | |
updated: obj2, | |
}; | |
// if (returnObj.type != this.VALUE_UNCHANGED) { | |
// return returnObj; | |
// } | |
// return undefined; | |
return returnObj; | |
} | |
var diff = {}; | |
let foundKeys = {}; | |
for (var key in obj1) { | |
if (this.isFunction(obj1[key])) { | |
continue; | |
} | |
var value2 = undefined; | |
if (obj2[key] !== undefined) { | |
value2 = obj2[key]; | |
} | |
let mapValue = this.map(obj1[key], value2); | |
foundKeys[key] = true; | |
if (mapValue) { | |
diff[key] = mapValue; | |
} | |
} | |
for (var key in obj2) { | |
if (this.isFunction(obj2[key]) || foundKeys[key] !== undefined) { | |
continue; | |
} | |
let mapValue = this.map(undefined, obj2[key]); | |
if (mapValue) { | |
diff[key] = mapValue; | |
} | |
} | |
//2020-06-13: object length code copied from https://stackoverflow.com/a/13190981/2336212 | |
if (Object.keys(diff).length > 0) { | |
return diff; | |
} | |
return undefined; | |
}, | |
compareValues: function (value1, value2) { | |
if (value1 === value2) { | |
return this.VALUE_UNCHANGED; | |
} | |
if (this.isDate(value1) && this.isDate(value2) && value1.getTime() === value2.getTime()) { | |
return this.VALUE_UNCHANGED; | |
} | |
if (value1 === undefined) { | |
return this.VALUE_CREATED; | |
} | |
if (value2 === undefined) { | |
return this.VALUE_DELETED; | |
} | |
return this.VALUE_UPDATED; | |
}, | |
isFunction: function (x) { | |
return Object.prototype.toString.call(x) === '[object Function]'; | |
}, | |
isArray: function (x) { | |
return Object.prototype.toString.call(x) === '[object Array]'; | |
}, | |
isDate: function (x) { | |
return Object.prototype.toString.call(x) === '[object Date]'; | |
}, | |
isObject: function (x) { | |
return Object.prototype.toString.call(x) === '[object Object]'; | |
}, | |
isValue: function (x) { | |
return !this.isObject(x) && !this.isArray(x); | |
} | |
} | |
}(); | |
// Calling | |
deepDiffMapper.map({_id: 1, descricao: 'teste'}, {_id: 1, descricao: 'teste 2'}) | |
// Return | |
/*{ | |
"_id":{ | |
"type":"unchanged", | |
"original":1, | |
"updated":1 | |
}, | |
"descricao":{ | |
"type":"updated", | |
"original":"teste", | |
"updated":"teste 2" | |
} | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment