immutable-json-patch
Version:
Immutable JSON patch with support for reverting operations
1 lines • 6.29 kB
JavaScript
!function(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){"use strict";function p(e){return Array.isArray(e)}function s(e){return null!==e&&"object"==typeof e&&(void 0===e.constructor||"Object"===e.constructor.name)}function c(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 l(t){if(p(t)){const r=t.slice();return Object.getOwnPropertySymbols(t).forEach(e=>{r[e]=t[e]}),r}if(s(t)){const n={...t};return Object.getOwnPropertySymbols(t).forEach(e=>{n[e]=t[e]}),n}return t}function i(e,t,r){return e[t]===r?e:((e=l(e))[t]=r,e)}function h(e,t){let r=e,n=0;for(;n<t.length;)r=s(r)?r[t[n]]:p(r)?r[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(s(e)||p(e))return i(e,o,t);if(n)return(r=f.test(o)?[]:{})[o]=t,r;throw new Error("Path does not exist")}const f=/^\d+$/;function a(e,t,r){if(0===t.length)return r(e);var n;if(o(e))return i(e,n=t[0],a(e[n],t.slice(1),r));throw new Error("Path doesn't exist")}function v(e,t){if(0===t.length)return e;if(!o(e))throw new Error("Path does not exist");if(1===t.length){const r=t[0];return r in e?(p(n=l(e))&&n.splice(parseInt(r),1),s(n)&&delete n[r],n):e}const r=t[0];var n=v(e[r],t.slice(1));return i(e,r,n)}function g(e,t,r){const n=t.slice(0,t.length-1),o=t[t.length-1];return a(e,n,e=>{if(Array.isArray(e))return(e=l(e)).splice(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(u).join("")}function u(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&&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=S(o,e.path);if("add"===e.op)o=(p=o,s=f,l=e.value,(w(p,s)?g:d)(p,s,l));else if("remove"===e.op)o=v(o,f);else if("replace"===e.op)o=(p=o,s=e.value,d(p,f,s));else if("copy"===e.op)o=function(e,t,r){const n=h(e,r);{if(w(e,t))return g(e,t,n);{const n=h(e,r);return d(e,t,n)}}}(o,f,b(e.from));else if("move"===e.op)o=(l=o,c=f,a=b(e.from),u=void 0,u=h(l,a),(w(l=v(l,a),c)?g:d)(l,c,u));else{if("test"!==e.op)throw new Error("Unknown JSONPatch operation "+JSON.stringify(e));u=c=a=void 0;a=o,c=f,u=e.value;if(void 0===u)throw new Error(`Test failed: no value provided (path: "${m(c)}")`);if(!y(a,c))throw new Error(`Test failed: path not found (path: "${m(c)}")`);if(!function(e,t){return JSON.stringify(e)===JSON.stringify(t)}(a=h(a,c),u))throw new Error(`Test failed, value differs (path: "${m(c)}")`)}n&&n.after&&void 0!==(f=n.after(o,e,i))&&(o=f)}var c,a,u,p,s,l;return o}function w(e,t){if(0!==t.length)return e=h(e,r(t)),Array.isArray(e)}function S(e,t){return e=e,"-"!==(t=n(t))[t.length-1]?t:(t=h(e,e=r(t)),e.concat(t.length))}function b(e){return n(e)}function J(e,t){return[{op:"replace",path:m(t),value:h(e,t)}]}function N(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)}]:J(e,t)}e.appendToJSONPointer=function(e,t){return e+u(t)},e.compileJSONPointer=m,e.compileJSONPointerProp=u,e.deleteIn=v,e.existsIn=y,e.getIn=h,e.immutableJSONPatch=O,e.insertAt=g,e.isJSONArray=p,e.isJSONObject=s,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=b,e.parseJSONPointer=n,e.parsePath=S,e.revertJSONPatch=function(e,t,i){let f=[];return O(e,t,{before:(e,t)=>{let r;var n=S(e,t.path);if("add"===t.op)r=P(e,n);else if("remove"===t.op)r=N(e,n);else if("replace"===t.op)r=J(e,n);else if("copy"===t.op)r=P(e,n);else if("move"===t.op)r=function(e,t,r){if(t.length<r.length&&function(t,r,e){var n=2<arguments.length&&void 0!==e?e:c;if(!(t.length<r.length)){for(let e=0;e<r.length;e++)if(!n(t[e],r[e]))return;return 1}}(r,t))return[{op:"replace",path:m(t),value:e}];r={op:"move",from:m(t),path:m(r)};return!w(e,t)&&y(e,t)?[r,...N(e,t)]:[r]}(e,n,b(t.from));else{if("test"!==t.op)throw new Error("Unknown JSONPatch operation "+JSON.stringify(t));r=[]}let o;if(i&&i.before){n=i.before(e,t,r);if(n&&n.revertOperations&&(r=n.revertOperations),n&&n.document&&(o=n.document),n&&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(p(f)){let t;for(let e=0;e<f.length;e++){var c=f[e],a=r(c,n,i.concat(e+""));a!==c&&((t=t||l(f))[e]=a)}return t||f}if(s(f)){let e;for(const u in f)Object.hasOwnProperty.call(f,u)&&(o=r(t=f[u],n,i.concat(u)))!==t&&((e=e||l(f))[u]=o);return e||f}return f},e.updateIn=a});