Skip to content

Commit

Permalink
reconciled difference between index.es.js and index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip Clark committed May 3, 2017
1 parent f6f2e2a commit a95aad1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
12 changes: 12 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,15 @@ Currently testing on Travis CI against:
+ nodejs `0.12`
+ nodejs `0.11`
+ nodejs `0.10`

# Contributing

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.

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!

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.

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.

**Thanks to all those who have contributed so far!**
28 changes: 15 additions & 13 deletions index.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ function realTypeOf(subject) {

function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
path = path || [];
stack = stack || [];
var currentPath = path.slice(0);
if (typeof key !== 'undefined') {
if (prefilter) {
if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) { return; }
else if (typeof(prefilter) === 'object') {
if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) { return; }
if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) {
return; } else if (typeof(prefilter) === 'object') {
if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) {
return; }
if (prefilter.normalize) {
var alt = prefilter.normalize(currentPath, key, lhs, rhs);
if (alt) {
Expand All @@ -144,8 +146,8 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
var ltype = typeof lhs;
var rtype = typeof rhs;

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

This comment has been minimized.

Copy link
@jongwookyi

jongwookyi Jun 16, 2017

This line can cause index out of range exception.
Need to be changed as follows:
stack && stack[stack.length - 1].lhs => stack && (0 < stack.length) && stack[stack.length - 1].lhs

var rdefined = rtype !== 'undefined' || (stack && stack[stack.length - 1].rhs && stack[stack.length - 1].rhs.hasOwnProperty(key));

if (!ldefined && rdefined) {
changes(new DiffNew(currentPath, rhs));
Expand All @@ -156,9 +158,9 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
} else if (realTypeOf(lhs) === 'date' && (lhs - rhs) !== 0) {
changes(new DiffEdit(currentPath, lhs, rhs));
} else if (ltype === 'object' && lhs !== null && rhs !== null) {
stack = stack || [];
if (stack.indexOf(lhs) < 0) {
stack.push(lhs);
if (!stack.filter(function(x) {
return x.lhs === lhs; }).length) {
stack.push({ lhs: lhs, rhs: rhs });
if (Array.isArray(lhs)) {
var i, len = lhs.length;
for (i = 0; i < lhs.length; i++) {
Expand Down Expand Up @@ -214,7 +216,7 @@ function accumulateDiff(lhs, rhs, prefilter, accum) {
function applyArrayChange(arr, index, change) {
if (change.path && change.path.length) {
var it = arr[index],
i, u = change.path.length - 1;
i, u = change.path.length - 1;
for (i = 0; i < u; i++) {
it = it[change.path[i]];
}
Expand Down Expand Up @@ -250,8 +252,8 @@ function applyArrayChange(arr, index, change) {
function applyChange(target, source, change) {
if (target && source && change && change.kind) {
var it = target,
i = -1,
last = change.path ? change.path.length - 1 : 0;
i = -1,
last = change.path ? change.path.length - 1 : 0;
while (++i < last) {
if (typeof it[change.path[i]] === 'undefined') {
it[change.path[i]] = (typeof change.path[i] === 'number') ? [] : {};
Expand All @@ -277,7 +279,7 @@ function revertArrayChange(arr, index, change) {
if (change.path && change.path.length) {
// the structure of the object at the index has changed...
var it = arr[index],
i, u = change.path.length - 1;
i, u = change.path.length - 1;
for (i = 0; i < u; i++) {
it = it[change.path[i]];
}
Expand Down Expand Up @@ -318,7 +320,7 @@ function revertArrayChange(arr, index, change) {
function revertChange(target, source, change) {
if (target && source && change && change.kind) {
var it = target,
i, u;
i, u;
u = change.path.length - 1;
for (i = 0; i < u; i++) {
if (typeof it[change.path[i]] === 'undefined') {
Expand Down
26 changes: 14 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ function realTypeOf(subject) {

function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
path = path || [];
stack = stack || [];
var currentPath = path.slice(0);
if (typeof key !== 'undefined') {
if (prefilter) {
if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) { return; }
else if (typeof(prefilter) === 'object') {
if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) { return; }
if (typeof(prefilter) === 'function' && prefilter(currentPath, key)) {
return; } else if (typeof(prefilter) === 'object') {
if (prefilter.prefilter && prefilter.prefilter(currentPath, key)) {
return; }
if (prefilter.normalize) {
var alt = prefilter.normalize(currentPath, key, lhs, rhs);
if (alt) {
Expand All @@ -151,8 +153,8 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
var ltype = typeof lhs;
var rtype = typeof rhs;

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

if (!ldefined && rdefined) {
changes(new DiffNew(currentPath, rhs));
Expand All @@ -163,8 +165,8 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
} else if (realTypeOf(lhs) === 'date' && (lhs - rhs) !== 0) {
changes(new DiffEdit(currentPath, lhs, rhs));
} else if (ltype === 'object' && lhs !== null && rhs !== null) {
stack = stack || [];
if (!stack.filter(function (x) { return x.lhs === lhs }).length) {
if (!stack.filter(function(x) {
return x.lhs === lhs; }).length) {
stack.push({ lhs: lhs, rhs: rhs });
if (Array.isArray(lhs)) {
var i, len = lhs.length;
Expand Down Expand Up @@ -221,7 +223,7 @@ function accumulateDiff(lhs, rhs, prefilter, accum) {
function applyArrayChange(arr, index, change) {
if (change.path && change.path.length) {
var it = arr[index],
i, u = change.path.length - 1;
i, u = change.path.length - 1;
for (i = 0; i < u; i++) {
it = it[change.path[i]];
}
Expand Down Expand Up @@ -257,8 +259,8 @@ function applyArrayChange(arr, index, change) {
function applyChange(target, source, change) {
if (target && source && change && change.kind) {
var it = target,
i = -1,
last = change.path ? change.path.length - 1 : 0;
i = -1,
last = change.path ? change.path.length - 1 : 0;
while (++i < last) {
if (typeof it[change.path[i]] === 'undefined') {
it[change.path[i]] = (typeof change.path[i] === 'number') ? [] : {};
Expand All @@ -284,7 +286,7 @@ function revertArrayChange(arr, index, change) {
if (change.path && change.path.length) {
// the structure of the object at the index has changed...
var it = arr[index],
i, u = change.path.length - 1;
i, u = change.path.length - 1;
for (i = 0; i < u; i++) {
it = it[change.path[i]];
}
Expand Down Expand Up @@ -325,7 +327,7 @@ function revertArrayChange(arr, index, change) {
function revertChange(target, source, change) {
if (target && source && change && change.kind) {
var it = target,
i, u;
i, u;
u = change.path.length - 1;
for (i = 0; i < u; i++) {
if (typeof it[change.path[i]] === 'undefined') {
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@
"uglifyjs": "^2.4.10"
},
"scripts": {
"lint": "jscs index.es.js test/ -e && jshint index.es.js test/",
"build": "rollup index.es.js -f umd -o index.js -n DeepDiff",
"test": "mocha test/",
"release": "uglifyjs index.js -o releases/deep-diff-$npm_package_version.min.js -r '$,require,exports,module,window,global' -m --comments '/^!/'",
"pretest": "jscs index.es.js test/ -e && jshint index.es.js test/",
"test": "mocha",
"build": "rollup index.es.js -f umd -o index.js -n DeepDiff"

"prepublish": "npm run build",
"prerelease": "npm test",
"prebuild": "npm run lint",
"pretest": "npm run build"

}
}
2 changes: 2 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ describe('deep-diff', function() {
it('should detect a difference with undefined property on lhs', function() {
var diff = deep.diff({foo: undefined }, {});

expect(diff).to.be.an(Array);
expect(diff.length).to.be(1);

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

expect(diff).to.be.an(Array);
expect(diff.length).to.be(1);

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

0 comments on commit a95aad1

Please sign in to comment.