Skip to content
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
9 changes: 7 additions & 2 deletions json-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function getDiffRepresentation(left, right) {
result.push(new Diff(key, _getInDepthArrayDiff(value, op), op, ARRAY));
}
}
result.sort(function(a,b) {return (a.key > b.key) ? 1 : ((b.key > a.key) ? -1 : 0);} );
result.sort(_sortByKeyAndOp);
return result;
}

Expand Down Expand Up @@ -145,10 +145,15 @@ function getDiffRepresentation(left, right) {
}
}

result.sort(function(a,b) {return (a.key > b.key) ? 1 : ((b.key > a.key) ? -1 : 0);} );
result.sort(_sortByKeyAndOp);
return result;
}

function _sortByKeyAndOp(a, b){
if (a.key === b.key) return (a.op === ADD) ? -1 : (a.op === REMOVE) ? 1 : 0;
return a.key > b.key ? 1 : (b.key > a.key) ? -1 : 0;
}

var leftJson = JSON.parse(left);
var rightJson = JSON.parse(right);

Expand Down
12 changes: 12 additions & 0 deletions test/spec/json-diff-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,16 @@ describe("Get Json diff representation", function() {
expect(result.diff[0].value[2].value).toEqual(12);
expect(result.diff[0].value[2].key).toEqual("e");
});

it("Diff should be sorted by key and operation", function() {
var result = getDiffRepresentation("{\"a\":1}", "{\"a\":\"1\"}");
Copy link
Owner

Choose a reason for hiding this comment

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

Please make example with values of the same type 👍


expect(result.type).toEqual(OBJECT);
expect(result.diff[0].key).toEqual("a");
expect(result.diff[0].op).toEqual(ADD);
expect(result.diff[0].valueType).toEqual(SCALAR);
expect(result.diff[1].key).toEqual("a");
expect(result.diff[1].op).toEqual(REMOVE);
expect(result.diff[1].valueType).toEqual(SCALAR);
})
});