Skip to content

Commit

Permalink
fix issue Starcounter-Jack#76 by using deepClone
Browse files Browse the repository at this point in the history
  • Loading branch information
baranga committed May 3, 2016
1 parent 3f208ef commit 3e1dfc7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/json-patch-duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ var jsonpatch;
/* The operations applicable to an object */
var objOps = {
add: function (obj, key) {
obj[key] = this.value;
obj[key] = deepClone(this.value);
return true;
},
remove: function (obj, key) {
delete obj[key];
return true;
},
replace: function (obj, key) {
obj[key] = this.value;
obj[key] = deepClone(this.value);
return true;
},
move: function (obj, key, tree) {
Expand Down Expand Up @@ -110,15 +110,15 @@ var jsonpatch;
/* The operations applicable to an array. Many are the same as for the object */
var arrOps = {
add: function (arr, i) {
arr.splice(i, 0, this.value);
arr.splice(i, 0, deepClone(this.value));
return true;
},
remove: function (arr, i) {
arr.splice(i, 1);
return true;
},
replace: function (arr, i) {
arr[i] = this.value;
arr[i] = deepClone(this.value);
return true;
},
move: objOps.move,
Expand All @@ -132,7 +132,7 @@ var jsonpatch;
rootOps.remove.call(this, obj);
for (var key in this.value) {
if (this.value.hasOwnProperty(key)) {
obj[key] = this.value[key];
obj[key] = deepClone(this.value[key]);
}
}
return true;
Expand All @@ -150,7 +150,7 @@ var jsonpatch;
{ op: "remove", path: this.path }
]);
apply(obj, [
{ op: "add", path: this.path, value: this.value }
{ op: "add", path: this.path, value: deepClone(this.value) }
]);
return true;
},
Expand Down
12 changes: 6 additions & 6 deletions src/json-patch-duplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ module jsonpatch {
/* The operations applicable to an object */
var objOps = {
add: function (obj, key) {
obj[key] = this.value;
obj[key] = deepClone(this.value);
return true;
},
remove: function (obj, key) {
delete obj[key];
return true;
},
replace: function (obj, key) {
obj[key] = this.value;
obj[key] = deepClone(this.value);
return true;
},
move: function (obj, key, tree) {
Expand Down Expand Up @@ -125,15 +125,15 @@ module jsonpatch {
/* The operations applicable to an array. Many are the same as for the object */
var arrOps = {
add: function (arr, i) {
arr.splice(i, 0, this.value);
arr.splice(i, 0, deepClone(this.value));
return true;
},
remove: function (arr, i) {
arr.splice(i, 1);
return true;
},
replace: function (arr, i) {
arr[i] = this.value;
arr[i] = deepClone(this.value);
return true;
},
move: objOps.move,
Expand All @@ -148,7 +148,7 @@ module jsonpatch {
rootOps.remove.call(this, obj);
for (var key in this.value) {
if (this.value.hasOwnProperty(key)) {
obj[key] = this.value[key];
obj[key] = deepClone(this.value[key]);
}
}
return true;
Expand All @@ -166,7 +166,7 @@ module jsonpatch {
{op: "remove", path: this.path}
]);
apply(obj, [
{op: "add", path: this.path, value: this.value}
{op: "add", path: this.path, value: deepClone(this.value)}
]);
return true;
},
Expand Down
20 changes: 14 additions & 6 deletions src/json-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ var jsonpatch;
/* The operations applicable to an object */
var objOps = {
add: function (obj, key) {
obj[key] = this.value;
obj[key] = deepClone(this.value);
return true;
},
remove: function (obj, key) {
delete obj[key];
return true;
},
replace: function (obj, key) {
obj[key] = this.value;
obj[key] = deepClone(this.value);
return true;
},
move: function (obj, key, tree) {
Expand Down Expand Up @@ -104,15 +104,15 @@ var jsonpatch;
/* The operations applicable to an array. Many are the same as for the object */
var arrOps = {
add: function (arr, i) {
arr.splice(i, 0, this.value);
arr.splice(i, 0, deepClone(this.value));
return true;
},
remove: function (arr, i) {
arr.splice(i, 1);
return true;
},
replace: function (arr, i) {
arr[i] = this.value;
arr[i] = deepClone(this.value);
return true;
},
move: objOps.move,
Expand All @@ -126,7 +126,7 @@ var jsonpatch;
rootOps.remove.call(this, obj);
for (var key in this.value) {
if (this.value.hasOwnProperty(key)) {
obj[key] = this.value[key];
obj[key] = deepClone(this.value[key]);
}
}
return true;
Expand All @@ -144,7 +144,7 @@ var jsonpatch;
{ op: "remove", path: this.path }
]);
apply(obj, [
{ op: "add", path: this.path, value: this.value }
{ op: "add", path: this.path, value: deepClone(this.value) }
]);
return true;
},
Expand All @@ -157,6 +157,14 @@ var jsonpatch;
this.value = obj;
}
};
function deepClone(obj) {
if (typeof obj === "object") {
return JSON.parse(JSON.stringify(obj)); //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
}
else {
return obj; //no need to clone primitives
}
}
var _isArray;
if (Array.isArray) {
_isArray = Array.isArray;
Expand Down
21 changes: 15 additions & 6 deletions src/json-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ module jsonpatch {
/* The operations applicable to an object */
var objOps = {
add: function (obj, key) {
obj[key] = this.value;
obj[key] = deepClone(this.value);
return true;
},
remove: function (obj, key) {
delete obj[key];
return true;
},
replace: function (obj, key) {
obj[key] = this.value;
obj[key] = deepClone(this.value);
return true;
},
move: function (obj, key, tree) {
Expand Down Expand Up @@ -109,15 +109,15 @@ module jsonpatch {
/* The operations applicable to an array. Many are the same as for the object */
var arrOps = {
add: function (arr, i) {
arr.splice(i, 0, this.value);
arr.splice(i, 0, deepClone(this.value));
return true;
},
remove: function (arr, i) {
arr.splice(i, 1);
return true;
},
replace: function (arr, i) {
arr[i] = this.value;
arr[i] = deepClone(this.value);
return true;
},
move: objOps.move,
Expand All @@ -132,7 +132,7 @@ module jsonpatch {
rootOps.remove.call(this, obj);
for (var key in this.value) {
if (this.value.hasOwnProperty(key)) {
obj[key] = this.value[key];
obj[key] = deepClone(this.value[key]);
}
}
return true;
Expand All @@ -150,7 +150,7 @@ module jsonpatch {
{op: "remove", path: this.path}
]);
apply(obj, [
{op: "add", path: this.path, value: this.value}
{op: "add", path: this.path, value: deepClone(this.value)}
]);
return true;
},
Expand All @@ -164,6 +164,15 @@ module jsonpatch {
}
};

function deepClone(obj:any) {
if (typeof obj === "object") {
return JSON.parse(JSON.stringify(obj)); //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
}
else {
return obj; //no need to clone primitives
}
}

var _isArray;
if (Array.isArray) { //standards; http://jsperf.com/isarray-shim/4
_isArray = Array.isArray;
Expand Down

0 comments on commit 3e1dfc7

Please sign in to comment.