From d9a800f5f2d0f4c1ec81ac50fda389960bfb94a3 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Thu, 5 Mar 2020 20:51:16 -0500 Subject: [PATCH] fix(core): fix json diff and implicitJsonChanges part 2 --- .../workspace/src/utils/json-diff.spec.ts | 32 +++++++++++++++++++ packages/workspace/src/utils/json-diff.ts | 5 ++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/workspace/src/utils/json-diff.spec.ts b/packages/workspace/src/utils/json-diff.spec.ts index 1ec2dda564d7d..655030996fc72 100644 --- a/packages/workspace/src/utils/json-diff.spec.ts +++ b/packages/workspace/src/utils/json-diff.spec.ts @@ -108,6 +108,38 @@ describe('jsonDiff', () => { }); }); + it('should work for added array values', () => { + const result = jsonDiff( + { + rules: undefined + }, + { + rules: ['rule1'] + } + ); + + expect(result).toEqual( + expect.arrayContaining([ + { + type: DiffType.Modified, + path: ['rules'], + value: { + lhs: undefined, + rhs: ['rule1'] + } + }, + { + type: DiffType.Added, + path: ['rules', '0'], + value: { + lhs: undefined, + rhs: 'rule1' + } + } + ]) + ); + }); + it('should work for added array items', () => { const result = jsonDiff( { diff --git a/packages/workspace/src/utils/json-diff.ts b/packages/workspace/src/utils/json-diff.ts index 127051a748e70..8dd9cc2be7122 100644 --- a/packages/workspace/src/utils/json-diff.ts +++ b/packages/workspace/src/utils/json-diff.ts @@ -49,7 +49,7 @@ export function jsonDiff(lhs: any, rhs: any): JsonChange[] { } }); } - return typeof lhsValue === 'object'; + return typeof lhsValue === 'object' || Array.isArray(lhsValue); }); walkJsonTree(rhs, [], (path, rhsValue) => { @@ -63,9 +63,8 @@ export function jsonDiff(lhs: any, rhs: any): JsonChange[] { rhs: rhsValue } }); - return false; } - return typeof rhsValue === 'object'; + return typeof rhsValue === 'object' || Array.isArray(rhsValue); }); return result;