From c2ec00a16adea4b1d5e21cd20b5e25b51dd55d6a Mon Sep 17 00:00:00 2001 From: Brandon Barker Date: Tue, 2 Apr 2024 20:01:50 -0400 Subject: [PATCH] [fix] [102] Fix deep diffing issue for obj in array --- lib/index.js | 13 +++++++++---- package.json | 2 +- test/diff_test.coffee | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index c952d80..8cd165b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -232,7 +232,8 @@ class JsonDiff { } break case 'replace': - if (!this.options.keysOnly) { + // if the replacement is not one-to-one default to full diff + if (this.options.keysOnly || i2 - i1 !== j2 - j1) { let asc3, end3 let asc4, end4 for ( @@ -262,11 +263,15 @@ class JsonDiff { this.descalarize(seq1[i], originals1), this.descalarize(seq2[i - i1 + j1], originals2) ) - if (!change.equal) { + + if (change.equal) { + result.push([' ']) + } else if (change.score === 0) { + result.push(['-', this.descalarize(seq1[i], originals1)]) + result.push(['+', this.descalarize(seq2[i], originals2)]) + } else { result.push(['~', change.result]) equal = false - } else { - result.push([' ']) } } } diff --git a/package.json b/package.json index 74f9c52..3dfa217 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ ], "name": "json-diff", "description": "JSON diff", - "version": "1.0.6", + "version": "1.1.0", "homepage": "https://github.com/andreyvit/json-diff", "license": "MIT", "repository": { diff --git a/test/diff_test.coffee b/test/diff_test.coffee index 07885a1..dc64c75 100644 --- a/test/diff_test.coffee +++ b/test/diff_test.coffee @@ -219,6 +219,21 @@ describe 'diff({full: true})', -> { foo: 30, bar: { bbbar: 92, bbboz: 34 } }], {full: true}) ) + it "should return [[' ', ], ['~', ], [' ', ]] for two arrays when an item has been modified", -> + assert.deepEqual( + diff( + [{"holes": [{ "depth": 11.5, "diameter": 8, "start": { "x": 218, "y": 241, "z": 9 }, "end": { "x": 220, "y": 241, "z": -2.5 }, "plankFaceId": "0", "name": "短木销_修改", "type": "PLANK" }, { "depth": 11.5, "diameter": 8, "start": { "x": 218, "y": 241, "z": 9 }, "end": { "x": 220, "y": 241, "z": -2.5 }, "plankFaceId": "0", "name": "短木销_修改", "type": "PLANK" }]}], + [{"holes": [{ "depth": 11.5, "diameter": 8, "start": { "x": 218, "y": 241, "z": 9 }, "end": { "x": 218, "y": 241, "z": -2.5 }, "plankFaceId": "0", "name": "短木销", "type": "PLANK" }, { "depth": 11.5, "diameter": 8, "start": { "x": 218, "y": 241, "z": 9 }, "end": { "x": 218, "y": 241, "z": -2.5 }, "plankFaceId": "0", "name": "短木销", "type": "PLANK" }]}], + {} + ), + [["~", { + "holes": [ + ["~", {"end": {"x": {"__old": 220, "__new": 218}}, "name": {"__old": "短木销_修改", "__new": "短木销"}}], + ["~", {"end": {"x": {"__old": 220, "__new": 218}}, "name": {"__old": "短木销_修改", "__new": "短木销"}}] + ] + }]] + ) + describe 'diff({ outputKeys: foo,bar }', -> it "should return keys foo and bar although they have no changes", ->