Last active
June 15, 2018 20:52
-
-
Save hicaro/ac9ef9ce97a5ef17aca04238c6526d5f to your computer and use it in GitHub Desktop.
Generic deep diff between two objects
This file contains hidden or 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
| /* | |
| Credits to sbgoran (https://jsfiddle.net/user/sbgoran/fiddles/) | |
| https://jsfiddle.net/sbgoran/kySNu/ | |
| */ | |
| var 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)) { | |
| return { | |
| type: this.compareValues(obj1, obj2), | |
| data: (obj1 === undefined) ? obj2 : obj1 | |
| }; | |
| } | |
| var diff = {}; | |
| for (var key1 in obj1) { | |
| if (this.isFunction(obj1[key1])) { | |
| continue; | |
| } | |
| var value2 = undefined; | |
| if ('undefined' != typeof(obj2[key1])) { | |
| value2 = obj2[key1]; | |
| } | |
| diff[key1] = this.map(obj1[key1], value2); | |
| } | |
| for (var key2 in obj2) { | |
| if (this.isFunction(obj2[key2]) || ('undefined' != typeof(diff[key2]))) { | |
| continue; | |
| } | |
| diff[key2] = this.map(undefined, obj2[key2]); | |
| } | |
| return diff; | |
| }, | |
| 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 ('undefined' == typeof(value1)) { | |
| return this.VALUE_CREATED; | |
| } | |
| if ('undefined' == typeof(value2)) { | |
| return this.VALUE_DELETED; | |
| } | |
| return this.VALUE_UPDATED; | |
| }, | |
| isFunction: function(obj) { | |
| return {}.toString.apply(obj) === '[object Function]'; | |
| }, | |
| isArray: function(obj) { | |
| return {}.toString.apply(obj) === '[object Array]'; | |
| }, | |
| isObject: function(obj) { | |
| return {}.toString.apply(obj) === '[object Object]'; | |
| }, | |
| isDate: function(obj) { | |
| return {}.toString.apply(obj) === '[object Date]'; | |
| }, | |
| isValue: function(obj) { | |
| return !this.isObject(obj) && !this.isArray(obj); | |
| } | |
| }; | |
| }(); | |
| var result = deepDiffMapper.map( | |
| { | |
| a:'i am unchanged', | |
| b:'i am deleted', | |
| e:{ a: 1,b:false, c: null}, | |
| f: [1,{a: 'same',b:[{a:'same'},{d: 'delete'}]}], | |
| g: new Date('2017.11.25') | |
| }, | |
| { | |
| a:'i am unchanged', | |
| c:'i am created', | |
| e:{ a: '1', b: '', d:'created'}, | |
| f: [{a: 'same',b:[{a:'same'},{c: 'create'}]},1], | |
| g: new Date('2017.11.25') | |
| } | |
| ); | |
| console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment