immutable-json-patch
Advanced tools
Comparing version 1.1.0 to 1.1.1
# Changelog | ||
## 2021-01-27, version 1.1.1 | ||
- Fix revert operation of operation `move` not correct when moving a child | ||
object to the root. | ||
## 2021-01-20, version 1.1.0 | ||
@@ -5,0 +11,0 @@ |
@@ -10,5 +10,7 @@ "use strict"; | ||
var _immutableJSONPatch = require("./immutableJSONPatch.js"); | ||
var _jsonPointer = require("./jsonPointer.js"); | ||
var _immutableJSONPatch = require("./immutableJSONPatch.js"); | ||
var _utils = require("./utils.js"); | ||
@@ -119,2 +121,12 @@ /** | ||
from = _ref5.from; | ||
if (path.length < from.length && (0, _utils.startsWith)(from, path)) { | ||
// replacing the parent with the child | ||
return [{ | ||
op: 'replace', | ||
path: (0, _jsonPointer.compileJSONPointer)(path), | ||
value: json | ||
}]; | ||
} | ||
var revert = [{ | ||
@@ -121,0 +133,0 @@ op: 'move', |
@@ -7,4 +7,6 @@ "use strict"; | ||
exports.isEqual = isEqual; | ||
exports.strictEqual = strictEqual; | ||
exports.initial = initial; | ||
exports.last = last; | ||
exports.startsWith = startsWith; | ||
exports.isObjectOrArray = isObjectOrArray; | ||
@@ -22,5 +24,18 @@ | ||
function isEqual(a, b) { | ||
// FIXME: this function will return false for two objects with the same keys | ||
// but different order of keys | ||
return JSON.stringify(a) === JSON.stringify(b); | ||
} | ||
/** | ||
* Test whether two values are strictly equal | ||
* @param {*} a | ||
* @param {*} b | ||
* @returns {boolean} | ||
*/ | ||
function strictEqual(a, b) { | ||
return a === b; | ||
} | ||
/** | ||
* Get all but the last items from an array | ||
@@ -48,2 +63,25 @@ * @param {Array} array | ||
/** | ||
* Test whether array1 starts with array2 | ||
* @param {Array} array1 | ||
* @param {Array} array2 | ||
* @param {function} [isEqual=strictEqual] Optional function to check equality | ||
*/ | ||
function startsWith(array1, array2) { | ||
var isEqual = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : strictEqual; | ||
if (array1.length < array2.length) { | ||
return false; | ||
} | ||
for (var i = 0; i < array2.length; i++) { | ||
if (!isEqual(array1[i], array2[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* Test whether a value is an Object or an Array (and not a primitive JSON value) | ||
@@ -50,0 +88,0 @@ * @param {*} value |
@@ -1,3 +0,3 @@ | ||
import { setIn, getIn, deleteIn, insertAt, existsIn } from './immutabilityHelpers.js'; | ||
import { parseJSONPointer, compileJSONPointer } from './jsonPointer.js'; | ||
import { deleteIn, existsIn, getIn, insertAt, setIn } from './immutabilityHelpers.js'; | ||
import { compileJSONPointer, parseJSONPointer } from './jsonPointer.js'; | ||
import { initial, isEqual, last } from './utils.js'; | ||
@@ -4,0 +4,0 @@ /** |
import { existsIn, getIn } from './immutabilityHelpers.js'; | ||
import { immutableJSONPatch, isArrayItem } from './immutableJSONPatch.js'; | ||
import { compileJSONPointer } from './jsonPointer.js'; | ||
import { immutableJSONPatch, isArrayItem } from './immutableJSONPatch.js'; | ||
import { startsWith } from './utils.js'; | ||
/** | ||
@@ -108,2 +109,12 @@ * Create the inverse of a set of json patch operations | ||
from = _ref5.from; | ||
if (path.length < from.length && startsWith(from, path)) { | ||
// replacing the parent with the child | ||
return [{ | ||
op: 'replace', | ||
path: compileJSONPointer(path), | ||
value: json | ||
}]; | ||
} | ||
var revert = [{ | ||
@@ -110,0 +121,0 @@ op: 'move', |
@@ -11,5 +11,17 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
export function isEqual(a, b) { | ||
// FIXME: this function will return false for two objects with the same keys | ||
// but different order of keys | ||
return JSON.stringify(a) === JSON.stringify(b); | ||
} | ||
/** | ||
* Test whether two values are strictly equal | ||
* @param {*} a | ||
* @param {*} b | ||
* @returns {boolean} | ||
*/ | ||
export function strictEqual(a, b) { | ||
return a === b; | ||
} | ||
/** | ||
* Get all but the last items from an array | ||
@@ -35,2 +47,24 @@ * @param {Array} array | ||
/** | ||
* Test whether array1 starts with array2 | ||
* @param {Array} array1 | ||
* @param {Array} array2 | ||
* @param {function} [isEqual=strictEqual] Optional function to check equality | ||
*/ | ||
export function startsWith(array1, array2) { | ||
var isEqual = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : strictEqual; | ||
if (array1.length < array2.length) { | ||
return false; | ||
} | ||
for (var i = 0; i < array2.length; i++) { | ||
if (!isEqual(array1[i], array2[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* Test whether a value is an Object or an Array (and not a primitive JSON value) | ||
@@ -37,0 +71,0 @@ * @param {*} value |
@@ -17,5 +17,17 @@ (function (global, factory) { | ||
function isEqual(a, b) { | ||
// FIXME: this function will return false for two objects with the same keys | ||
// but different order of keys | ||
return JSON.stringify(a) === JSON.stringify(b); | ||
} | ||
/** | ||
* Test whether two values are strictly equal | ||
* @param {*} a | ||
* @param {*} b | ||
* @returns {boolean} | ||
*/ | ||
function strictEqual(a, b) { | ||
return a === b; | ||
} | ||
/** | ||
* Get all but the last items from an array | ||
@@ -41,2 +53,24 @@ * @param {Array} array | ||
/** | ||
* Test whether array1 starts with array2 | ||
* @param {Array} array1 | ||
* @param {Array} array2 | ||
* @param {function} [isEqual=strictEqual] Optional function to check equality | ||
*/ | ||
function startsWith(array1, array2) { | ||
var isEqual = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : strictEqual; | ||
if (array1.length < array2.length) { | ||
return false; | ||
} | ||
for (var i = 0; i < array2.length; i++) { | ||
if (!isEqual(array1[i], array2[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** | ||
* Test whether a value is an Object or an Array (and not a primitive JSON value) | ||
@@ -635,2 +669,12 @@ * @param {*} value | ||
from = _ref5.from; | ||
if (path.length < from.length && startsWith(from, path)) { | ||
// replacing the parent with the child | ||
return [{ | ||
op: 'replace', | ||
path: compileJSONPointer(path), | ||
value: json | ||
}]; | ||
} | ||
var revert = [{ | ||
@@ -637,0 +681,0 @@ op: 'move', |
@@ -1,1 +0,1 @@ | ||
!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r="undefined"!=typeof globalThis?globalThis:r||self).immutableJSONPatch={})}(this,function(r){"use strict";function t(r){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}function a(r){return r.slice(0,r.length-1)}function i(r){return"object"===t(r)&&null!==r}function e(t,r){var e,n=Object.keys(t);return Object.getOwnPropertySymbols&&(e=Object.getOwnPropertySymbols(t),r&&(e=e.filter(function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable})),n.push.apply(n,e)),n}function o(n){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?e(Object(o),!0).forEach(function(r){var t,e;t=n,r=o[e=r],e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach(function(r){Object.defineProperty(n,r,Object.getOwnPropertyDescriptor(o,r))})}return n}function u(r){return(u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}function f(t){if(Array.isArray(t)){var e=t.slice();return Object.getOwnPropertySymbols(t).forEach(function(r){e[r]=t[r]}),e}if("object"!==u(t))return t;var n=o({},t);return Object.getOwnPropertySymbols(t).forEach(function(r){n[r]=t[r]}),n}function c(r,t,e){if(r[t]===e)return r;r=f(r);return r[t]=e,r}function p(r,t){for(var e=r,n=0;n<t.length;)e=i(e)?e[t[n]]:void 0,n++;return e}function l(r,t,e){var n=3<arguments.length&&void 0!==arguments[3]&&arguments[3];if(0===t.length)return e;var o=t[0],e=l(r?r[o]:void 0,t.slice(1),e,n);if(i(r))return c(r,o,e);if(n){n="number"==typeof o?[]:{};return n[o]=e,n}throw new Error("Path does not exist")}function s(r,t,e){if(0===t.length)return e(r);if(!i(r))throw new Error("Path doesn't exist");var n=t[0];return c(r,n,s(r[n],t.slice(1),e))}function y(r,t){if(0===t.length)return r;if(!i(r))throw new Error("Path does not exist");if(1===t.length){var e=t[0];if(e in r){var n=f(r);return Array.isArray(n)?n.splice(e,1):delete n[e],n}return r}n=t[0];return c(r,n,y(r[n],t.slice(1)))}function h(r,t,e){var n=t.slice(0,t.length-1),o=t[t.length-1];return s(r,n,function(r){if(!Array.isArray(r))throw new TypeError("Array expected at path "+JSON.stringify(n));r=f(r);return r.splice(o,0,e),r})}function v(r,t){return void 0!==r&&(0===t.length||v(r[t[0]],t.slice(1)))}function m(r){r=r.split("/");return r.shift(),r.map(function(r){return r.replace(/~1/g,"/").replace(/~0/g,"~")})}function d(r){return r.map(function(r){return"/"+String(r).replace(/~/g,"~0").replace(/\//g,"~1")}).join("")}function b(r,t,e){for(var n=r,o=0;o<t.length;o++){!function(r){if(!["add","remove","replace","copy","move","test"].includes(r.op))throw new Error("Unknown JSONPatch op "+JSON.stringify(r.op));if("string"!=typeof r.path)throw new Error('Required property "path" missing or not a string in operation '+JSON.stringify(r));if(("copy"===r.op||"move"===r.op)&&"string"!=typeof r.from)throw new Error('Required property "from" missing or not a string in operation '+JSON.stringify(r))}(t[o]);var i=(i=n,{op:(u=t[o]).op,path:function(r,t){if("-"!==function(r){return r[r.length-1]}(t))return t;t=a(t),r=p(r,t);return t.concat(r.length)}(i,m(u.path)),from:void 0!==u.from?m(u.from):null,value:u.value});e&&e.before&&(void 0!==(f=e.before(n,i))&&(void 0!==f.json&&(n=f.json),void 0!==f.operation&&(i=f.operation)));var u=n,f=g[i.op];if(f)n=f(n,i);else{if("test"!==i.op)throw new Error("Unknown JSONPatch operation "+JSON.stringify(i.op));!function(r,t){var e=t.path;if(void 0===(t=t.value))throw new Error('Test failed: no value provided (path: "'.concat(d(e),'")'));if(!v(r,e))throw new Error('Test failed: path not found (path: "'.concat(d(e),'")'));if(!function(r,t){return JSON.stringify(r)===JSON.stringify(t)}(p(r,e),t))throw new Error('Test failed, value differs (path: "'.concat(d(e),'")'))}(n,i)}e&&e.after&&(void 0!==(u=e.after(n,i,u))&&(n=u))}return n}var g={add:function(r,t){var e=t.path,t=t.value;return(O(r,e)?h:l)(r,e,t)},remove:function(r,t){t=t.path;return y(r,t)},replace:function(r,t){var e=t.path,t=t.value;return l(r,e,t)},copy:function(r,t){var e=t.path,n=t.from,t=p(r,n);{if(O(r,e))return h(r,e,t);n=p(r,n);return l(r,e,n)}},move:function(r,t){var e=t.path,n=t.from,t=p(r,n),n=y(r,n);return(O(n,e)?h:l)(n,e,t)}};function O(r,t){t=p(r,a(t));return Array.isArray(t)}var w={add:P,remove:S,replace:n,copy:function(r,t){var e=t.path,t=t.value;return P(r,{path:e,value:t})},move:function(r,t){var e=t.path,t=t.from,t=[{op:"move",from:d(e),path:d(t)}];!O(r,e)&&v(r,e)&&(t=t.concat(S(r,{path:e})));return t}};function n(r,t){t=t.path;return[{op:"replace",path:d(t),value:p(r,t)}]}function S(r,t){t=t.path;return[{op:"add",path:d(t),value:p(r,t)}]}function P(r,t){var e=t.path,t=t.value;return O(r,e)||!v(r,e)?[{op:"remove",path:d(e)}]:n(r,{path:e,value:t})}r.compileJSONPointer=d,r.deleteIn=y,r.existsIn=v,r.getIn=p,r.immutableJSONPatch=b,r.insertAt=h,r.parseJSONPointer=m,r.revertJSONPatch=function(r,t){var n=[];return b(r,t,{before:function(r,t){var e=w[t.op];e&&(n=e(r,t).concat(n))}}),n},r.setIn=l,r.updateIn=s,Object.defineProperty(r,"__esModule",{value:!0})}); | ||
!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r="undefined"!=typeof globalThis?globalThis:r||self).immutableJSONPatch={})}(this,function(r){"use strict";function t(r){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}function i(r,t){return r===t}function a(r){return r.slice(0,r.length-1)}function u(r){return"object"===t(r)&&null!==r}function e(t,r){var e,n=Object.keys(t);return Object.getOwnPropertySymbols&&(e=Object.getOwnPropertySymbols(t),r&&(e=e.filter(function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable})),n.push.apply(n,e)),n}function o(n){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?e(Object(o),!0).forEach(function(r){var t,e;t=n,r=o[e=r],e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r}):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach(function(r){Object.defineProperty(n,r,Object.getOwnPropertyDescriptor(o,r))})}return n}function f(r){return(f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}function c(t){if(Array.isArray(t)){var e=t.slice();return Object.getOwnPropertySymbols(t).forEach(function(r){e[r]=t[r]}),e}if("object"!==f(t))return t;var n=o({},t);return Object.getOwnPropertySymbols(t).forEach(function(r){n[r]=t[r]}),n}function p(r,t,e){if(r[t]===e)return r;r=c(r);return r[t]=e,r}function l(r,t){for(var e=r,n=0;n<t.length;)e=u(e)?e[t[n]]:void 0,n++;return e}function s(r,t,e){var n=3<arguments.length&&void 0!==arguments[3]&&arguments[3];if(0===t.length)return e;var o=t[0],e=s(r?r[o]:void 0,t.slice(1),e,n);if(u(r))return p(r,o,e);if(n){n="number"==typeof o?[]:{};return n[o]=e,n}throw new Error("Path does not exist")}function h(r,t,e){if(0===t.length)return e(r);if(!u(r))throw new Error("Path doesn't exist");var n=t[0];return p(r,n,h(r[n],t.slice(1),e))}function y(r,t){if(0===t.length)return r;if(!u(r))throw new Error("Path does not exist");if(1===t.length){var e=t[0];if(e in r){var n=c(r);return Array.isArray(n)?n.splice(e,1):delete n[e],n}return r}n=t[0];return p(r,n,y(r[n],t.slice(1)))}function v(r,t,e){var n=t.slice(0,t.length-1),o=t[t.length-1];return h(r,n,function(r){if(!Array.isArray(r))throw new TypeError("Array expected at path "+JSON.stringify(n));r=c(r);return r.splice(o,0,e),r})}function d(r,t){return void 0!==r&&(0===t.length||d(r[t[0]],t.slice(1)))}function g(r){r=r.split("/");return r.shift(),r.map(function(r){return r.replace(/~1/g,"/").replace(/~0/g,"~")})}function m(r){return r.map(function(r){return"/"+String(r).replace(/~/g,"~0").replace(/\//g,"~1")}).join("")}function b(r,t,e){for(var n=r,o=0;o<t.length;o++){!function(r){if(!["add","remove","replace","copy","move","test"].includes(r.op))throw new Error("Unknown JSONPatch op "+JSON.stringify(r.op));if("string"!=typeof r.path)throw new Error('Required property "path" missing or not a string in operation '+JSON.stringify(r));if(("copy"===r.op||"move"===r.op)&&"string"!=typeof r.from)throw new Error('Required property "from" missing or not a string in operation '+JSON.stringify(r))}(t[o]);var i=(i=n,{op:(u=t[o]).op,path:function(r,t){if("-"!==function(r){return r[r.length-1]}(t))return t;t=a(t),r=l(r,t);return t.concat(r.length)}(i,g(u.path)),from:void 0!==u.from?g(u.from):null,value:u.value});e&&e.before&&(void 0!==(f=e.before(n,i))&&(void 0!==f.json&&(n=f.json),void 0!==f.operation&&(i=f.operation)));var u=n,f=O[i.op];if(f)n=f(n,i);else{if("test"!==i.op)throw new Error("Unknown JSONPatch operation "+JSON.stringify(i.op));!function(r,t){var e=t.path;if(void 0===(t=t.value))throw new Error('Test failed: no value provided (path: "'.concat(m(e),'")'));if(!d(r,e))throw new Error('Test failed: path not found (path: "'.concat(m(e),'")'));if(!function(r,t){return JSON.stringify(r)===JSON.stringify(t)}(l(r,e),t))throw new Error('Test failed, value differs (path: "'.concat(m(e),'")'))}(n,i)}e&&e.after&&(void 0!==(u=e.after(n,i,u))&&(n=u))}return n}var O={add:function(r,t){var e=t.path,t=t.value;return(w(r,e)?v:s)(r,e,t)},remove:function(r,t){t=t.path;return y(r,t)},replace:function(r,t){var e=t.path,t=t.value;return s(r,e,t)},copy:function(r,t){var e=t.path,n=t.from,t=l(r,n);{if(w(r,e))return v(r,e,t);n=l(r,n);return s(r,e,n)}},move:function(r,t){var e=t.path,n=t.from,t=l(r,n),n=y(r,n);return(w(n,e)?v:s)(n,e,t)}};function w(r,t){t=l(r,a(t));return Array.isArray(t)}var S={add:j,remove:P,replace:n,copy:function(r,t){var e=t.path,t=t.value;return j(r,{path:e,value:t})},move:function(r,t){var e=t.path,t=t.from;if(e.length<t.length&&function(r,t,e){var n=2<arguments.length&&void 0!==e?e:i;if(!(r.length<t.length)){for(var o=0;o<t.length;o++)if(!n(r[o],t[o]))return;return 1}}(t,e))return[{op:"replace",path:m(e),value:r}];t=[{op:"move",from:m(e),path:m(t)}];!w(r,e)&&d(r,e)&&(t=t.concat(P(r,{path:e})));return t}};function n(r,t){t=t.path;return[{op:"replace",path:m(t),value:l(r,t)}]}function P(r,t){t=t.path;return[{op:"add",path:m(t),value:l(r,t)}]}function j(r,t){var e=t.path,t=t.value;return w(r,e)||!d(r,e)?[{op:"remove",path:m(e)}]:n(r,{path:e,value:t})}r.compileJSONPointer=m,r.deleteIn=y,r.existsIn=d,r.getIn=l,r.immutableJSONPatch=b,r.insertAt=v,r.parseJSONPointer=g,r.revertJSONPatch=function(r,t){var n=[];return b(r,t,{before:function(r,t){var e=S[t.op];e&&(n=e(r,t).concat(n))}}),n},r.setIn=s,r.updateIn=h,Object.defineProperty(r,"__esModule",{value:!0})}); |
{ | ||
"name": "immutable-json-patch", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Immutable JSON patch with support for reverting operations", | ||
@@ -55,3 +55,3 @@ "repository": { | ||
"mocha": "8.2.1", | ||
"rollup": "2.37.1", | ||
"rollup": "2.38.0", | ||
"uglify-js": "3.12.5" | ||
@@ -58,0 +58,0 @@ }, |
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
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
206201
2076