Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the two operands are of the same type before interpreting a missing prop as a remove op #205

Merged
merged 6 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: node_js
dist: trusty
before_script:
- npm install
node_js: 8
script:
- npm test && npm run bench
7 changes: 6 additions & 1 deletion src/duplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ function _generate(mirror, obj, patches, path) {
for (var t = oldKeys.length - 1; t >= 0; t--) {
var key = oldKeys[t];
var oldVal = mirror[key];

if (hasOwnProperty(obj, key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) {
var newVal = obj[key];

if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null) {
_generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key));
}
Expand All @@ -199,9 +201,12 @@ function _generate(mirror, obj, patches, path) {
}
}
}
else {
else if(Array.isArray(mirror) === Array.isArray(obj)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some code comments about what's happening here? Why for two arrays or two non-arrays it's a delete, but for array and non-array, it's a replace?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add something of the effect of: Because the replace is for the whole obj, not for obj[key].

patches.push({ op: "remove", path: path + "/" + escapePathComponent(key) });
deleted = true; // property has been deleted
} else {
patches.push({ op: "replace", path, value: obj });
changed = true;
}
}

Expand Down
41 changes: 40 additions & 1 deletion test/spec/duplexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1426,8 +1426,47 @@ describe('duplex', function() {
}, 20);
});
});

describe('compare', function() {
it('Replacing a root array with an object should be handled well', function() {

const obj = {};
var patches = jsonpatch.compare(['jack'], obj);
expect(patches).toEqual([
{
op: 'replace',
path: '',
value: obj
}
]);

});
it('Replacing an array with an object should be handled well', function() {

const obj = {};
var patches = jsonpatch.compare({arr: ['jack']}, {arr: obj});
expect(patches).toEqual([
{
op: 'replace',
path: '/arr',
value: obj
}
]);

});
it('Replacing an array that nested in an object with an object nested in an an object should be handled well', function() {

const obj = {};
var patches = jsonpatch.compare({arr: {deeperArray: ['jack']}}, {arr: {deeperArray: obj}});

expect(patches).toEqual([
{
op: 'replace',
path: '/arr/deeperArray',
value: obj
}
]);

});
it('should return an add for a property that does not exist in the first obj', function() {
var objA = {
user: {
Expand Down