From 1fdabb6f5fbd510df496182d34ba32339b11c49b Mon Sep 17 00:00:00 2001 From: heqiang Date: Tue, 14 Mar 2023 18:29:08 +0800 Subject: [PATCH] fix: op type error with an new empty string property --- packages/collection-diff/index.cjs | 2 +- packages/collection-diff/index.mjs | 2 +- test/collection-diff/index.cjs | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/collection-diff/index.cjs b/packages/collection-diff/index.cjs index 5f4fa3400..0c2daf6a9 100644 --- a/packages/collection-diff/index.cjs +++ b/packages/collection-diff/index.cjs @@ -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', diff --git a/packages/collection-diff/index.mjs b/packages/collection-diff/index.mjs index 7c32c57f5..f965f7646 100644 --- a/packages/collection-diff/index.mjs +++ b/packages/collection-diff/index.mjs @@ -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', diff --git a/test/collection-diff/index.cjs b/test/collection-diff/index.cjs index 5c230686a..166879663 100644 --- a/test/collection-diff/index.cjs +++ b/test/collection-diff/index.cjs @@ -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}; @@ -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) {