Skip to content

Commit a64fc38

Browse files
author
pkafel
committed
#10 Handling of different input types
1 parent ee19c60 commit a64fc38

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

json-diff.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function Diff(key, value, op, valueType) {
1919
}
2020

2121
function TopDiff(type, diff) {
22-
this.type = type;
22+
this.topType = type;
2323
this.diff = diff;
2424
}
2525

@@ -159,5 +159,9 @@ function getDiffRepresentation(left, right) {
159159

160160
if(leftJson instanceof Array && rightJson instanceof Array) return new TopDiff(ARRAY, _getArraysDiff(leftJson, rightJson));
161161
else if(!(leftJson instanceof Array) && !(rightJson instanceof Array)) return new TopDiff(OBJECT, _getJsonsDiff(leftJson, rightJson));
162-
else throw "Nothing in common !";
162+
else {
163+
var leftOutput = new Diff(null, _getInDepthDiff(leftJson, ADD), ADD, _getType(leftJson));
164+
var rightOutput = new Diff(null, _getInDepthDiff(rightJson, REMOVE), REMOVE, _getType(rightJson));
165+
return new TopDiff(NONE, [leftOutput, rightOutput]);
166+
}
163167
}

test/spec/json-diff-spec.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ describe("Get Json diff representation", function() {
22

33
it("For two arrays with different value should return correct diff", function() {
44
var result = getDiffRepresentation("[1,2]", "[4,5]");
5-
expect(result.type).toEqual(ARRAY);
5+
expect(result.topType).toEqual(ARRAY);
66
expect(result.diff[0].value).toEqual(1);
77
expect(result.diff[0].op).toEqual(ADD);
88
expect(result.diff[0].valueType).toEqual(SCALAR);
@@ -19,7 +19,7 @@ describe("Get Json diff representation", function() {
1919

2020
it("For two arrays with the same values should return correct diff", function() {
2121
var result = getDiffRepresentation("[1,2]", "[1,2]");
22-
expect(result.type).toEqual(ARRAY);
22+
expect(result.topType).toEqual(ARRAY);
2323
expect(result.diff[0].value).toEqual(1);
2424
expect(result.diff[0].op).toEqual(NONE);
2525
expect(result.diff[0].valueType).toEqual(SCALAR);
@@ -30,7 +30,7 @@ describe("Get Json diff representation", function() {
3030

3131
it("For two the same flat JSONs should return object without any differences", function() {
3232
var result = getDiffRepresentation("{\"key1\": 123, \"key2\": \"some value\"}", "{\"key2\": \"some value\", \"key1\": 123}");
33-
expect(result.type).toEqual(OBJECT);
33+
expect(result.topType).toEqual(OBJECT);
3434
expect(result.diff[0].key).toEqual("key1");
3535
expect(result.diff[0].value).toEqual(123);
3636
expect(result.diff[0].op).toEqual(NONE);
@@ -44,7 +44,7 @@ describe("Get Json diff representation", function() {
4444
it("For two arrays with flat JSONs on it should return correct diff", function() {
4545
var result = getDiffRepresentation("[1,2,{\"key1\": 234, \"key2\": \"val\"}]",
4646
"[3,2,{\"key2\": 234, \"key3\": \"val\"}]");
47-
expect(result.type).toEqual(ARRAY);
47+
expect(result.topType).toEqual(ARRAY);
4848
expect(result.diff[0].value).toEqual(1);
4949
expect(result.diff[0].op).toEqual(ADD);
5050
expect(result.diff[0].valueType).toEqual(SCALAR);
@@ -77,7 +77,7 @@ describe("Get Json diff representation", function() {
7777

7878
it("Array of arrays and empty flat array should be different", function() {
7979
var result = getDiffRepresentation("[]", "[[],[]]");
80-
expect(result.type).toEqual(ARRAY);
80+
expect(result.topType).toEqual(ARRAY);
8181
expect(result.diff[0].value).toEqual([]);
8282
expect(result.diff[0].op).toEqual(REMOVE);
8383
expect(result.diff[0].valueType).toEqual(ARRAY);
@@ -86,17 +86,29 @@ describe("Get Json diff representation", function() {
8686
expect(result.diff[1].valueType).toEqual(ARRAY);
8787
});
8888

89-
it("Array and JSON object have nothing in common so should throw exception", function() {
90-
var call = function() {
91-
return getDiffRepresentation("[]", "{}");
92-
};
93-
expect(call).toThrow();
89+
it("Array and JSON object have nothing in common so returned diff should represent that", function() {
90+
var result = getDiffRepresentation("[1,2]", "{\"a\": \"hello\"}");
91+
expect(result.topType).toEqual(NONE);
92+
expect(result.diff[0].op).toEqual(ADD);
93+
expect(result.diff[0].valueType).toEqual(ARRAY);
94+
expect(result.diff[0].value[0].op).toEqual(ADD);
95+
expect(result.diff[0].value[0].valueType).toEqual(SCALAR);
96+
expect(result.diff[0].value[0].value).toEqual(1);
97+
expect(result.diff[0].value[1].op).toEqual(ADD);
98+
expect(result.diff[0].value[1].valueType).toEqual(SCALAR);
99+
expect(result.diff[0].value[1].value).toEqual(2);
100+
expect(result.diff[1].op).toEqual(REMOVE);
101+
expect(result.diff[1].valueType).toEqual(OBJECT);
102+
expect(result.diff[1].value[0].op).toEqual(REMOVE);
103+
expect(result.diff[1].value[0].valueType).toEqual(SCALAR);
104+
expect(result.diff[1].value[0].key).toEqual("a");
105+
expect(result.diff[1].value[0].value).toEqual("hello");
94106
});
95107

96108
it("Two similar with small difference hidden in depth should be different", function() {
97109
var result = getDiffRepresentation("{\"a\":{\"b\":{\"c\":\"d\"}}}", "{\"a\":{\"b\":{\"c\":\"e\"}}}");
98110

99-
expect(result.type).toEqual(OBJECT);
111+
expect(result.topType).toEqual(OBJECT);
100112
expect(result.diff[0].key).toEqual("a");
101113
expect(result.diff[0].op).toEqual(NONE);
102114
expect(result.diff[0].valueType).toEqual(OBJECT);
@@ -117,7 +129,7 @@ describe("Get Json diff representation", function() {
117129
var result = getDiffRepresentation("{\"a\":[1, 2, 3]}",
118130
"{\"b\":{\"c\":12,\"d\":[1, 2]}}");
119131

120-
expect(result.type).toEqual(OBJECT);
132+
expect(result.topType).toEqual(OBJECT);
121133
expect(result.diff[0].key).toEqual("a");
122134
expect(result.diff[0].op).toEqual(ADD);
123135
expect(result.diff[0].valueType).toEqual(ARRAY);
@@ -153,7 +165,7 @@ describe("Get Json diff representation", function() {
153165
var result = getDiffRepresentation("{\"a\":{\"e\":12,\"b\":32,\"d\":11}}",
154166
"{}");
155167

156-
expect(result.type).toEqual(OBJECT);
168+
expect(result.topType).toEqual(OBJECT);
157169
expect(result.diff[0].key).toEqual("a");
158170
expect(result.diff[0].op).toEqual(ADD);
159171
expect(result.diff[0].valueType).toEqual(OBJECT);

0 commit comments

Comments
 (0)