Skip to content

Commit 57ac1de

Browse files
author
Phillip Clark
committed
close #63
1 parent fa9f5d5 commit 57ac1de

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

Readme.md

+49-37
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Currently `v1.0.0-pre.2`
1414
npm install deep-diff@next
1515
```
1616

17+
Possible v1.0.0 incompatabilities:
18+
19+
* elements in arrays are not processed in reverse order, which fixes a few nagging bugs but may break some users
20+
* If your code relied on the order in which the differences were reported then your code will break. If you consider an object graph to be a big tree, then `deep-diff` does a [pre-order traversal of the object graph](https://en.wikipedia.org/wiki/Tree_traversal), however, when it encounters an array, the array is processed from the end towards the front, with each element recursively processed in-order during further descent.
21+
1722
## Features
1823

1924
* Get the structural differences between two objects.
@@ -85,23 +90,22 @@ In order to describe differences, change revolves around an `origin` object. For
8590
var diff = require('deep-diff').diff;
8691

8792
var lhs = {
88-
name: 'my object',
89-
description: 'it\'s an object!',
90-
details: {
91-
it: 'has',
92-
an: 'array',
93-
with: ['a', 'few', 'elements']
94-
}
93+
name: 'my object',
94+
description: 'it\'s an object!',
95+
details: {
96+
it: 'has',
97+
an: 'array',
98+
with: ['a', 'few', 'elements']
99+
}
95100
};
96101

97102
var rhs = {
98-
name: 'updated object',
99-
description: 'it\'s an object!',
100-
details: {
101-
it: 'has',
102-
an: 'array',
103-
with: ['a', 'few', 'more', 'elements', { than: 'before' }]
104-
}
103+
name: 'updated object',
104+
description: 'it\'s an object!',
105+
details: {
106+
it: 'has',
107+
an: 'array',
108+
with: ['a', 'few', 'more', 'elements', { than: 'before' }]
105109
};
106110

107111
var differences = diff(lhs, rhs);
@@ -138,8 +142,8 @@ var differences = diff(lhs, rhs);
138142
rhs: 'updated object' },
139143
{ kind: 'E',
140144
path: [ 'details', 'with', 2 ],
141-
lhs: 'elements',
142-
rhs: 'more' },
145+
lhs: 'elements',
146+
rhs: 'more' },
143147
{ kind: 'A',
144148
path: [ 'details', 'with' ],
145149
index: 3,
@@ -175,34 +179,33 @@ differences. If the structural differences are applied from the `comparand` to t
175179
When two objects differ, you can observe the differences as they are calculated and selectively apply those changes to the origin object (left-hand-side).
176180
177181
``` javascript
178-
var observableDiff = require('deep-diff').observableDiff,
179-
applyChange = require('deep-diff').applyChange;
182+
var observableDiff = require('deep-diff').observableDiff;
183+
var applyChange = require('deep-diff').applyChange;
180184

181185
var lhs = {
182-
name: 'my object',
183-
description: 'it\'s an object!',
184-
details: {
185-
it: 'has',
186-
an: 'array',
187-
with: ['a', 'few', 'elements']
188-
}
186+
name: 'my object',
187+
description: 'it\'s an object!',
188+
details: {
189+
it: 'has',
190+
an: 'array',
191+
with: ['a', 'few', 'elements']
192+
}
189193
};
190194

191195
var rhs = {
192-
name: 'updated object',
193-
description: 'it\'s an object!',
194-
details: {
195-
it: 'has',
196-
an: 'array',
197-
with: ['a', 'few', 'more', 'elements', { than: 'before' }]
198-
}
196+
name: 'updated object',
197+
description: 'it\'s an object!',
198+
details: {
199+
it: 'has',
200+
an: 'array',
201+
with: ['a', 'few', 'more', 'elements', { than: 'before' }]
199202
};
200203

201204
observableDiff(lhs, rhs, function (d) {
202-
// Apply all changes except those to the 'name' property...
203-
if (d.path.length !== 1 || d.path.join('.') !== 'name') {
204-
applyChange(lhs, rhs, d);
205-
}
205+
// Apply all changes except to the name property...
206+
if (d.path[d.path.length - 1] !== 'name') {
207+
applyChange(lhs, rhs, d);
208+
}
206209
});
207210
```
208211
@@ -220,13 +223,22 @@ A standard import of `var diff = require('deep-diff')` is assumed in all of the
220223
221224
The `diff` function calculates the difference between two objects.
222225
223-
**Arguments**
226+
#### Arguments
224227
225228
* `lhs` - the left-hand operand; the origin object.
226229
* `rhs` - the right-hand operand; the object being compared structurally with the origin object.
227230
* `prefilter` - an optional function that determines whether difference analysis should continue down the object graph.
228231
* `acc` - an optional accumulator/array (requirement is that it have a `push` function). Each difference is pushed to the specified accumulator.
229232
233+
Returns either an array of changes or, if there are no changes, `undefined`. This was originally chosen so the result would be pass a truthy test:
234+
235+
```javascript
236+
var changes = diff(obja, objb);
237+
if (changes) {
238+
// do something with the changes.
239+
}
240+
```
241+
230242
#### Pre-filtering Object Properties
231243
232244
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.

0 commit comments

Comments
 (0)