immutable-json-patch
Version:
Immutable JSON patch with support for reverting operations
1 lines • 6.21 kB
JavaScript
((e,t)=>{"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).immutableJSONPatch={})})(this,function(e){function u(e){return Array.isArray(e)}function l(e){return null!==e&&"object"==typeof e&&(void 0===e.constructor||"Object"===e.constructor.name)}function a(e,t){return e===t}function r(e){return e.slice(0,e.length-1)}function o(e){return"object"==typeof e&&null!==e}function s(r){if(u(r)){let t=r.slice();return Object.getOwnPropertySymbols(r).forEach(e=>{t[e]=r[e]}),t}if(l(r)){let t={...r};return Object.getOwnPropertySymbols(r).forEach(e=>{t[e]=r[e]}),t}return r}function i(e,t,r){return e[t]===r||((e=s(e))[t]=r),e}function h(e,t){let r=e,n=0;for(;n<t.length;)r=l(r)?r[t[n]]:u(r)?r[Number.parseInt(t[n])]:void 0,n++;return r}function d(e,t,r){var n=3<arguments.length&&void 0!==arguments[3]&&arguments[3];if(0===t.length)return r;var o=t[0],t=d(e?e[o]:void 0,t.slice(1),r,n);if(l(e)||u(e))return i(e,o,t);if(n)return(r=f.test(o)?[]:{})[o]=t,r;throw new Error("Path does not exist")}let f=/^\d+$/;function p(e,t,r){if(0===t.length)return r(e);var n;if(o(e))return i(e,n=t[0],p(e[n],t.slice(1),r));throw new Error("Path doesn't exist")}function v(t,r){if(0===r.length)return t;if(!o(t))throw new Error("Path does not exist");if(1===r.length){let e=r[0];return e in t?(u(n=s(t))&&n.splice(Number.parseInt(e),1),l(n)&&delete n[e],n):t}let e=r[0];var n=v(t[e],r.slice(1));return i(t,e,n)}function g(e,t,r){let n=t.slice(0,t.length-1),o=t[t.length-1];return p(e,n,e=>{if(Array.isArray(e))return(e=s(e)).splice(Number.parseInt(o),0,r),e;throw new TypeError("Array expected at path "+JSON.stringify(n))})}function y(e,t){return void 0!==e&&(0===t.length||null!==e&&y(e[t[0]],t.slice(1)))}function n(e){e=e.split("/");return e.shift(),e.map(e=>e.replace(/~1/g,"/").replace(/~0/g,"~"))}function m(e){return e.map(c).join("")}function c(e){return"/"+String(e).replace(/~/g,"~0").replace(/\//g,"~1")}function O(e,r,n){let o=e;for(let t=0;t<r.length;t++){i=void 0;var i=r[t];if(!["add","remove","replace","copy","move","test"].includes(i.op))throw new Error("Unknown JSONPatch op "+JSON.stringify(i.op));if("string"!=typeof i.path)throw new Error('Required property "path" missing or not a string in operation '+JSON.stringify(i));if(("copy"===i.op||"move"===i.op)&&"string"!=typeof i.from)throw new Error('Required property "from" missing or not a string in operation '+JSON.stringify(i));let e=r[t];if(n?.before){i=n.before(o,e);if(void 0!==i){if(void 0!==i.document&&(o=i.document),void 0!==i.json)throw new Error('Deprecation warning: returned object property ".json" has been renamed to ".document"');void 0!==i.operation&&(e=i.operation)}}var i=o,f=b(o,e.path);if("add"===e.op)o=((e,t,r)=>(w(e,t)?g:d)(e,t,r))(o,f,e.value);else if("remove"===e.op)o=v(o,f);else if("replace"===e.op)o=(u=o,l=f,s=e.value,y(u,l)?d(u,l,s):u);else if("copy"===e.op)o=((e,t,r)=>(r=h(e,r),(w(e,t)?g:d)(e,t,r)))(o,f,S(e.from));else if("move"===e.op)o=(l=o,s=f,u=S(e.from),a=void 0,a=h(l,u),(w(l=v(l,u),s)?g:d)(l,s,a));else{if("test"!==e.op)throw new Error("Unknown JSONPatch operation "+JSON.stringify(e));c=p=a=void 0;var a=o,p=f,c=e.value;if(void 0===c)throw new Error(`Test failed: no value provided (path: "${m(p)}")`);if(!y(a,p))throw new Error(`Test failed: path not found (path: "${m(p)}")`);if(!((e,t)=>JSON.stringify(e)===JSON.stringify(t))(a=h(a,p),c))throw new Error(`Test failed, value differs (path: "${m(p)}")`)}n?.after&&void 0!==(f=n.after(o,e,i))&&(o=f)}var a,u,l,s;return o}function w(e,t){if(0!==t.length)return e=h(e,r(t)),Array.isArray(e)}function b(e,t){return e=e,"-"!==(t=n(t))[t.length-1]?t:(t=h(e,e=r(t)),e.concat(t.length))}function S(e){return n(e)}function N(e,t){return y(e,t)?[{op:"replace",path:m(t),value:h(e,t)}]:[]}function J(e,t){return[{op:"add",path:m(t),value:h(e,t)}]}function P(e,t){return w(e,t)||!y(e,t)?[{op:"remove",path:m(t)}]:N(e,t)}e.appendToJSONPointer=function(e,t){return e+c(t)},e.compileJSONPointer=m,e.compileJSONPointerProp=c,e.deleteIn=v,e.existsIn=y,e.getIn=h,e.immutableJSONPatch=O,e.insertAt=g,e.isJSONArray=u,e.isJSONObject=l,e.isJSONPatchAdd=function(e){return!(!e||"object"!=typeof e)&&"add"===e.op},e.isJSONPatchCopy=function(e){return!(!e||"object"!=typeof e)&&"copy"===e.op},e.isJSONPatchMove=function(e){return!(!e||"object"!=typeof e)&&"move"===e.op},e.isJSONPatchOperation=function(e){return!(!e||"object"!=typeof e)&&"string"==typeof e.op},e.isJSONPatchRemove=function(e){return!(!e||"object"!=typeof e)&&"remove"===e.op},e.isJSONPatchReplace=function(e){return!(!e||"object"!=typeof e)&&"replace"===e.op},e.isJSONPatchTest=function(e){return!(!e||"object"!=typeof e)&&"test"===e.op},e.parseFrom=S,e.parseJSONPointer=n,e.parsePath=b,e.revertJSONPatch=function(e,t,i){let f=[];return O(e,t,{before:(e,t)=>{let r;var n=b(e,t.path);if("add"===t.op)r=P(e,n);else if("remove"===t.op)r=J(e,n);else if("replace"===t.op)r=N(e,n);else if("copy"===t.op)r=P(e,n);else if("move"===t.op)r=((e,t,r)=>t.length<r.length&&function(t,r,e){var n=2<arguments.length&&void 0!==e?e:a;if(!(t.length<r.length)){for(let e=0;e<r.length;e++)if(!n(t[e],r[e]))return;return 1}}(r,t)?[{op:"replace",path:m(t),value:e}]:(r={op:"move",from:m(t),path:m(r)},w(e,t)||!y(e,t)?[r]:[r,...J(e,t)]))(e,n,S(t.from));else{if("test"!==t.op)throw new Error("Unknown JSONPatch operation "+JSON.stringify(t));r=[]}let o;if(i?.before){n=i.before(e,t,r);if(n?.revertOperations&&(r=n.revertOperations),n?.document&&(o=n.document),n?.json)throw new Error('Deprecation warning: returned object property ".json" has been renamed to ".document"')}if(f=r.concat(f),void 0!==o)return{document:o}}}),f},e.setIn=d,e.startsWithJSONPointer=function(e,t){return e.startsWith(t)&&(e.length===t.length||"/"===e[t.length])},e.transform=function r(e,n){var t,o,i=2<arguments.length&&void 0!==arguments[2]?arguments[2]:[],f=n(e,i);if(u(f)){let t;for(let e=0;e<f.length;e++){var a=f[e],p=r(a,n,i.concat(String(e)));p!==a&&((t=t||s(f))[e]=p)}return t||f}if(l(f)){let e;for(var c in f)Object.hasOwnProperty.call(f,c)&&(o=r(t=f[c],n,i.concat(c)))!==t&&((e=e||s(f))[c]=o);return e||f}return f},e.updateIn=p});