Comparing version 0.1.6 to 0.1.7
{ | ||
"name": "deep-diff", | ||
"main": "index.js", | ||
"version": "0.1.4", | ||
"version": "0.1.6", | ||
"homepage": "https://github.com/flitbit/diff", | ||
@@ -6,0 +6,0 @@ "authors": [ |
29
index.js
@@ -77,6 +77,9 @@ ;(function(undefined) { | ||
function deepDiff(lhs, rhs, changes, path, key, stack) { | ||
function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) { | ||
path = path || []; | ||
var currentPath = path.slice(0); | ||
if (key) { currentPath.push(key); } | ||
if (key) { | ||
if (prefilter && prefilter(currentPath, key)) return; | ||
currentPath.push(key); | ||
} | ||
var ltype = typeof lhs; | ||
@@ -108,3 +111,3 @@ var rtype = typeof rhs; | ||
} else { | ||
deepDiff(lhs[i], rhs[i], ea, [], null, stack); | ||
deepDiff(lhs[i], rhs[i], ea, prefilter, [], null, stack); | ||
} | ||
@@ -121,10 +124,10 @@ } | ||
if (i >= 0) { | ||
deepDiff(lhs[k], rhs[k], changes, currentPath, k, stack); | ||
deepDiff(lhs[k], rhs[k], changes, prefilter, currentPath, k, stack); | ||
pkeys = arrayRemove(pkeys, i); | ||
} else { | ||
deepDiff(lhs[k], undefined, changes, currentPath, k, stack); | ||
deepDiff(lhs[k], undefined, changes, prefilter, currentPath, k, stack); | ||
} | ||
}); | ||
pkeys.forEach(function(k) { | ||
deepDiff(undefined, rhs[k], changes, currentPath, k, stack); | ||
deepDiff(undefined, rhs[k], changes, prefilter, currentPath, k, stack); | ||
}); | ||
@@ -141,9 +144,11 @@ } | ||
function accumulateDiff(lhs, rhs, accum) { | ||
function accumulateDiff(lhs, rhs, prefilter, accum) { | ||
accum = accum || []; | ||
deepDiff(lhs, rhs, function(diff) { | ||
if (diff) { | ||
accum.push(diff); | ||
} | ||
}); | ||
deepDiff(lhs, rhs, | ||
function(diff) { | ||
if (diff) { | ||
accum.push(diff); | ||
} | ||
}, | ||
prefilter); | ||
return (accum.length) ? accum : undefined; | ||
@@ -150,0 +155,0 @@ } |
{ | ||
"name": "deep-diff", | ||
"description": "Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "diff", |
@@ -12,2 +12,8 @@ # deep-diff [![Build Status](https://travis-ci.org/flitbit/diff.png?branch=master)](https://travis-ci.org/flitbit/diff) | ||
## ChangeLog | ||
`0.1.7` - [Enhancement #11](https://github.com/flitbit/diff/issues/11) Added the ability to filter properties that should not be analyzed while calculating differences. Makes `deep-diff` more usable when other frameworks attach housekeeping properties to existing objects. AngularJS does exactly such, and this filter ability should ease working with it. | ||
`0.1.6` - Changed objects within nested arrays can now be applied. They were previously recording the changes appropriately but `applyDiff` would error. Comparison of `NaN` works more sanely - comparison to number shows difference, comparison to another `Nan` does not. | ||
## Installation | ||
@@ -171,2 +177,16 @@ ``` | ||
### `diff` | ||
The `diff` function calculates the difference between two objects. In version `0.1.7` you can supply your own `prefilter` function as the 3rd arguement and control which properties are ignored while calculating differences throughout the object graph. | ||
**Arguments** | ||
+ `lhs` - the left-hand operand; the origin object. | ||
+ `rhs` - the right-hand operand; the object being compared structurally with the origin object. | ||
+ `prefilter` - an optional function that determines whether difference analysis should continue down the object graph. | ||
+ `acc` - an optional accumulator/array (requirement is that it have a `push` function). Each difference is pushed to the specified accumulator. | ||
#### Pre-filtering Object Properties | ||
The `prefilter`'s signature should be `function(path, key)` and it should return a truthy value for any `path`-`key` combination that should be filtered. If filtered, the difference analysis does no further analysis of on the identified object-property path. | ||
@@ -164,2 +164,52 @@ if (typeof require === 'function') { | ||
describe('When filtering keys', function() { | ||
var lhs = { enhancement: 'Filter/Ignore Keys?', numero: 11, submitted_by: 'ericclemmons', supported_by: ['ericclemmons'], status: 'open' }; | ||
var rhs = { enhancement: 'Filter/Ignore Keys?', numero: 11, submitted_by: 'ericclemmons', supported_by: [ | ||
'ericclemmons', | ||
'TylerGarlick', | ||
'flitbit', | ||
'ergdev' | ||
], status: 'closed', fixed_by: 'flitbit' }; | ||
describe('if the filtered property is an array', function() { | ||
it('changes to the array do not appear as a difference', function() { | ||
var prefilter = function(path, key) { | ||
return key === 'supported_by' | ||
}; | ||
var diff = deep(lhs, rhs, prefilter); | ||
expect(diff).to.be.ok(); | ||
expect(diff.length).to.be(2); | ||
expect(diff[0]).to.have.property('kind'); | ||
expect(diff[0].kind).to.be('E'); | ||
expect(diff[1]).to.have.property('kind'); | ||
expect(diff[1].kind).to.be('N'); | ||
}); | ||
}); | ||
describe('if the filtered property is not an array', function() { | ||
it('changes do not appear as a difference', function() { | ||
var prefilter = function(path, key) { | ||
return key === 'fixed_by' | ||
}; | ||
var diff = deep(lhs, rhs, prefilter); | ||
expect(diff).to.be.ok(); | ||
expect(diff.length).to.be(4); | ||
expect(diff[0]).to.have.property('kind'); | ||
expect(diff[0].kind).to.be('A'); | ||
expect(diff[1]).to.have.property('kind'); | ||
expect(diff[1].kind).to.be('A'); | ||
expect(diff[2]).to.have.property('kind'); | ||
expect(diff[2].kind).to.be('A'); | ||
expect(diff[3]).to.have.property('kind'); | ||
expect(diff[3].kind).to.be('E'); | ||
}); | ||
}); | ||
}); | ||
describe('A target that has nested values', function() { | ||
@@ -166,0 +216,0 @@ var nestedOne = { noChange: 'same', levelOne: { levelTwo: 'value' } }; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
504677
20
191
3238