diff --git a/node_modules/just-diff-apply/index.d.ts b/node_modules/just-diff-apply/index.d.ts index 9fc05257af0aa..7547b722f484f 100644 --- a/node_modules/just-diff-apply/index.d.ts +++ b/node_modules/just-diff-apply/index.d.ts @@ -1,6 +1,6 @@ // Definitions by: Eddie Atkinson -type Operation = "add" | "replace" | "remove"; +type Operation = "add" | "replace" | "remove" | "move"; type DiffOps = Array<{ op: Operation; diff --git a/node_modules/just-diff-apply/index.mjs b/node_modules/just-diff-apply/index.mjs index 045830507cd17..adc5f46ed51df 100644 --- a/node_modules/just-diff-apply/index.mjs +++ b/node_modules/just-diff-apply/index.mjs @@ -42,6 +42,7 @@ var REMOVE = 'remove'; var REPLACE = 'replace'; var ADD = 'add'; +var MOVE = 'move'; function diffApply(obj, diff, pathConverter) { if (!obj || typeof obj != 'object') { @@ -57,23 +58,40 @@ function diffApply(obj, diff, pathConverter) { var thisDiff = diff[i]; var subObject = obj; var thisOp = thisDiff.op; - var thisPath = thisDiff.path; - if (pathConverter) { - thisPath = pathConverter(thisPath); - if (!Array.isArray(thisPath)) { - throw new Error('pathConverter must return an array'); + + var thisPath = transformPath(pathConverter, thisDiff.path); + var thisFromPath = thisDiff.from && transformPath(pathConverter, thisDiff.from); + var toPath, toPathCopy, lastToProp, subToObject, valueToMove; + + if (thisFromPath) { + // MOVE only, "fromPath" is effectively path and "path" is toPath + toPath = thisPath; + thisPath = thisFromPath; + + toPathCopy = toPath.slice(); + lastToProp = toPathCopy.pop(); + prototypeCheck(lastToProp); + if (lastToProp == null) { + return false; } - } else { - if (!Array.isArray(thisPath)) { - throw new Error('diff path must be an array, consider supplying a path converter'); + + var thisToProp; + while (((thisToProp = toPathCopy.shift())) != null) { + prototypeCheck(thisToProp); + if (!(thisToProp in subToObject)) { + subToObject[thisToProp] = {}; + } + subToObject = subToObject[thisToProp]; } } + var pathCopy = thisPath.slice(); var lastProp = pathCopy.pop(); prototypeCheck(lastProp); if (lastProp == null) { return false; } + var thisProp; while (((thisProp = pathCopy.shift())) != null) { prototypeCheck(thisProp); @@ -82,21 +100,50 @@ function diffApply(obj, diff, pathConverter) { } subObject = subObject[thisProp]; } - if (thisOp === REMOVE || thisOp === REPLACE) { + if (thisOp === REMOVE || thisOp === REPLACE || thisOp === MOVE) { + var path = thisOp === MOVE ? thisDiff.from : thisDiff.path; if (!subObject.hasOwnProperty(lastProp)) { - throw new Error(['expected to find property', thisDiff.path, 'in object', obj].join(' ')); + throw new Error(['expected to find property', path, 'in object', obj].join(' ')); } } - if (thisOp === REMOVE) { + if (thisOp === REMOVE || thisOp === MOVE) { + if (thisOp === MOVE) { + valueToMove = subObject[lastProp]; + } Array.isArray(subObject) ? subObject.splice(lastProp, 1) : delete subObject[lastProp]; } if (thisOp === REPLACE || thisOp === ADD) { subObject[lastProp] = thisDiff.value; } + + if (thisOp === MOVE) { + subObject[lastToProp] = valueToMove; + } } return subObject; } +function transformPath(pathConverter, thisPath) { + if(pathConverter) { + thisPath = pathConverter(thisPath); + if(!Array.isArray(thisPath)) { + throw new Error([ + 'pathConverter must return an array, returned:', + thisPath, + ].join(' ')); + } + } else { + if(!Array.isArray(thisPath)) { + throw new Error([ + 'diff path', + thisPath, + 'must be an array, consider supplying a path converter'] + .join(' ')); + } + } + return thisPath; +} + function jsonPatchPathConverter(stringPath) { return stringPath.split('/').slice(1); } diff --git a/node_modules/just-diff-apply/package.json b/node_modules/just-diff-apply/package.json index b8e5012ff83b1..b2f80b73a19c6 100644 --- a/node_modules/just-diff-apply/package.json +++ b/node_modules/just-diff-apply/package.json @@ -1,6 +1,6 @@ { "name": "just-diff-apply", - "version": "5.2.0", + "version": "5.3.1", "description": "Apply a diff to an object. Optionally supports jsonPatch protocol", "main": "index.js", "module": "index.mjs", diff --git a/package-lock.json b/package-lock.json index b19e310408bb3..e2c6397e9ef5e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4396,9 +4396,10 @@ "inBundle": true }, "node_modules/just-diff-apply": { - "version": "5.2.0", - "inBundle": true, - "license": "MIT" + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/just-diff-apply/-/just-diff-apply-5.3.1.tgz", + "integrity": "sha512-dgFenZnMsc1xGNqgdtgnh7DK+Oy352CE3VZLbzcbQpsBs9iI2K3M0IRrdgREZ72eItTjbl0suRyvKRdVQa9GbA==", + "inBundle": true }, "node_modules/lcov-parse": { "version": "1.0.0",