Skip to content

Commit d492e99

Browse files
author
Phillip Clark
committed
fixes #47, fixes #111, close #115
1 parent 5907aa3 commit d492e99

10 files changed

+319
-283
lines changed

examples/array-change.js

+36-108
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,45 @@
11
/*jshint indent:2, laxcomma:true, laxbreak:true*/
2-
var util = require('util')
3-
, expect = require('expect.js')
4-
, eql = require('deep-equal')
5-
, deep = require('..')
6-
;
2+
var util = require('util');
3+
var expect = require('expect.js');
4+
var deep = require('..');
75

86
var lhs = {
9-
"id": "Release",
10-
"phases": [{
11-
"id": "Phase1",
12-
"tasks": [
13-
{"id": "Task1"},
14-
{"id": "Task2"}
15-
]
16-
}, {
17-
"id": "Phase2",
18-
"tasks": [
19-
{"id": "Task3"}
20-
]
21-
}]
7+
'id': 'Release',
8+
'phases': [{
9+
'id': 'Phase1',
10+
'tasks': [
11+
{ 'id': 'Task1' },
12+
{ 'id': 'Task2' }
13+
]
14+
}, {
15+
'id': 'Phase2',
16+
'tasks': [
17+
{ 'id': 'Task3' }
18+
]
19+
}]
2220
};
2321
var rhs = {
24-
"id": "Release",
25-
"phases": [{
26-
// E: Phase1 -> Phase2
27-
"id": "Phase2",
28-
"tasks": [
29-
{"id": "Task3"}
30-
]
31-
}, {
32-
"id": "Phase1",
33-
"tasks": [
34-
{"id": "Task1"},
35-
{"id": "Task2"}
36-
]
37-
}]
38-
};
39-
40-
var diff = deep.diff(lhs, rhs);
41-
42-
// there should be differences
43-
expect(diff).to.be.ok();
44-
expect(diff.length).to.be(6);
45-
46-
47-
// It.phases[0].id changed from 'Phase1' to 'Phase2'
48-
//
49-
expect(diff[0].kind).to.be('E');
50-
expect(diff[0].path).to.be.an('array');
51-
expect(diff[0].path).to.have.length(3);
52-
expect(diff[0].path[0]).to.be('phases');
53-
expect(diff[0].path[1]).to.be(0);
54-
expect(diff[0].path[2]).to.be('id');
55-
expect(diff[0].lhs).to.be('Phase1');
56-
expect(diff[0].rhs).to.be('Phase2');
57-
58-
// It.phases[0].tasks[0].id changed from 'Task1' to 'Task3'
59-
//
60-
expect(diff[1].kind).to.be('E');
61-
expect(diff[1].path).to.be.an('array');
62-
expect(diff[1].path).to.have.length(5);
63-
expect(diff[1].path[0]).to.be('phases');
64-
expect(diff[1].path[1]).to.be(0);
65-
expect(diff[1].path[2]).to.be('tasks');
66-
expect(diff[1].path[3]).to.be(0);
67-
expect(diff[1].path[4]).to.be('id');
68-
expect(diff[1].lhs).to.be('Task1');
69-
expect(diff[1].rhs).to.be('Task3');
70-
71-
// It.phases[0].tasks[1] was deleted
72-
//
73-
expect(diff[2].kind).to.be('A');
74-
expect(diff[2].path).to.be.an('array');
75-
expect(diff[2].path).to.have.length(3);
76-
expect(diff[2].path[0]).to.be('phases');
77-
expect(diff[2].path[1]).to.be(0);
78-
expect(diff[2].path[2]).to.be('tasks');
79-
expect(diff[2].index).to.be(1);
80-
expect(diff[2].item.kind).to.be('D');
22+
'id': 'Release',
23+
'phases': [{
24+
// E: Phase1 -> Phase2
25+
'id': 'Phase2',
26+
'tasks': [
27+
{ 'id': 'Task3' }
28+
]
29+
}, {
30+
'id': 'Phase1',
31+
'tasks': [
32+
{ 'id': 'Task1' },
33+
{ 'id': 'Task2' }
34+
]
35+
}]
36+
};
8137

82-
// It.phases[1].id changed from 'Phase2' to 'Phase1'
83-
//
84-
expect(diff[3].kind).to.be('E');
85-
expect(diff[3].path).to.be.an('array');
86-
expect(diff[3].path).to.have.length(3);
87-
expect(diff[3].path[0]).to.be('phases');
88-
expect(diff[3].path[1]).to.be(1);
89-
expect(diff[3].path[2]).to.be('id');
90-
expect(diff[3].lhs).to.be('Phase2');
91-
expect(diff[3].rhs).to.be('Phase1');
38+
var diff = deep.diff(lhs, rhs);
39+
console.log(util.inspect(diff, false, 9)); // eslint-disable-line no-console
9240

93-
// It.phases[1].tasks[0].id changed from 'Task3' to 'Task1'
94-
//
95-
expect(diff[4].kind).to.be('E');
96-
expect(diff[4].path).to.be.an('array');
97-
expect(diff[4].path).to.have.length(5);
98-
expect(diff[4].path[0]).to.be('phases');
99-
expect(diff[4].path[1]).to.be(1);
100-
expect(diff[4].path[2]).to.be('tasks');
101-
expect(diff[4].path[3]).to.be(0);
102-
expect(diff[4].path[4]).to.be('id');
103-
expect(diff[4].lhs).to.be('Task3');
104-
expect(diff[4].rhs).to.be('Task1');
41+
deep.applyDiff(lhs, rhs);
42+
console.log(util.inspect(lhs, false, 9)); // eslint-disable-line no-console
10543

106-
// It.phases[1].tasks[1] is new
107-
//
108-
expect(diff[5].kind).to.be('A');
109-
expect(diff[5].path).to.be.an('array');
110-
expect(diff[5].path).to.have.length(3);
111-
expect(diff[5].path[0]).to.be('phases');
112-
expect(diff[5].path[1]).to.be(1);
113-
expect(diff[5].path[2]).to.be('tasks');
114-
expect(diff[5].index).to.be(1);
115-
expect(diff[5].item.kind).to.be('N');
44+
expect(lhs).to.be.eql(rhs);
11645

117-
var applied = deep.applyDiff(lhs, rhs);

examples/issue-35.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
var deep = require("../");
1+
var deep = require('../');
22

3-
var lhs = ["a","a"];
4-
var rhs = ["a"];
3+
var lhs = ['a', 'a'];
4+
var rhs = ['a'];
55
var differences = deep.diff(lhs, rhs);
66
differences.forEach(function (change) {
7-
deep.applyChange(lhs, true, change);
7+
deep.applyChange(lhs, true, change);
88
});
99

10-
console.log(lhs);
11-
console.log(rhs);
10+
console.log(lhs); // eslint-disable-line no-console
11+
console.log(rhs); // eslint-disable-line no-console

examples/issue-48.js

+32-26
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
1-
'use strict';
21

3-
var dd = require('../'); // deep-diff
2+
var dd = require('../'); // deep-diff
3+
var inspect = require('util').inspect;
4+
var expect = require('expect.js');
45

56
var before = {
6-
name: 'my object',
7-
description: 'it\'s an object!',
8-
details: {
9-
it: 'has',
10-
an: 'array',
11-
with: ['a', 'few', 'elements']
12-
}
7+
name: 'my object',
8+
description: 'it\'s an object!',
9+
details: {
10+
it: 'has',
11+
an: 'array',
12+
with: ['a', 'few', 'elements']
13+
}
1314
};
1415

1516
var after = {
16-
name: 'updated object',
17-
description: 'it\'s an object!',
18-
details: {
19-
it: 'has',
20-
an: 'array',
21-
with: ['a', 'few', 'more', 'elements', { than: 'before' }]
22-
}
17+
name: 'updated object',
18+
description: 'it\'s an object!',
19+
details: {
20+
it: 'has',
21+
an: 'array',
22+
with: ['a', 'few', 'more', 'elements', { than: 'before' }]
23+
}
2324
};
2425

25-
var revertDiff = function(src, d) {
26-
d.forEach(function (change) {
27-
dd.revertChange(src, true, change);
28-
});
29-
return src;
26+
var revertDiff = function (src, d) {
27+
d.forEach(function (change) {
28+
dd.revertChange(src, true, change);
29+
});
30+
return src;
3031
};
3132

32-
var clone = function(src) {
33-
return JSON.parse(JSON.stringify(src));
33+
var clone = function (src) {
34+
return JSON.parse(JSON.stringify(src));
3435
};
3536

3637
var df = dd.diff(before, after);
3738
var b1 = clone(before);
3839
var a1 = clone(after);
3940

40-
console.log(JSON.stringify(after));
41-
console.log(JSON.stringify(revertDiff(after, df)));
42-
console.log(JSON.stringify(b1 ));
41+
console.log(inspect(a1, false, 9)); // eslint-disable-line no-console
42+
43+
var reverted = revertDiff(a1, df);
44+
console.log(inspect(reverted, false, 9)); // eslint-disable-line no-console
45+
console.log(inspect(b1, false, 9)); // eslint-disable-line no-console
46+
47+
expect(reverted).to.eql(b1);
48+

examples/issue-70.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var deepDiff = require("../");
1+
var deepDiff = require('../');
22

33
var left = {foo: undefined};
44
var right = {};
55

6-
console.log(deepDiff.diff(left, right));
6+
console.log(deepDiff.diff(left, right)); // eslint-disable-line no-console

examples/issue-71.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var deepDiff = require("../");
1+
var diff = require('../');
22

33
var left = {
44
left: 'yes',
@@ -12,4 +12,4 @@ var right = {
1212
right: 'no',
1313
};
1414

15-
console.log(deepDiff(left, right));
15+
console.log(diff(left, right)); // eslint-disable-line no-console

examples/issue-72.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var diff = require("../");
1+
var diff = require('../');
22

33
var before = {
44
data: [1, 2, 3]
@@ -9,14 +9,13 @@ var after = {
99
};
1010

1111
var differences = diff(before, after);
12-
console.log(differences);
13-
14-
var reverted = differences.reduce(
12+
console.log(differences); // eslint-disable-line no-console
13+
differences.reduce(
1514
(acc, change) => {
1615
diff.revertChange(acc, true, change);
1716
return acc;
1817
},
1918
after
2019
);
2120

22-
console.log(after);
21+
console.log(after); // eslint-disable-line no-console

0 commit comments

Comments
 (0)