Skip to content

Commit

Permalink
fix: op type error with an new empty string property (#545)
Browse files Browse the repository at this point in the history
Co-authored-by: heqiang <[email protected]>
  • Loading branch information
skymoonya and heqiang authored Mar 25, 2023
1 parent 50bd940 commit b4c836b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/collection-diff/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function diff(obj1, obj2, pathConverter) {
var obj1AtKey = obj1[key];
var obj2AtKey = obj2[key];

if(!(key in obj1) && obj2AtKey) {
if(!(key in obj1) && (key in obj2)) {
var obj2Value = obj2AtKey;
permutation.add.push({
op: 'add',
Expand Down
2 changes: 1 addition & 1 deletion packages/collection-diff/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function diff(obj1, obj2, pathConverter) {
var obj1AtKey = obj1[key];
var obj2AtKey = obj2[key];

if(!(key in obj1) && obj2AtKey) {
if(!(key in obj1) && (key in obj2)) {
var obj2Value = obj2AtKey;
permutation.add.push({
op: 'add',
Expand Down
16 changes: 15 additions & 1 deletion test/collection-diff/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var {
var compare = require('../../packages/collection-compare');

test('flat objects', function(t) {
t.plan(8);
t.plan(10);

var obj1 = {a: 4, b: 5};
var obj2 = {a: 3, b: 5};
Expand Down Expand Up @@ -58,6 +58,20 @@ test('flat objects', function(t) {
{op: 'replace', path: ['b'], value: 5},
])
);
t.ok(
compare(diff(obj3, obj4), [
{ op: 'remove', path: [ 'c' ] },
{ op: 'replace', path: [ 'a' ], value: 3 },
{ op: 'add', path: [ 'b' ], value: null }
])
);
t.ok(
compare(diff(obj4, obj3), [
{ op: 'remove', path: [ 'b' ] },
{ op: 'replace', path: [ 'a' ], value: 4 },
{ op: 'add', path: [ 'c' ], value: 5 }
])
);
});

test('flat null and undefined', function(t) {
Expand Down

0 comments on commit b4c836b

Please sign in to comment.