Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-diff

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-diff - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

.travis.yml

2

bower.json
{
"name": "backbone-dom-view",
"main": "backbone-dom-view.js",
"version": "1.0.1",
"version": "1.1.0",
"homepage": "https://github.com/redexp/simple-diff",

@@ -6,0 +6,0 @@ "authors": [

{
"name": "simple-diff",
"version": "1.0.1",
"version": "1.1.0",
"description": "simple diff for object and arrays with options",

@@ -5,0 +5,0 @@ "main": "simple-diff.js",

@@ -5,2 +5,5 @@ simple-diff

Lib for simple diff with options
[![Build Status](https://travis-ci.org/redexp/simple-diff.svg?branch=master)](https://travis-ci.org/redexp/simple-diff)
```javascript

@@ -7,0 +10,0 @@ var oldObject = {

@@ -33,5 +33,13 @@ (function (root, factory) {

if (_DEV_) {
if (!isObject(oldObj) || !isObject(newObj)) {
throw new Error('All arguments should be an objects ' + oldPath.join('.'));
if (!isObject(oldObj) || !isObject(newObj)) {
if (oldObj !== newObj) {
callback({
oldPath: oldPath,
newPath: newPath,
type: CHANGE_EVENT,
oldValue: oldObj,
newValue: newObj
});
return changes;
}

@@ -38,0 +46,0 @@ }

@@ -1,1 +0,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,newIndex,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)};if(isArray(oldObj)){var sample=oldObj.length>0?oldObj[0]:newObj[0];if(sample===UNDEFINED)return changes;if("object"==typeof sample){var idProp=ops.idProps&&(ops.idProps[oldPath.map(numberToAsterisk).join(".")]||ops.idProps[oldPath.join(".")])||ID_PROP,oldHash=indexBy(oldObj,idProp),newHash=indexBy(newObj,idProp);for(i=0,len=oldObj.length;i<len;i++)id=oldObj[i][idProp],newIndex=newObj.indexOf(newHash[id]),(newIndex===-1||i>=newObj.length||id!==newObj[i][idProp])&&callback({oldPath:oldPath,newPath:newPath,type:newIndex===-1?REMOVE_ITEM_EVENT:MOVE_ITEM_EVENT,oldIndex:i,newIndex:newIndex}),newHash.hasOwnProperty(id)&&diff(oldObj[i],newHash[id],extend({},ops,{callback:callback,oldPath:oldPath.concat(i),newPath:newPath.concat(newIndex)}));for(i=0,len=newObj.length;i<len;i++)oldHash.hasOwnProperty(newObj[i][idProp])||callback({oldPath:oldPath,newPath:newPath,type:ADD_ITEM_EVENT,oldIndex:-1,newIndex:i,newValue:newObj[i]})}else{var oldObjHash={},offset=0;for(i=0,len=oldObj.length;i<len;i++)oldObjHash[oldObj[i]]=i,newIndex=newObj.indexOf(oldObj[i]),(newIndex===-1||oldObj[i]!==newObj[i]&&oldObj[i]!==newObj[i-offset])&&callback({oldPath:oldPath,newPath:newPath,type:newIndex===-1?REMOVE_ITEM_EVENT:MOVE_ITEM_EVENT,oldIndex:i,newIndex:newIndex}),newIndex===-1&&offset++;for(i=0,len=newObj.length;i<len;i++)oldObjHash.hasOwnProperty(newObj[i])||callback({oldPath:oldPath,newPath:newPath,type:ADD_ITEM_EVENT,oldIndex:-1,newIndex:i,newValue:newObj[i]})}}else{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 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});
!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,newIndex,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)};if(!(isObject(oldObj)&&isObject(newObj)||oldObj===newObj))return callback({oldPath:oldPath,newPath:newPath,type:CHANGE_EVENT,oldValue:oldObj,newValue:newObj}),changes;if(isArray(oldObj)){var sample=oldObj.length>0?oldObj[0]:newObj[0];if(sample===UNDEFINED)return changes;if("object"==typeof sample){var idProp=ops.idProps&&(ops.idProps[oldPath.map(numberToAsterisk).join(".")]||ops.idProps[oldPath.join(".")])||ID_PROP,oldHash=indexBy(oldObj,idProp),newHash=indexBy(newObj,idProp);for(i=0,len=oldObj.length;i<len;i++)id=oldObj[i][idProp],newIndex=newObj.indexOf(newHash[id]),(newIndex===-1||i>=newObj.length||id!==newObj[i][idProp])&&callback({oldPath:oldPath,newPath:newPath,type:newIndex===-1?REMOVE_ITEM_EVENT:MOVE_ITEM_EVENT,oldIndex:i,newIndex:newIndex}),newHash.hasOwnProperty(id)&&diff(oldObj[i],newHash[id],extend({},ops,{callback:callback,oldPath:oldPath.concat(i),newPath:newPath.concat(newIndex)}));for(i=0,len=newObj.length;i<len;i++)oldHash.hasOwnProperty(newObj[i][idProp])||callback({oldPath:oldPath,newPath:newPath,type:ADD_ITEM_EVENT,oldIndex:-1,newIndex:i,newValue:newObj[i]})}else{var oldObjHash={},offset=0;for(i=0,len=oldObj.length;i<len;i++)oldObjHash[oldObj[i]]=i,newIndex=newObj.indexOf(oldObj[i]),(newIndex===-1||oldObj[i]!==newObj[i]&&oldObj[i]!==newObj[i-offset])&&callback({oldPath:oldPath,newPath:newPath,type:newIndex===-1?REMOVE_ITEM_EVENT:MOVE_ITEM_EVENT,oldIndex:i,newIndex:newIndex}),newIndex===-1&&offset++;for(i=0,len=newObj.length;i<len;i++)oldObjHash.hasOwnProperty(newObj[i])||callback({oldPath:oldPath,newPath:newPath,type:ADD_ITEM_EVENT,oldIndex:-1,newIndex:i,newValue:newObj[i]})}}else{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 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});

@@ -425,2 +425,16 @@ var expect = require('chai').expect,

});
it('should compare non object', function () {
var changes = diff({test: 1}, 'test');
expect(changes).to.deep.equal([
{
type: 'change',
oldPath: [],
newPath: [],
oldValue: {test: 1},
newValue: 'test'
}
]);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc