Skip to content

Commit a95aad1

Browse files
author
Phillip Clark
committed
reconciled difference between index.es.js and index.js
1 parent f6f2e2a commit a95aad1

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

Readme.md

+12
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,15 @@ Currently testing on Travis CI against:
249249
+ nodejs `0.12`
250250
+ nodejs `0.11`
251251
+ nodejs `0.10`
252+
253+
# Contributing
254+
255+
When contributing, keep in mind that it is an objective of `deep-diff` to have no package dependencies. This may change in the future, but for now, no-dependencies.
256+
257+
As of release 0.3.5, all edits/changes should be made to `index.es.js`. You must run the unit tests before submitting your PR: `npm test`. Hopefully your PR includes additional unit tests to illustrate your change/modification!
258+
259+
When you run `npm test`, linting will be performed and `index.js` will be built from `index.es.js`. Any linting errors will fail the tests... this includes code formatting.
260+
261+
This module still uses `jshint` but the plan is to switch to `eslint` very soon as I have done in several of my other modules.
262+
263+
**Thanks to all those who have contributed so far!**

index.es.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ function realTypeOf(subject) {
117117

118118
function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
119119
path = path || [];
120+
stack = stack || [];
120121
var currentPath = path.slice(0);
121122
if (typeof key !== 'undefined') {
122123
if (prefilter) {
123-
if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) { return; }
124-
else if (typeof(prefilter) === 'object') {
125-
if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) { return; }
124+
if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) {
125+
return; } else if (typeof(prefilter) === 'object') {
126+
if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) {
127+
return; }
126128
if (prefilter.normalize) {
127129
var alt = prefilter.normalize(currentPath, key, lhs, rhs);
128130
if (alt) {
@@ -144,8 +146,8 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
144146
var ltype = typeof lhs;
145147
var rtype = typeof rhs;
146148

147-
var ldefined = ltype !== 'undefined' || stack && stack[stack.length - 1].lhs.hasOwnProperty(key);
148-
var rdefined = rtype !== 'undefined' || stack && stack[stack.length - 1].rhs.hasOwnProperty(key);
149+
var ldefined = ltype !== 'undefined' || (stack && stack[stack.length - 1].lhs && stack[stack.length - 1].lhs.hasOwnProperty(key));
150+
var rdefined = rtype !== 'undefined' || (stack && stack[stack.length - 1].rhs && stack[stack.length - 1].rhs.hasOwnProperty(key));
149151

150152
if (!ldefined && rdefined) {
151153
changes(new DiffNew(currentPath, rhs));
@@ -156,9 +158,9 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
156158
} else if (realTypeOf(lhs) === 'date' && (lhs - rhs) !== 0) {
157159
changes(new DiffEdit(currentPath, lhs, rhs));
158160
} else if (ltype === 'object' && lhs !== null && rhs !== null) {
159-
stack = stack || [];
160-
if (stack.indexOf(lhs) < 0) {
161-
stack.push(lhs);
161+
if (!stack.filter(function(x) {
162+
return x.lhs === lhs; }).length) {
163+
stack.push({ lhs: lhs, rhs: rhs });
162164
if (Array.isArray(lhs)) {
163165
var i, len = lhs.length;
164166
for (i = 0; i < lhs.length; i++) {
@@ -214,7 +216,7 @@ function accumulateDiff(lhs, rhs, prefilter, accum) {
214216
function applyArrayChange(arr, index, change) {
215217
if (change.path && change.path.length) {
216218
var it = arr[index],
217-
i, u = change.path.length - 1;
219+
i, u = change.path.length - 1;
218220
for (i = 0; i < u; i++) {
219221
it = it[change.path[i]];
220222
}
@@ -250,8 +252,8 @@ function applyArrayChange(arr, index, change) {
250252
function applyChange(target, source, change) {
251253
if (target && source && change && change.kind) {
252254
var it = target,
253-
i = -1,
254-
last = change.path ? change.path.length - 1 : 0;
255+
i = -1,
256+
last = change.path ? change.path.length - 1 : 0;
255257
while (++i < last) {
256258
if (typeof it[change.path[i]] === 'undefined') {
257259
it[change.path[i]] = (typeof change.path[i] === 'number') ? [] : {};
@@ -277,7 +279,7 @@ function revertArrayChange(arr, index, change) {
277279
if (change.path && change.path.length) {
278280
// the structure of the object at the index has changed...
279281
var it = arr[index],
280-
i, u = change.path.length - 1;
282+
i, u = change.path.length - 1;
281283
for (i = 0; i < u; i++) {
282284
it = it[change.path[i]];
283285
}
@@ -318,7 +320,7 @@ function revertArrayChange(arr, index, change) {
318320
function revertChange(target, source, change) {
319321
if (target && source && change && change.kind) {
320322
var it = target,
321-
i, u;
323+
i, u;
322324
u = change.path.length - 1;
323325
for (i = 0; i < u; i++) {
324326
if (typeof it[change.path[i]] === 'undefined') {

index.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,14 @@ function realTypeOf(subject) {
124124

125125
function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
126126
path = path || [];
127+
stack = stack || [];
127128
var currentPath = path.slice(0);
128129
if (typeof key !== 'undefined') {
129130
if (prefilter) {
130-
if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) { return; }
131-
else if (typeof(prefilter) === 'object') {
132-
if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) { return; }
131+
if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) {
132+
return; } else if (typeof(prefilter) === 'object') {
133+
if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) {
134+
return; }
133135
if (prefilter.normalize) {
134136
var alt = prefilter.normalize(currentPath, key, lhs, rhs);
135137
if (alt) {
@@ -151,8 +153,8 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
151153
var ltype = typeof lhs;
152154
var rtype = typeof rhs;
153155

154-
var ldefined = ltype !== 'undefined' || stack && stack[stack.length - 1].lhs.hasOwnProperty(key);
155-
var rdefined = rtype !== 'undefined' || stack && stack[stack.length - 1].rhs.hasOwnProperty(key);
156+
var ldefined = ltype !== 'undefined' || (stack && stack[stack.length - 1].lhs && stack[stack.length - 1].lhs.hasOwnProperty(key));
157+
var rdefined = rtype !== 'undefined' || (stack && stack[stack.length - 1].rhs && stack[stack.length - 1].rhs.hasOwnProperty(key));
156158

157159
if (!ldefined && rdefined) {
158160
changes(new DiffNew(currentPath, rhs));
@@ -163,8 +165,8 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
163165
} else if (realTypeOf(lhs) === 'date' && (lhs - rhs) !== 0) {
164166
changes(new DiffEdit(currentPath, lhs, rhs));
165167
} else if (ltype === 'object' && lhs !== null && rhs !== null) {
166-
stack = stack || [];
167-
if (!stack.filter(function (x) { return x.lhs === lhs }).length) {
168+
if (!stack.filter(function(x) {
169+
return x.lhs === lhs; }).length) {
168170
stack.push({ lhs: lhs, rhs: rhs });
169171
if (Array.isArray(lhs)) {
170172
var i, len = lhs.length;
@@ -221,7 +223,7 @@ function accumulateDiff(lhs, rhs, prefilter, accum) {
221223
function applyArrayChange(arr, index, change) {
222224
if (change.path && change.path.length) {
223225
var it = arr[index],
224-
i, u = change.path.length - 1;
226+
i, u = change.path.length - 1;
225227
for (i = 0; i < u; i++) {
226228
it = it[change.path[i]];
227229
}
@@ -257,8 +259,8 @@ function applyArrayChange(arr, index, change) {
257259
function applyChange(target, source, change) {
258260
if (target && source && change && change.kind) {
259261
var it = target,
260-
i = -1,
261-
last = change.path ? change.path.length - 1 : 0;
262+
i = -1,
263+
last = change.path ? change.path.length - 1 : 0;
262264
while (++i < last) {
263265
if (typeof it[change.path[i]] === 'undefined') {
264266
it[change.path[i]] = (typeof change.path[i] === 'number') ? [] : {};
@@ -284,7 +286,7 @@ function revertArrayChange(arr, index, change) {
284286
if (change.path && change.path.length) {
285287
// the structure of the object at the index has changed...
286288
var it = arr[index],
287-
i, u = change.path.length - 1;
289+
i, u = change.path.length - 1;
288290
for (i = 0; i < u; i++) {
289291
it = it[change.path[i]];
290292
}
@@ -325,7 +327,7 @@ function revertArrayChange(arr, index, change) {
325327
function revertChange(target, source, change) {
326328
if (target && source && change && change.kind) {
327329
var it = target,
328-
i, u;
330+
i, u;
329331
u = change.path.length - 1;
330332
for (i = 0; i < u; i++) {
331333
if (typeof it[change.path[i]] === 'undefined') {

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@
6565
"uglifyjs": "^2.4.10"
6666
},
6767
"scripts": {
68+
"lint": "jscs index.es.js test/ -e && jshint index.es.js test/",
69+
"build": "rollup index.es.js -f umd -o index.js -n DeepDiff",
70+
"test": "mocha test/",
6871
"release": "uglifyjs index.js -o releases/deep-diff-$npm_package_version.min.js -r '$,require,exports,module,window,global' -m --comments '/^!/'",
69-
"pretest": "jscs index.es.js test/ -e && jshint index.es.js test/",
70-
"test": "mocha",
71-
"build": "rollup index.es.js -f umd -o index.js -n DeepDiff"
72+
73+
"prepublish": "npm run build",
74+
"prerelease": "npm test",
75+
"prebuild": "npm run lint",
76+
"pretest": "npm run build"
77+
7278
}
7379
}

test/tests.js

+2
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ describe('deep-diff', function() {
569569
it('should detect a difference with undefined property on lhs', function() {
570570
var diff = deep.diff({foo: undefined }, {});
571571

572+
expect(diff).to.be.an(Array);
572573
expect(diff.length).to.be(1);
573574

574575
expect(diff[0].kind).to.be('D');
@@ -582,6 +583,7 @@ describe('deep-diff', function() {
582583
it('should detect a difference with undefined property on rhs', function() {
583584
var diff = deep.diff({}, { foo: undefined });
584585

586+
expect(diff).to.be.an(Array);
585587
expect(diff.length).to.be(1);
586588

587589
expect(diff[0].kind).to.be('N');

0 commit comments

Comments
 (0)