Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jul 12, 2022
1 parent c94919d commit 18ddc57
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion node_modules/just-diff-apply/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Definitions by: Eddie Atkinson <https://github.com/eddie-atkinson>

type Operation = "add" | "replace" | "remove";
type Operation = "add" | "replace" | "remove" | "move";

type DiffOps = Array<{
op: Operation;
Expand Down
69 changes: 58 additions & 11 deletions node_modules/just-diff-apply/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion node_modules/just-diff-apply/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 18ddc57

Please sign in to comment.