jsonpatcherproxy
Advanced tools
Comparing version
@@ -76,3 +76,3 @@ module.exports = | ||
* https://github.com/PuppetJS/JSONPatcherProxy | ||
* JSONPatcherProxy version: 0.0.2 | ||
* JSONPatcherProxy version: 0.0.3 | ||
* (c) 2017 Starcounter | ||
@@ -236,3 +236,3 @@ * MIT license | ||
} | ||
return this.generateProxyAtPath(root, ""); | ||
return this.generateProxyAtPath(root, path); | ||
}; | ||
@@ -239,0 +239,0 @@ //this function is for aesthetic purposes |
var JSONPatcherProxy=function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.i=function(e){return e},t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t){/*! | ||
* https://github.com/PuppetJS/JSONPatcherProxy | ||
* JSONPatcherProxy version: 0.0.2 | ||
* JSONPatcherProxy version: 0.0.3 | ||
* (c) 2017 Starcounter | ||
* MIT license | ||
*/ | ||
var r=function(){function e(e){this.originalObject=e,this.cachedProxy=null,this.isRecording=!1,this.userCallback;var t=this;this.switchObserverOn=function(){t.defaultCallback=function(e){t.isRecording&&t.patches.push(e),t.userCallback&&t.userCallback(e)}},this.switchObserverOff=function(){t.defaultCallback=function(){}}}return e.deepClone=function(e){switch(typeof e){case"object":return JSON.parse(JSON.stringify(e));case"undefined":return null;default:return e}},e.escapePathComponent=function(e){return e.indexOf("/")===-1&&e.indexOf("~")===-1?e:e.replace(/~/g,"~0").replace(/\//g,"~1")},e.prototype.generateProxyAtPath=function(t,r){if(!t)return t;var n=this,o=new Proxy(t,{get:function(e,t,r){return"_isProxified"===t.toString()||Reflect.get(e,t,r)},set:function(t,o,a){var i=r+"/"+e.escapePathComponent(o.toString());if(a&&"object"==typeof a&&a._isProxified!==!0&&(a=n._proxifyObjectTreeRecursively(a,i)),"undefined"==typeof a){if(t.hasOwnProperty(o))return Array.isArray(t)?n.defaultCallback({op:"replace",path:i,value:null}):n.defaultCallback({op:"remove",path:i}),Reflect.set(t,o,a);if(!Array.isArray(t))return Reflect.set(t,o,a)}return Array.isArray(t)&&!Number.isInteger(+o.toString())?Reflect.set(t,o,a):t.hasOwnProperty(o)?"undefined"==typeof t[o]?(Array.isArray(t)?n.defaultCallback({op:"replace",path:i,value:a}):n.defaultCallback({op:"add",path:i,value:a}),Reflect.set(t,o,a)):(n.defaultCallback({op:"replace",path:i,value:a}),Reflect.set(t,o,a)):(n.defaultCallback({op:"add",path:i,value:a}),Reflect.set(t,o,a))},deleteProperty:function(t,o){return"undefined"!=typeof t[o]&&n.defaultCallback({op:"remove",path:r+"/"+e.escapePathComponent(o.toString())}),Reflect.deleteProperty(t,o)}});return o},e.prototype._proxifyObjectTreeRecursively=function(t,r){for(var n in t)if(t.hasOwnProperty(n)&&"object"==typeof t[n]){var o=r+"/"+e.escapePathComponent(n);t[n]=this.generateProxyAtPath(t[n],o),this._proxifyObjectTreeRecursively(t[n],o)}return this.generateProxyAtPath(t,"")},e.prototype.proxifyObjectTree=function(e){this.switchObserverOff();var t=this._proxifyObjectTreeRecursively(e,"");return this.switchObserverOn(),t},e.prototype.observe=function(t,r){if(!t&&!r)throw new Error("You need to either record changes or pass a callback");return this.isRecording=t,r&&(this.userCallback=r),t&&(this.patches=[]),this.cachedProxy=this.proxifyObjectTree(e.deepClone(this.originalObject))},e.prototype.generate=function(){if(!this.isRecording)throw new Error("You should set record to true to get patches later");return this.patches.splice(0,this.patches.length)},e.prototype.unobserve=function(){return e.deepClone(this.cachedProxy)},e}();e.exports=r,e.exports.default=r}]); | ||
var r=function(){function e(e){this.originalObject=e,this.cachedProxy=null,this.isRecording=!1,this.userCallback;var t=this;this.switchObserverOn=function(){t.defaultCallback=function(e){t.isRecording&&t.patches.push(e),t.userCallback&&t.userCallback(e)}},this.switchObserverOff=function(){t.defaultCallback=function(){}}}return e.deepClone=function(e){switch(typeof e){case"object":return JSON.parse(JSON.stringify(e));case"undefined":return null;default:return e}},e.escapePathComponent=function(e){return e.indexOf("/")===-1&&e.indexOf("~")===-1?e:e.replace(/~/g,"~0").replace(/\//g,"~1")},e.prototype.generateProxyAtPath=function(t,r){if(!t)return t;var n=this,o=new Proxy(t,{get:function(e,t,r){return"_isProxified"===t.toString()||Reflect.get(e,t,r)},set:function(t,o,a){var i=r+"/"+e.escapePathComponent(o.toString());if(a&&"object"==typeof a&&a._isProxified!==!0&&(a=n._proxifyObjectTreeRecursively(a,i)),"undefined"==typeof a){if(t.hasOwnProperty(o))return Array.isArray(t)?n.defaultCallback({op:"replace",path:i,value:null}):n.defaultCallback({op:"remove",path:i}),Reflect.set(t,o,a);if(!Array.isArray(t))return Reflect.set(t,o,a)}return Array.isArray(t)&&!Number.isInteger(+o.toString())?Reflect.set(t,o,a):t.hasOwnProperty(o)?"undefined"==typeof t[o]?(Array.isArray(t)?n.defaultCallback({op:"replace",path:i,value:a}):n.defaultCallback({op:"add",path:i,value:a}),Reflect.set(t,o,a)):(n.defaultCallback({op:"replace",path:i,value:a}),Reflect.set(t,o,a)):(n.defaultCallback({op:"add",path:i,value:a}),Reflect.set(t,o,a))},deleteProperty:function(t,o){return"undefined"!=typeof t[o]&&n.defaultCallback({op:"remove",path:r+"/"+e.escapePathComponent(o.toString())}),Reflect.deleteProperty(t,o)}});return o},e.prototype._proxifyObjectTreeRecursively=function(t,r){for(var n in t)if(t.hasOwnProperty(n)&&"object"==typeof t[n]){var o=r+"/"+e.escapePathComponent(n);t[n]=this.generateProxyAtPath(t[n],o),this._proxifyObjectTreeRecursively(t[n],o)}return this.generateProxyAtPath(t,r)},e.prototype.proxifyObjectTree=function(e){this.switchObserverOff();var t=this._proxifyObjectTreeRecursively(e,"");return this.switchObserverOn(),t},e.prototype.observe=function(t,r){if(!t&&!r)throw new Error("You need to either record changes or pass a callback");return this.isRecording=t,r&&(this.userCallback=r),t&&(this.patches=[]),this.cachedProxy=this.proxifyObjectTree(e.deepClone(this.originalObject))},e.prototype.generate=function(){if(!this.isRecording)throw new Error("You should set record to true to get patches later");return this.patches.splice(0,this.patches.length)},e.prototype.unobserve=function(){return e.deepClone(this.cachedProxy)},e}();e.exports=r,e.exports.default=r}]); |
/*! | ||
* https://github.com/PuppetJS/JSONPatcherProxy | ||
* JSONPatcherProxy version: 0.0.2 | ||
* JSONPatcherProxy version: 0.0.3 | ||
* (c) 2017 Starcounter | ||
@@ -5,0 +5,0 @@ * MIT license |
{ | ||
"name": "jsonpatcherproxy", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.", | ||
@@ -23,2 +23,3 @@ "main": "dist/jsonpatcherproxy.js", | ||
"benchmark": "^2.1.3", | ||
"chalk": "^1.1.3", | ||
"fast-json-patch": "^1.1.6", | ||
@@ -25,0 +26,0 @@ "jasmine": "^2.5.3", |
/*! | ||
* https://github.com/PuppetJS/JSONPatcherProxy | ||
* JSONPatcherProxy version: 0.0.2 | ||
* JSONPatcherProxy version: 0.0.3 | ||
* (c) 2017 Starcounter | ||
@@ -162,3 +162,3 @@ * MIT license | ||
} | ||
return this.generateProxyAtPath(root, ""); | ||
return this.generateProxyAtPath(root, path); | ||
}; | ||
@@ -165,0 +165,0 @@ //this function is for aesthetic purposes |
@@ -373,2 +373,3 @@ if (typeof window === 'undefined') { | ||
patches = jsonPatcherProxy.generate(); | ||
expect(patches).toReallyEqual([{ | ||
@@ -550,19 +551,2 @@ op: 'replace', | ||
/*it('should not generate the same patch twice (move)', function() { //"move" is not implemented yet in jsonpatch.generate | ||
var obj = { lastName: {str: "Einstein"} }; | ||
var jsonPatcherProxy = new JSONPatcherProxy(obj); | ||
var observedObj = jsonPatcherProxy.observe(true); | ||
obj.lastName2 = obj.lastName; | ||
delete obj.lastName; | ||
var patches = jsonPatcherProxy.generate(); | ||
expect(patches).toReallyEqual([ | ||
{ op: 'move', from: '/lastName', to: '/lastName2' } | ||
]); | ||
var patches = jsonPatcherProxy.generate(); | ||
expect(patches).toReallyEqual([]); | ||
});*/ | ||
describe("undefined - JS to JSON projection", function () { | ||
@@ -569,0 +553,0 @@ it('when value is set to `undefined`, should generate remove (undefined is JSON.stringified to no value)', function () { |
348178
-0.19%7
16.67%8269
-0.16%