-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple-diff.min.js
More file actions
1 lines (1 loc) · 4.7 KB
/
simple-diff.min.js
File metadata and controls
1 lines (1 loc) · 4.7 KB
1
!function(root,factory){"function"==typeof define&&define.amd?define([],factory):"object"==typeof exports?module.exports=factory():root.simpleDiff=factory()}(this,function(){function diff(oldObj,newObj,ops){ops=ops||{};var i,len,prop,id,changes=[],oldPath=ops.oldPath||[],newPath=ops.newPath||[],ID_PROP=ops.idProp||"id",ADD_EVENT=ops.addEvent||"add",REMOVE_EVENT=ops.removeEvent||"remove",CHANGE_EVENT=ops.changeEvent||"change",ADD_ITEM_EVENT=ops.addItemEvent||"add-item",REMOVE_ITEM_EVENT=ops.removeItemEvent||"remove-item",MOVE_ITEM_EVENT=ops.moveItemEvent||"move-item",callback=ops.callback||function(item){changes.push(item)},comparators=ops.comparators||[],ignore=ops.ignore;if(!(isObject(oldObj)&&isObject(newObj)||oldObj===newObj))return callback({oldPath:oldPath,newPath:newPath,type:CHANGE_EVENT,oldValue:oldObj,newValue:newObj}),changes;if(ignore&&ignore(oldObj,newObj,{oldPath:oldPath,newPath:newPath}))return changes;if(isArray(oldObj)){var idProp=ops.idProps&&(ops.idProps[oldPath.map(numberToAsterisk).join(".")]||ops.idProps[oldPath.join(".")])||ID_PROP;if("*"===idProp){var oldLength=oldObj.length,newLength=newObj.length;for(i=0,len=oldLength>newLength?oldLength:newLength;i<len;i++)i<oldLength&&i<newLength?diff(oldObj[i],newObj[i],extend({},ops,{callback:callback,oldPath:oldPath.concat(i),newPath:newPath.concat(i)})):i>=oldLength?callback({oldPath:oldPath,newPath:newPath,type:ADD_ITEM_EVENT,oldIndex:-1,curIndex:-1,newIndex:i,newValue:newObj[i]}):i>=newLength&&callback({oldPath:oldPath,newPath:newPath,type:REMOVE_ITEM_EVENT,oldIndex:i,curIndex:newLength,newIndex:-1,oldValue:oldObj[i]});return changes}var sample=oldObj.length>0?oldObj[0]:newObj[0];if(sample===UNDEFINED)return changes;var curIndex,oldIndex,objective="object"==typeof sample,oldHash=objective?indexBy(oldObj,idProp):hashOf(oldObj),newHash=objective?indexBy(newObj,idProp):hashOf(newObj),curArray=[].concat(oldObj);for(i=0,len=oldObj.length;i<len;i++)id=objective?oldObj[i][idProp]:oldObj[i],newHash.hasOwnProperty(id)||(curIndex=curArray.indexOf(oldObj[i]),curArray.splice(curIndex,1),callback({oldPath:oldPath,newPath:newPath,type:REMOVE_ITEM_EVENT,oldIndex:i,curIndex:curIndex,newIndex:-1,oldValue:oldObj[i]}));for(i=0,len=newObj.length;i<len;i++)id=objective?newObj[i][idProp]:newObj[i],oldHash.hasOwnProperty(id)||(callback({oldPath:oldPath,newPath:newPath,type:ADD_ITEM_EVENT,oldIndex:-1,curIndex:-1,newIndex:i,newValue:newObj[i]}),i>=curArray.length?curArray.push(newObj[i]):curArray.splice(i,0,newObj[i]));for(i=0,len=newObj.length;i<len;i++)id=objective?newObj[i][idProp]:newObj[i],oldHash.hasOwnProperty(id)&&(oldIndex=oldObj.indexOf(oldHash[id]),curIndex=curArray.indexOf(oldHash[id]),i!==curIndex&&(callback({oldPath:oldPath,newPath:newPath,type:MOVE_ITEM_EVENT,oldIndex:oldIndex,curIndex:curIndex,newIndex:i}),curArray.splice(curIndex,1),curArray.splice(i,0,oldHash[id])),diff(oldHash[id],newObj[i],extend({},ops,{callback:callback,oldPath:oldPath.concat(oldIndex),newPath:newPath.concat(i)})))}else{if(comparators.length>0)for(i=0,len=comparators.length;i<len;i++)if(oldObj instanceof comparators[i][0]!=!1||newObj instanceof comparators[i][0]!=!1){var objEqual=comparators[i][1](oldObj,newObj,{oldPath:oldPath,newPath:newPath});return objEqual||callback({oldPath:oldPath,newPath:newPath,type:CHANGE_EVENT,oldValue:oldObj,newValue:newObj}),changes}for(prop in oldObj)oldObj.hasOwnProperty(prop)&&(newObj.hasOwnProperty(prop)?isObject(oldObj[prop])&&isObject(newObj[prop])?diff(oldObj[prop],newObj[prop],extend({},ops,{callback:callback,oldPath:oldPath.concat(prop),newPath:newPath.concat(prop)})):oldObj[prop]!==newObj[prop]&&callback({oldPath:oldPath.concat(prop),newPath:newPath.concat(prop),type:CHANGE_EVENT,oldValue:oldObj[prop],newValue:newObj[prop]}):callback({oldPath:oldPath.concat(prop),newPath:newPath.concat(prop),type:REMOVE_EVENT,oldValue:oldObj[prop],newValue:UNDEFINED}));for(prop in newObj)newObj.hasOwnProperty(prop)&&(oldObj.hasOwnProperty(prop)||callback({oldPath:oldPath.concat(prop),newPath:newPath.concat(prop),type:ADD_EVENT,oldValue:UNDEFINED,newValue:newObj[prop]}))}return changes}function isObject(object){return!!object&&"object"==typeof object}function isArray(array){return Array.isArray?Array.isArray(array):"[object Array]"===Object.prototype.toString.call(array)}function indexBy(array,id){for(var hash={},i=0,len=array.length;i<len;i++)hash[array[i][id]]=array[i];return hash}function hashOf(array){for(var hash={},i=0,len=array.length;i<len;i++)hash[array[i]]=array[i];return hash}function extend(target){for(var i=1,len=arguments.length;i<len;i++){var source=arguments[i];for(var prop in source)source.hasOwnProperty(prop)&&(target[prop]=source[prop])}return target}function numberToAsterisk(value){return"number"==typeof value?"*":value}var UNDEFINED;return diff});