fast-json-patch
Advanced tools
Comparing version 0.3.9 to 0.3.10
/*! | ||
* json-patch-duplex.js 0.3.8 | ||
* json-patch-duplex.js 0.3.10 | ||
* (c) 2013 Joachim Wester | ||
* MIT license | ||
*/ | ||
var jsonpatch;!function(a){function b(a){return-1===a.indexOf("/")&&-1===a.indexOf("~")?a:a.replace(/~/g,"~0").replace(/\//g,"~1")}function c(a,d){var e;for(var f in a)if(a.hasOwnProperty(f)){if(a[f]===d)return b(f)+"/";if("object"==typeof a[f]&&(e=c(a[f],d),""!=e))return b(f)+"/"+e}return""}function d(a,b){if(a===b)return"/";var d=c(a,b);if(""===d)throw new Error("Object not found in root");return"/"+d}function e(a){for(var b=0,c=s.length;c>b;b++)if(s[b].obj===a)return s[b]}function f(a,b){for(var c=0,d=a.observers.length;d>c;c++)if(a.observers[c].callback===b)return a.observers[c].observer}function g(a,b){for(var c=0,d=a.observers.length;d>c;c++)if(a.observers[c].observer===b)return void a.observers.splice(c,1)}function h(a,b){l(b),Object.observe?k(b,a):clearTimeout(b.next);var c=e(a);g(c,b)}function i(a,b){var c,g=[],h=a,i=e(a);if(i?c=f(i,b):(i=new t(a),s.push(i)),c)return c;if(Object.observe)c=function(e){k(c,a),j(c,a);for(var f=0,i=e.length;i>f;){if(("length"!==e[f].name||!w(e[f].object))&&"__Jasmine_been_here_before__"!==e[f].name){var l=e[f].type;switch(l){case"new":l="add";break;case"deleted":l="delete";break;case"updated":l="update"}r[l].call(e[f],g,d(h,e[f].object))}f++}g&&b&&b(g),c.patches=g,g=[]};else if(c={},i.value=JSON.parse(JSON.stringify(a)),b){c.callback=b,c.next=null;var m=this.intervals||[100,1e3,1e4,6e4];if(void 0===m.push)throw new Error("jsonpatch.intervals must be an array");var n=0,o=function(){l(c)},p=function(){clearTimeout(c.next),c.next=setTimeout(function(){o(),n=0,c.next=setTimeout(q,m[n++])},0)},q=function(){o(),n==m.length&&(n=m.length-1),c.next=setTimeout(q,m[n++])};"undefined"!=typeof window&&(window.addEventListener?(window.addEventListener("mousedown",p),window.addEventListener("mouseup",p),window.addEventListener("keydown",p)):(window.attachEvent("onmousedown",p),window.attachEvent("onmouseup",p),window.attachEvent("onkeydown",p))),c.next=setTimeout(q,m[n++])}return c.patches=g,c.object=a,i.observers.push(new u(b,c)),j(c,a)}function j(a,b){if(Object.observe){Object.observe(b,a);for(var c in b)if(b.hasOwnProperty(c)){var d=b[c];d&&"object"==typeof d&&j(a,d)}}return a}function k(a,b){if(Object.observe){Object.unobserve(b,a);for(var c in b)if(b.hasOwnProperty(c)){var d=b[c];d&&"object"==typeof d&&k(a,d)}}return a}function l(a){if(Object.observe)Object.deliverChangeRecords(a);else{for(var b,c=0,d=s.length;d>c;c++)if(s[c].obj===a.object){b=s[c];break}m(b.value,a.object,a.patches,"")}var e=a.patches;return e.length>0&&(a.patches=[],a.callback&&a.callback(e)),e}function m(a,c,d,e){for(var f=v(c),g=v(a),h=!1,i=!1,j=g.length-1;j>=0;j--){var k=g[j],l=a[k];if(c.hasOwnProperty(k)){var n=c[k];l instanceof Object?m(l,n,d,e+"/"+b(k)):l!=n&&(h=!0,d.push({op:"replace",path:e+"/"+b(k),value:n}),a[k]=n)}else d.push({op:"remove",path:e+"/"+b(k)}),delete a[k],i=!0}if(i||f.length!=g.length)for(var j=0;j<f.length;j++){var k=f[j];a.hasOwnProperty(k)||(d.push({op:"add",path:e+"/"+b(k),value:c[k]}),a[k]=JSON.parse(JSON.stringify(c[k])))}}function n(a,b){for(var c,d=!1,e=0,f=b.length;f>e;){c=b[e];for(var g=c.path.split("/"),h=a,i=1,j=g.length;;)if(w(h)){var k=parseInt(g[i],10);if(i++,i>=j){d=q[c.op].call(c,h,k,a);break}h=h[k]}else{var l=g[i];if(-1!=l.indexOf("~")&&(l=l.replace(/~1/g,"/").replace(/~0/g,"~")),i++,i>=j){d=p[c.op].call(c,h,l,a);break}h=h[l]}e++}return d}function o(a,b){var c=[];return m(a,b,c,""),c}var p={add:function(a,b){return a[b]=this.value,!0},remove:function(a,b){return delete a[b],!0},replace:function(a,b){return a[b]=this.value,!0},move:function(a,b,c){var d={op:"_get",path:this.from};return n(c,[d]),n(c,[{op:"remove",path:this.from}]),n(c,[{op:"add",path:this.path,value:d.value}]),!0},copy:function(a,b,c){var d={op:"_get",path:this.from};return n(c,[d]),n(c,[{op:"add",path:this.path,value:d.value}]),!0},test:function(a,b){return JSON.stringify(a[b])===JSON.stringify(this.value)},_get:function(a,b){this.value=a[b]}},q={add:function(a,b){return a.splice(b,0,this.value),!0},remove:function(a,b){return a.splice(b,1),!0},replace:function(a,b){return a[b]=this.value,!0},move:p.move,copy:p.copy,test:p.test,_get:p._get},r={add:function(a,c){var d={op:"add",path:c+b(this.name),value:this.object[this.name]};a.push(d)},"delete":function(a,c){var d={op:"remove",path:c+b(this.name)};a.push(d)},update:function(a,c){var d={op:"replace",path:c+b(this.name),value:this.object[this.name]};a.push(d)}},s=[];a.intervals;var t=function(){function a(a){this.observers=[],this.obj=a}return a}(),u=function(){function a(a,b){this.callback=a,this.observer=b}return a}();a.unobserve=h,a.observe=i,a.generate=l;var v;v=Object.keys?Object.keys:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c);return b};var w;w=Array.isArray?Array.isArray:function(a){return a.push&&"number"==typeof a.length},a.apply=n,a.compare=o}(jsonpatch||(jsonpatch={})),"undefined"!=typeof exports&&(exports.apply=jsonpatch.apply,exports.observe=jsonpatch.observe,exports.unobserve=jsonpatch.unobserve,exports.generate=jsonpatch.generate); | ||
var jsonpatch;!function(a){function b(a,c){switch(typeof a){case"undefined":case"boolean":case"string":case"number":return a===c;case"object":if(null===a)return null===c;if(y(a)){if(!y(c)||a.length!==c.length)return!1;for(var d=0,e=a.length;e>d;d++)if(!b(a[d],c[d]))return!1;return!0}var f=q(c),g=f.length;if(q(a).length!==g)return!1;for(var d=0;g>d;d++)if(!b(a[d],c[d]))return!1;return!0;default:return!1}}function c(a){return-1===a.indexOf("/")&&-1===a.indexOf("~")?a:a.replace(/~/g,"~0").replace(/\//g,"~1")}function d(a,b){var e;for(var f in a)if(a.hasOwnProperty(f)){if(a[f]===b)return c(f)+"/";if("object"==typeof a[f]&&(e=d(a[f],b),""!=e))return c(f)+"/"+e}return""}function e(a,b){if(a===b)return"/";var c=d(a,b);if(""===c)throw new Error("Object not found in root");return"/"+c}function f(a){for(var b=0,c=v.length;c>b;b++)if(v[b].obj===a)return v[b]}function g(a,b){for(var c=0,d=a.observers.length;d>c;c++)if(a.observers[c].callback===b)return a.observers[c].observer}function h(a,b){for(var c=0,d=a.observers.length;d>c;c++)if(a.observers[c].observer===b)return void a.observers.splice(c,1)}function i(a,b){m(b),Object.observe?l(b,a):clearTimeout(b.next);var c=f(a);h(c,b)}function j(a,b){var c,d=[],h=a,i=f(a);if(i?c=g(i,b):(i=new w(a),v.push(i)),c)return c;if(Object.observe)c=function(f){l(c,a),k(c,a);for(var g=0,i=f.length;i>g;){if(("length"!==f[g].name||!y(f[g].object))&&"__Jasmine_been_here_before__"!==f[g].name){var j=f[g].type;switch(j){case"new":j="add";break;case"deleted":j="delete";break;case"updated":j="update"}u[j].call(f[g],d,e(h,f[g].object))}g++}d&&b&&b(d),c.patches=d,d=[]};else if(c={},i.value=JSON.parse(JSON.stringify(a)),b){c.callback=b,c.next=null;var j=this.intervals||[100,1e3,1e4,6e4];if(void 0===j.push)throw new Error("jsonpatch.intervals must be an array");var n=0,o=function(){m(c)},p=function(){clearTimeout(c.next),c.next=setTimeout(function(){o(),n=0,c.next=setTimeout(q,j[n++])},0)},q=function(){o(),n==j.length&&(n=j.length-1),c.next=setTimeout(q,j[n++])};"undefined"!=typeof window&&(window.addEventListener?(window.addEventListener("mousedown",p),window.addEventListener("mouseup",p),window.addEventListener("keydown",p)):(window.attachEvent("onmousedown",p),window.attachEvent("onmouseup",p),window.attachEvent("onkeydown",p))),c.next=setTimeout(q,j[n++])}return c.patches=d,c.object=a,i.observers.push(new x(b,c)),k(c,a)}function k(a,b){if(Object.observe){Object.observe(b,a);for(var c in b)if(b.hasOwnProperty(c)){var d=b[c];d&&"object"==typeof d&&k(a,d)}}return a}function l(a,b){if(Object.observe){Object.unobserve(b,a);for(var c in b)if(b.hasOwnProperty(c)){var d=b[c];d&&"object"==typeof d&&l(a,d)}}return a}function m(a){if(Object.observe)Object.deliverChangeRecords(a);else{for(var b,c=0,d=v.length;d>c;c++)if(v[c].obj===a.object){b=v[c];break}n(b.value,a.object,a.patches,"")}var e=a.patches;return e.length>0&&(a.patches=[],a.callback&&a.callback(e)),e}function n(a,b,d,e){for(var f=q(b),g=q(a),h=!1,i=!1,j=g.length-1;j>=0;j--){var k=g[j],l=a[k];if(b.hasOwnProperty(k)){var m=b[k];l instanceof Object?n(l,m,d,e+"/"+c(k)):l!=m&&(h=!0,d.push({op:"replace",path:e+"/"+c(k),value:m}),a[k]=m)}else d.push({op:"remove",path:e+"/"+c(k)}),delete a[k],i=!0}if(i||f.length!=g.length)for(var j=0;j<f.length;j++){var k=f[j];a.hasOwnProperty(k)||(d.push({op:"add",path:e+"/"+c(k),value:b[k]}),a[k]=JSON.parse(JSON.stringify(b[k])))}}function o(a,b){for(var c,d=!1,e=0,f=b.length;f>e;){c=b[e],e++;for(var g=c.path.split("/"),h=a,i=1,j=g.length;;)if(y(h)){var k=parseInt(g[i],10);if(i++,i>=j){d=s[c.op].call(c,h,k,a);break}h=h[k]}else{var l=g[i];if(l){if(l&&-1!=l.indexOf("~")&&(l=l.replace(/~1/g,"/").replace(/~0/g,"~")),i++,i>=j){d=r[c.op].call(c,h,l,a);break}}else if(i++,i>=j){d=t[c.op].call(c,h,l,a);break}h=h[l]}}return d}function p(a,b){var c=[];return n(a,b,c,""),c}var q=function(){return Object.keys?Object.keys:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c);return b}}(),r={add:function(a,b){return a[b]=this.value,!0},remove:function(a,b){return delete a[b],!0},replace:function(a,b){return a[b]=this.value,!0},move:function(a,b,c){var d={op:"_get",path:this.from};return o(c,[d]),o(c,[{op:"remove",path:this.from}]),o(c,[{op:"add",path:this.path,value:d.value}]),!0},copy:function(a,b,c){var d={op:"_get",path:this.from};return o(c,[d]),o(c,[{op:"add",path:this.path,value:d.value}]),!0},test:function(a,c){return b(a[c],this.value)},_get:function(a,b){this.value=a[b]}},s={add:function(a,b){return a.splice(b,0,this.value),!0},remove:function(a,b){return a.splice(b,1),!0},replace:function(a,b){return a[b]=this.value,!0},move:r.move,copy:r.copy,test:r.test,_get:r._get},t={add:function(a){for(var b in this.value)this.value.hasOwnProperty(b)&&(a[b]=this.value[b]);return!0},remove:function(a){for(var b in a)a.hasOwnProperty(b)&&r.remove.call(this,a,b);return!0},replace:function(a){return o(a,[{op:"remove",path:this.path}]),o(a,[{op:"add",path:this.path,value:this.value}]),!0},move:r.move,copy:r.copy,test:function(a){return JSON.stringify(a)===JSON.stringify(this.value)},_get:r._get},u={add:function(a,b){var d={op:"add",path:b+c(this.name),value:this.object[this.name]};a.push(d)},"delete":function(a,b){var d={op:"remove",path:b+c(this.name)};a.push(d)},update:function(a,b){var d={op:"replace",path:b+c(this.name),value:this.object[this.name]};a.push(d)}},v=[];a.intervals;var w=function(){function a(a){this.observers=[],this.obj=a}return a}(),x=function(){function a(a,b){this.callback=a,this.observer=b}return a}();a.unobserve=i,a.observe=j,a.generate=m;var y;y=Array.isArray?Array.isArray:function(a){return a.push&&"number"==typeof a.length},a.apply=o,a.compare=p}(jsonpatch||(jsonpatch={})),"undefined"!=typeof exports&&(exports.apply=jsonpatch.apply,exports.observe=jsonpatch.observe,exports.unobserve=jsonpatch.unobserve,exports.generate=jsonpatch.generate,exports.compare=jsonpatch.compare); | ||
//# sourceMappingURL=json-patch-duplex.min.js.map |
/*! | ||
* json-patch-duplex.js 0.3.8 | ||
* json-patch-duplex.js 0.3.10 | ||
* (c) 2013 Joachim Wester | ||
* MIT license | ||
*/ | ||
var jsonpatch;!function(a){function b(a,b){for(var f,g=!1,h=0,i=b.length;i>h;){f=b[h];for(var j=f.path.split("/"),k=a,l=1,m=j.length;;)if(c(k)){var n=parseInt(j[l],10);if(l++,l>=m){g=e[f.op].call(f,k,n,a);break}k=k[n]}else{var o=j[l];if(-1!=o.indexOf("~")&&(o=o.replace(/~1/g,"/").replace(/~0/g,"~")),l++,l>=m){g=d[f.op].call(f,k,o,a);break}k=k[o]}h++}return g}var c,d={add:function(a,b){return a[b]=this.value,!0},remove:function(a,b){return delete a[b],!0},replace:function(a,b){return a[b]=this.value,!0},move:function(a,c,d){var e={op:"_get",path:this.from};return b(d,[e]),b(d,[{op:"remove",path:this.from}]),b(d,[{op:"add",path:this.path,value:e.value}]),!0},copy:function(a,c,d){var e={op:"_get",path:this.from};return b(d,[e]),b(d,[{op:"add",path:this.path,value:e.value}]),!0},test:function(a,b){return JSON.stringify(a[b])===JSON.stringify(this.value)},_get:function(a,b){this.value=a[b]}},e={add:function(a,b){return a.splice(b,0,this.value),!0},remove:function(a,b){return a.splice(b,1),!0},replace:function(a,b){return a[b]=this.value,!0},move:d.move,copy:d.copy,test:d.test,_get:d._get};c=Array.isArray?Array.isArray:function(a){return a.push&&"number"==typeof a.length},a.apply=b}(jsonpatch||(jsonpatch={})),"undefined"!=typeof exports&&(exports.apply=jsonpatch.apply); | ||
var jsonpatch;!function(a){function b(a,c){switch(typeof a){case"undefined":case"boolean":case"string":case"number":return a===c;case"object":if(null===a)return null===c;if(d(a)){if(!d(c)||a.length!==c.length)return!1;for(var f=0,g=a.length;g>f;f++)if(!b(a[f],c[f]))return!1;return!0}var h=e(c),i=h.length;if(e(a).length!==i)return!1;for(var f=0;i>f;f++)if(!b(a[f],c[f]))return!1;return!0;default:return!1}}function c(a,b){for(var c,e=!1,i=0,j=b.length;j>i;){c=b[i],i++;for(var k=c.path.split("/"),l=a,m=1,n=k.length;;)if(d(l)){var o=parseInt(k[m],10);if(m++,m>=n){e=g[c.op].call(c,l,o,a);break}l=l[o]}else{var p=k[m];if(p){if(p&&-1!=p.indexOf("~")&&(p=p.replace(/~1/g,"/").replace(/~0/g,"~")),m++,m>=n){e=f[c.op].call(c,l,p,a);break}}else if(m++,m>=n){e=h[c.op].call(c,l,p,a);break}l=l[p]}}return e}var d,e=function(){return Object.keys?Object.keys:function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(c);return b}}(),f={add:function(a,b){return a[b]=this.value,!0},remove:function(a,b){return delete a[b],!0},replace:function(a,b){return a[b]=this.value,!0},move:function(a,b,d){var e={op:"_get",path:this.from};return c(d,[e]),c(d,[{op:"remove",path:this.from}]),c(d,[{op:"add",path:this.path,value:e.value}]),!0},copy:function(a,b,d){var e={op:"_get",path:this.from};return c(d,[e]),c(d,[{op:"add",path:this.path,value:e.value}]),!0},test:function(a,c){return b(a[c],this.value)},_get:function(a,b){this.value=a[b]}},g={add:function(a,b){return a.splice(b,0,this.value),!0},remove:function(a,b){return a.splice(b,1),!0},replace:function(a,b){return a[b]=this.value,!0},move:f.move,copy:f.copy,test:f.test,_get:f._get},h={add:function(a){for(var b in this.value)this.value.hasOwnProperty(b)&&(a[b]=this.value[b]);return!0},remove:function(a){for(var b in a)a.hasOwnProperty(b)&&f.remove.call(this,a,b);return!0},replace:function(a){return c(a,[{op:"remove",path:this.path}]),c(a,[{op:"add",path:this.path,value:this.value}]),!0},move:f.move,copy:f.copy,test:function(a){return JSON.stringify(a)===JSON.stringify(this.value)},_get:f._get};d=Array.isArray?Array.isArray:function(a){return a.push&&"number"==typeof a.length},a.apply=c}(jsonpatch||(jsonpatch={})),"undefined"!=typeof exports&&(exports.apply=jsonpatch.apply); | ||
//# sourceMappingURL=json-patch.min.js.map |
{ | ||
"name": "fast-json-patch", | ||
"version": "0.3.9", | ||
"version": "0.3.10", | ||
"description": "JSON-Patch allows you to update a JSON document by sending the changes rather than the whole document.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/Starcounter-Jack/JSON-Patch", |
@@ -176,2 +176,10 @@ JSON-Patch | ||
#### 0.3.10 (Aug 21, 2014) | ||
Bugfix: | ||
- `test` operation failed of test if properties in two were ordered differently ([#39](https://github.com/Starcounter-Jack/JSON-Patch/issues/39)) | ||
- implement `add`, `replace`, `test`, `move`, `copy` operation on object root ([#40](https://github.com/Starcounter-Jack/JSON-Patch/issues/40), [#45](https://github.com/Starcounter-Jack/JSON-Patch/issues/45)) | ||
Upgraded test suite to use Benchmark.js instead of JSLitmus | ||
#### 0.3.9 (Aug 4, 2014) | ||
@@ -178,0 +186,0 @@ |
/*! | ||
* json-patch-duplex.js 0.3.9 | ||
* json-patch-duplex.js 0.3.10 | ||
* (c) 2013 Joachim Wester | ||
@@ -9,2 +9,54 @@ * MIT license | ||
(function (jsonpatch) { | ||
var _objectKeys = (function () { | ||
if (Object.keys) | ||
return Object.keys; | ||
return function (o) { | ||
var keys = []; | ||
for (var i in o) { | ||
if (o.hasOwnProperty(i)) { | ||
keys.push(i); | ||
} | ||
} | ||
return keys; | ||
}; | ||
})(); | ||
function _equals(a, b) { | ||
switch (typeof a) { | ||
case 'undefined': | ||
case 'boolean': | ||
case 'string': | ||
case 'number': | ||
return a === b; | ||
case 'object': | ||
if (a === null) | ||
return b === null; | ||
if (_isArray(a)) { | ||
if (!_isArray(b) || a.length !== b.length) | ||
return false; | ||
for (var i = 0, l = a.length; i < l; i++) | ||
if (!_equals(a[i], b[i])) | ||
return false; | ||
return true; | ||
} | ||
var bKeys = _objectKeys(b); | ||
var bLength = bKeys.length; | ||
if (_objectKeys(a).length !== bLength) | ||
return false; | ||
for (var i = 0; i < bLength; i++) | ||
if (!_equals(a[i], b[i])) | ||
return false; | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
/* We use a Javascript hash to store each | ||
@@ -50,3 +102,3 @@ function. Each hash entry (property) uses | ||
test: function (obj, key) { | ||
return (JSON.stringify(obj[key]) === JSON.stringify(this.value)); | ||
return _equals(obj[key], this.value); | ||
}, | ||
@@ -78,2 +130,37 @@ _get: function (obj, key) { | ||
/* The operations applicable to object root. Many are the same as for the object */ | ||
var rootOps = { | ||
add: function (obj) { | ||
for (var key in this.value) { | ||
if (this.value.hasOwnProperty(key)) { | ||
obj[key] = this.value[key]; | ||
} | ||
} | ||
return true; | ||
}, | ||
remove: function (obj) { | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
objOps.remove.call(this, obj, key); | ||
} | ||
} | ||
return true; | ||
}, | ||
replace: function (obj) { | ||
apply(obj, [ | ||
{ op: "remove", path: this.path } | ||
]); | ||
apply(obj, [ | ||
{ op: "add", path: this.path, value: this.value } | ||
]); | ||
return true; | ||
}, | ||
move: objOps.move, | ||
copy: objOps.copy, | ||
test: function (obj) { | ||
return (JSON.stringify(obj) === JSON.stringify(this.value)); | ||
}, | ||
_get: objOps._get | ||
}; | ||
var observeOps = { | ||
@@ -361,17 +448,2 @@ add: function (patches, path) { | ||
var _objectKeys; | ||
if (Object.keys) { | ||
_objectKeys = Object.keys; | ||
} else { | ||
_objectKeys = function (obj) { | ||
var keys = []; | ||
for (var o in obj) { | ||
if (obj.hasOwnProperty(o)) { | ||
keys.push(o); | ||
} | ||
} | ||
return keys; | ||
}; | ||
} | ||
// Dirty check if obj is different from mirror, generate patches and update mirror | ||
@@ -432,2 +504,3 @@ function _generate(mirror, obj, patches, path) { | ||
patch = patches[p]; | ||
p++; | ||
@@ -439,2 +512,3 @@ // Find the object | ||
var len = keys.length; | ||
while (true) { | ||
@@ -451,8 +525,16 @@ if (_isArray(obj)) { | ||
var key = keys[t]; | ||
if (key.indexOf('~') != -1) | ||
key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars | ||
t++; | ||
if (t >= len) { | ||
result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
if (key) { | ||
if (key && key.indexOf('~') != -1) | ||
key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars | ||
t++; | ||
if (t >= len) { | ||
result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
} | ||
} else { | ||
t++; | ||
if (t >= len) { | ||
result = rootOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
} | ||
} | ||
@@ -462,3 +544,2 @@ obj = obj[key]; | ||
} | ||
p++; | ||
} | ||
@@ -465,0 +546,0 @@ return result; |
/*! | ||
* json-patch-duplex.js 0.3.9 | ||
* json-patch-duplex.js 0.3.10 | ||
* (c) 2013 Joachim Wester | ||
@@ -15,2 +15,54 @@ * MIT license | ||
var _objectKeys = (function() { | ||
if (Object.keys) | ||
return Object.keys; | ||
return function (o) { //IE8 | ||
var keys = []; | ||
for (var i in o) { | ||
if (o.hasOwnProperty(i)) { | ||
keys.push(i); | ||
} | ||
} | ||
return keys; | ||
} | ||
})(); | ||
function _equals(a, b) { | ||
switch (typeof a) { | ||
case 'undefined': //backward compatibility, but really I think we should return false | ||
case 'boolean': | ||
case 'string': | ||
case 'number': | ||
return a === b; | ||
case 'object': | ||
if (a === null) | ||
return b === null; | ||
if (_isArray(a)) { | ||
if (!_isArray(b) || a.length !== b.length) | ||
return false; | ||
for (var i = 0, l = a.length; i < l; i++) | ||
if (!_equals(a[i], b[i])) return false; | ||
return true; | ||
} | ||
var bKeys = _objectKeys(b); | ||
var bLength = bKeys.length; | ||
if (_objectKeys(a).length !== bLength) | ||
return false; | ||
for (var i = 0; i < bLength; i++) | ||
if (!_equals(a[i], b[i])) return false; | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
/* We use a Javascript hash to store each | ||
@@ -57,3 +109,3 @@ function. Each hash entry (property) uses | ||
test: function (obj, key) { | ||
return(JSON.stringify(obj[key]) === JSON.stringify(this.value)); | ||
return _equals(obj[key], this.value); | ||
}, | ||
@@ -85,2 +137,37 @@ _get: function (obj, key) { | ||
/* The operations applicable to object root. Many are the same as for the object */ | ||
var rootOps = { | ||
add: function (obj) { | ||
for (var key in this.value) { | ||
if (this.value.hasOwnProperty(key)) { | ||
obj[key] = this.value[key]; | ||
} | ||
} | ||
return true; | ||
}, | ||
remove: function (obj) { | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
objOps.remove.call(this, obj, key); | ||
} | ||
} | ||
return true; | ||
}, | ||
replace: function (obj) { | ||
apply(obj, [ | ||
{op: "remove", path: this.path} | ||
]); | ||
apply(obj, [ | ||
{op: "add", path: this.path, value: this.value} | ||
]); | ||
return true; | ||
}, | ||
move: objOps.move, | ||
copy: objOps.copy, | ||
test: function (obj) { | ||
return (JSON.stringify(obj) === JSON.stringify(this.value)); | ||
}, | ||
_get: objOps._get | ||
}; | ||
var observeOps = { | ||
@@ -380,18 +467,2 @@ add: function (patches:any[], path) { | ||
var _objectKeys; | ||
if (Object.keys) { //standards | ||
_objectKeys = Object.keys; | ||
} | ||
else { //IE8 shim | ||
_objectKeys = function (obj) { | ||
var keys = []; | ||
for (var o in obj) { | ||
if (obj.hasOwnProperty(o)) { | ||
keys.push(o); | ||
} | ||
} | ||
return keys; | ||
} | ||
} | ||
// Dirty check if obj is different from mirror, generate patches and update mirror | ||
@@ -455,7 +526,8 @@ function _generate(mirror, obj, patches, path) { | ||
var result = false | ||
, p = 0 | ||
, plen = patches.length | ||
, patch; | ||
, p = 0 | ||
, plen = patches.length | ||
, patch; | ||
while (p < plen) { | ||
patch = patches[p]; | ||
p++; | ||
// Find the object | ||
@@ -466,2 +538,3 @@ var keys = patch.path.split('/'); | ||
var len = keys.length; | ||
while (true) { | ||
@@ -479,13 +552,21 @@ if (_isArray(obj)) { | ||
var key = keys[t]; | ||
if (key.indexOf('~') != -1) | ||
key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars | ||
t++; | ||
if (t >= len) { | ||
result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
if(key) { | ||
if (key && key.indexOf('~') != -1) | ||
key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars | ||
t++; | ||
if (t >= len) { | ||
result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
} | ||
} | ||
else { //is root | ||
t++; | ||
if (t >= len) { | ||
result = rootOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
} | ||
} | ||
obj = obj[key]; | ||
} | ||
} | ||
p++; | ||
} | ||
@@ -492,0 +573,0 @@ return result; |
/*! | ||
* json-patch-duplex.js 0.3.9 | ||
* json-patch-duplex.js 0.3.10 | ||
* (c) 2013 Joachim Wester | ||
@@ -8,2 +8,54 @@ * MIT license | ||
(function (jsonpatch) { | ||
var _objectKeys = (function () { | ||
if (Object.keys) | ||
return Object.keys; | ||
return function (o) { | ||
var keys = []; | ||
for (var i in o) { | ||
if (o.hasOwnProperty(i)) { | ||
keys.push(i); | ||
} | ||
} | ||
return keys; | ||
}; | ||
})(); | ||
function _equals(a, b) { | ||
switch (typeof a) { | ||
case 'undefined': | ||
case 'boolean': | ||
case 'string': | ||
case 'number': | ||
return a === b; | ||
case 'object': | ||
if (a === null) | ||
return b === null; | ||
if (_isArray(a)) { | ||
if (!_isArray(b) || a.length !== b.length) | ||
return false; | ||
for (var i = 0, l = a.length; i < l; i++) | ||
if (!_equals(a[i], b[i])) | ||
return false; | ||
return true; | ||
} | ||
var bKeys = _objectKeys(b); | ||
var bLength = bKeys.length; | ||
if (_objectKeys(a).length !== bLength) | ||
return false; | ||
for (var i = 0; i < bLength; i++) | ||
if (!_equals(a[i], b[i])) | ||
return false; | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
var objOps = { | ||
@@ -42,3 +94,3 @@ add: function (obj, key) { | ||
test: function (obj, key) { | ||
return (JSON.stringify(obj[key]) === JSON.stringify(this.value)); | ||
return _equals(obj[key], this.value); | ||
}, | ||
@@ -69,2 +121,37 @@ _get: function (obj, key) { | ||
/* The operations applicable to object root. Many are the same as for the object */ | ||
var rootOps = { | ||
add: function (obj) { | ||
for (var key in this.value) { | ||
if (this.value.hasOwnProperty(key)) { | ||
obj[key] = this.value[key]; | ||
} | ||
} | ||
return true; | ||
}, | ||
remove: function (obj) { | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
objOps.remove.call(this, obj, key); | ||
} | ||
} | ||
return true; | ||
}, | ||
replace: function (obj) { | ||
apply(obj, [ | ||
{ op: "remove", path: this.path } | ||
]); | ||
apply(obj, [ | ||
{ op: "add", path: this.path, value: this.value } | ||
]); | ||
return true; | ||
}, | ||
move: objOps.move, | ||
copy: objOps.copy, | ||
test: function (obj) { | ||
return (JSON.stringify(obj) === JSON.stringify(this.value)); | ||
}, | ||
_get: objOps._get | ||
}; | ||
var _isArray; | ||
@@ -84,2 +171,3 @@ if (Array.isArray) { | ||
patch = patches[p]; | ||
p++; | ||
@@ -91,2 +179,3 @@ // Find the object | ||
var len = keys.length; | ||
while (true) { | ||
@@ -103,8 +192,16 @@ if (_isArray(obj)) { | ||
var key = keys[t]; | ||
if (key.indexOf('~') != -1) | ||
key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars | ||
t++; | ||
if (t >= len) { | ||
result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
if (key) { | ||
if (key && key.indexOf('~') != -1) | ||
key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars | ||
t++; | ||
if (t >= len) { | ||
result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
} | ||
} else { | ||
t++; | ||
if (t >= len) { | ||
result = rootOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
} | ||
} | ||
@@ -114,3 +211,2 @@ obj = obj[key]; | ||
} | ||
p++; | ||
} | ||
@@ -125,1 +221,2 @@ return result; | ||
} | ||
//# sourceMappingURL=json-patch.js.map |
/*! | ||
* json-patch-duplex.js 0.3.9 | ||
* json-patch-duplex.js 0.3.10 | ||
* (c) 2013 Joachim Wester | ||
@@ -9,2 +9,54 @@ * MIT license | ||
var _objectKeys = (function() { | ||
if (Object.keys) | ||
return Object.keys; | ||
return function (o) { //IE8 | ||
var keys = []; | ||
for (var i in o) { | ||
if (o.hasOwnProperty(i)) { | ||
keys.push(i); | ||
} | ||
} | ||
return keys; | ||
} | ||
})(); | ||
function _equals(a, b) { | ||
switch (typeof a) { | ||
case 'undefined': //backward compatibility, but really I think we should return false | ||
case 'boolean': | ||
case 'string': | ||
case 'number': | ||
return a === b; | ||
case 'object': | ||
if (a === null) | ||
return b === null; | ||
if (_isArray(a)) { | ||
if (!_isArray(b) || a.length !== b.length) | ||
return false; | ||
for (var i = 0, l = a.length; i < l; i++) | ||
if (!_equals(a[i], b[i])) return false; | ||
return true; | ||
} | ||
var bKeys = _objectKeys(b); | ||
var bLength = bKeys.length; | ||
if (_objectKeys(a).length !== bLength) | ||
return false; | ||
for (var i = 0; i < bLength; i++) | ||
if (!_equals(a[i], b[i])) return false; | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
var objOps = { | ||
@@ -43,3 +95,3 @@ add: function (obj, key) { | ||
test: function (obj, key) { | ||
return(JSON.stringify(obj[key]) === JSON.stringify(this.value)); | ||
return _equals(obj[key], this.value); | ||
}, | ||
@@ -70,2 +122,37 @@ _get: function (obj, key) { | ||
/* The operations applicable to object root. Many are the same as for the object */ | ||
var rootOps = { | ||
add: function (obj) { | ||
for (var key in this.value) { | ||
if (this.value.hasOwnProperty(key)) { | ||
obj[key] = this.value[key]; | ||
} | ||
} | ||
return true; | ||
}, | ||
remove: function (obj) { | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
objOps.remove.call(this, obj, key); | ||
} | ||
} | ||
return true; | ||
}, | ||
replace: function (obj) { | ||
apply(obj, [ | ||
{op: "remove", path: this.path} | ||
]); | ||
apply(obj, [ | ||
{op: "add", path: this.path, value: this.value} | ||
]); | ||
return true; | ||
}, | ||
move: objOps.move, | ||
copy: objOps.copy, | ||
test: function (obj) { | ||
return (JSON.stringify(obj) === JSON.stringify(this.value)); | ||
}, | ||
_get: objOps._get | ||
}; | ||
var _isArray; | ||
@@ -84,7 +171,8 @@ if (Array.isArray) { //standards; http://jsperf.com/isarray-shim/4 | ||
var result = false | ||
, p = 0 | ||
, plen = patches.length | ||
, patch; | ||
, p = 0 | ||
, plen = patches.length | ||
, patch; | ||
while (p < plen) { | ||
patch = patches[p]; | ||
p++; | ||
// Find the object | ||
@@ -95,2 +183,3 @@ var keys = patch.path.split('/'); | ||
var len = keys.length; | ||
while (true) { | ||
@@ -108,13 +197,21 @@ if (_isArray(obj)) { | ||
var key = keys[t]; | ||
if (key.indexOf('~') != -1) | ||
key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars | ||
t++; | ||
if (t >= len) { | ||
result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
if(key) { | ||
if (key && key.indexOf('~') != -1) | ||
key = key.replace(/~1/g, '/').replace(/~0/g, '~'); // escape chars | ||
t++; | ||
if (t >= len) { | ||
result = objOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
} | ||
} | ||
else { //is root | ||
t++; | ||
if (t >= len) { | ||
result = rootOps[patch.op].call(patch, obj, key, tree); // Apply patch | ||
break; | ||
} | ||
} | ||
obj = obj[key]; | ||
} | ||
} | ||
p++; | ||
} | ||
@@ -121,0 +218,0 @@ return result; |
@@ -695,5 +695,5 @@ var obj, obj2, patches; | ||
// JSLitmus performance test | ||
if(typeof JSLitmus !== 'undefined') { | ||
JSLitmus.test('should Generate replace', function() { | ||
// Benchmark performance test | ||
if (typeof Benchmark !== 'undefined') { | ||
suite.add('generate operation', function () { | ||
obj = { firstName:"Albert", lastName:"Einstein", | ||
@@ -714,2 +714,2 @@ phoneNumbers:[ {number:"12345"}, {number:"45353"} ]}; | ||
}); | ||
} | ||
} |
124
src/test.js
@@ -1,2 +0,2 @@ | ||
var obj, compiled; | ||
var obj; | ||
@@ -36,3 +36,10 @@ if(typeof jsonpatch === 'undefined') { | ||
it('should apply add on root', function() { | ||
var obj = {"hello": "world"}; | ||
jsonpatch.apply(obj, [{"op":"add","path":"","value":{"hello": "universe"}}]); | ||
expect(obj).toEqual({"hello": "universe"}); | ||
}); | ||
it('should apply remove', function() { | ||
@@ -61,9 +68,35 @@ obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]}; | ||
it('should apply replace on root', function() { | ||
var obj = {"hello": "world"}; | ||
jsonpatch.apply(obj, [{"op":"replace","path":"","value":{"hello": "universe"}}]); | ||
expect(obj).toEqual({"hello": "universe"}); | ||
}); | ||
it('should apply test', function() { | ||
obj = {foo: {bar: [1, 2, 5, 4]}}; | ||
obj = { | ||
foo: {bar: [1, 2, 5, 4]}, | ||
bar: {a: 'a', b: 42, c: null, d: true} | ||
}; | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/foo', value: {bar: [1, 2, 5, 4]}}])).toBe(true); | ||
expect(!jsonpatch.apply(obj, [{op: 'test', path: '/foo', value: [1, 2]}])).toBe(true); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/foo', value: [1, 2]}])).toBe(false); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/bar', value: {d: true, b: 42, c: null, a: 'a'}}])).toBe(true); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/bar', value: obj.bar}])).toBe(true); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/bar/a', value: 'a'}])).toBe(true); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/bar/b', value: 42}])).toBe(true); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/bar/c', value: null}])).toBe(true); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/bar/d', value: true}])).toBe(true); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/bar/d', value: false}])).toBe(false); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '/bar', value: {d: true, b: 42, c: null, a: 'a', foo: 'bar'}}])).toBe(false); | ||
}); | ||
it('should apply test on root', function() { | ||
var obj = {"hello": "world"}; | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '', value: {"hello": "world"}}])).toBe(true); | ||
expect(jsonpatch.apply(obj, [{op: 'test', path: '', value: {"hello": "universe"}}])).toBe(false); | ||
}); | ||
it('should apply move', function() { | ||
@@ -79,2 +112,7 @@ obj = {foo: 1, baz: [{qux: 'hello'}]}; | ||
it('should apply move on root', function() { //investigate if this test is right (https://github.com/Starcounter-Jack/JSON-Patch/issues/40) | ||
var obj = {"hello": "world", "location": {"city": "Vancouver"}}; | ||
jsonpatch.apply(obj, [{op: 'move', from: '/location', path: ''}]); | ||
expect(obj).toEqual({"hello": "world", "city": "Vancouver"}); | ||
}); | ||
@@ -90,35 +128,65 @@ it('should apply copy', function() { | ||
}); | ||
}); | ||
// JSLitmus performance test | ||
if(typeof JSLitmus !== 'undefined') { | ||
JSLitmus.test('should Add Operation', function() { | ||
obj = {foo: 1, baz: [{qux: 'hello'}]}; | ||
jsonpatch.apply(obj, [{op: 'add', path: '/bar', value: [1, 2, 3, 4]}]); | ||
it('should apply copy on root', function() { | ||
var obj = {"hello": "world", "location": {"city": "Vancouver"}}; | ||
jsonpatch.apply(obj, [{op: 'copy', from: '/location', path: ''}]); | ||
expect(obj).toEqual({"hello": "world", "location": {"city": "Vancouver"}, "city": "Vancouver"}); | ||
}); | ||
}); | ||
JSLitmus.test('should Remove Operation', function() { | ||
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]}; | ||
jsonpatch.apply(obj, [{op: 'remove', path: '/bar'}]); | ||
// Benchmark performance test | ||
if (typeof Benchmark !== 'undefined') { | ||
var suite = new Benchmark.Suite; | ||
suite.add('add operation', function () { | ||
obj = {foo: 1, baz: [ | ||
{qux: 'hello'} | ||
]}; | ||
jsonpatch.apply(obj, [ | ||
{op: 'add', path: '/bar', value: [1, 2, 3, 4]} | ||
]); | ||
}); | ||
JSLitmus.test('should Replace Operation', function() { | ||
obj = {foo: 1, baz: [{qux: 'hello'}]}; | ||
jsonpatch.apply(obj, [{op: 'replace', path: '/foo', value: [1, 2, 3, 4]}]); | ||
suite.add('remove operation', function () { | ||
obj = {foo: 1, baz: [ | ||
{qux: 'hello'} | ||
], bar: [1, 2, 3, 4]}; | ||
jsonpatch.apply(obj, [ | ||
{op: 'remove', path: '/bar'} | ||
]); | ||
}); | ||
JSLitmus.test('should Move Operation', function() { | ||
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]}; | ||
jsonpatch.apply(obj, [{op: 'move', from: '/baz/0', path: '/bar/0'}]); | ||
suite.add('replace operation', function () { | ||
obj = {foo: 1, baz: [ | ||
{qux: 'hello'} | ||
]}; | ||
jsonpatch.apply(obj, [ | ||
{op: 'replace', path: '/foo', value: [1, 2, 3, 4]} | ||
]); | ||
}); | ||
JSLitmus.test('should Copy Operation', function() { | ||
obj = {foo: 1, baz: [{qux: 'hello'}], bar: [1, 2, 3, 4]}; | ||
jsonpatch.apply(obj, [{op: 'copy', from: '/baz/0', path: '/bar/0'}]); | ||
suite.add('move operation', function () { | ||
obj = {foo: 1, baz: [ | ||
{qux: 'hello'} | ||
], bar: [1, 2, 3, 4]}; | ||
jsonpatch.apply(obj, [ | ||
{op: 'move', from: '/baz/0', path: '/bar/0'} | ||
]); | ||
}); | ||
suite.add('copy operation', function () { | ||
obj = {foo: 1, baz: [ | ||
{qux: 'hello'} | ||
], bar: [1, 2, 3, 4]}; | ||
jsonpatch.apply(obj, [ | ||
{op: 'copy', from: '/baz/0', path: '/bar/0'} | ||
]); | ||
}); | ||
suite.add('test operation', function () { | ||
obj = {foo: 1, baz: [ | ||
{qux: 'hello'} | ||
]}; | ||
jsonpatch.apply(obj, [ | ||
{op: 'test', path: '/baz', value: [ | ||
{qux: 'hello'} | ||
]} | ||
]); | ||
}); | ||
JSLitmus.test('should Test Operation', function() { | ||
obj = {foo: 1, baz: [{qux: 'hello'}]}; | ||
jsonpatch.apply(obj, [{op: 'test', path: '/baz', value: [{qux: 'hello'}]}]); | ||
}); | ||
benchmarkReporter(suite); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
419592
41
7730
223
3
1