Skip to content

Commit 18ddc57

Browse files
committed
1 parent c94919d commit 18ddc57

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

node_modules/just-diff-apply/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Definitions by: Eddie Atkinson <https://github.com/eddie-atkinson>
22

3-
type Operation = "add" | "replace" | "remove";
3+
type Operation = "add" | "replace" | "remove" | "move";
44

55
type DiffOps = Array<{
66
op: Operation;

node_modules/just-diff-apply/index.mjs

+58-11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
var REMOVE = 'remove';
4343
var REPLACE = 'replace';
4444
var ADD = 'add';
45+
var MOVE = 'move';
4546

4647
function diffApply(obj, diff, pathConverter) {
4748
if (!obj || typeof obj != 'object') {
@@ -57,23 +58,40 @@ function diffApply(obj, diff, pathConverter) {
5758
var thisDiff = diff[i];
5859
var subObject = obj;
5960
var thisOp = thisDiff.op;
60-
var thisPath = thisDiff.path;
61-
if (pathConverter) {
62-
thisPath = pathConverter(thisPath);
63-
if (!Array.isArray(thisPath)) {
64-
throw new Error('pathConverter must return an array');
61+
62+
var thisPath = transformPath(pathConverter, thisDiff.path);
63+
var thisFromPath = thisDiff.from && transformPath(pathConverter, thisDiff.from);
64+
var toPath, toPathCopy, lastToProp, subToObject, valueToMove;
65+
66+
if (thisFromPath) {
67+
// MOVE only, "fromPath" is effectively path and "path" is toPath
68+
toPath = thisPath;
69+
thisPath = thisFromPath;
70+
71+
toPathCopy = toPath.slice();
72+
lastToProp = toPathCopy.pop();
73+
prototypeCheck(lastToProp);
74+
if (lastToProp == null) {
75+
return false;
6576
}
66-
} else {
67-
if (!Array.isArray(thisPath)) {
68-
throw new Error('diff path must be an array, consider supplying a path converter');
77+
78+
var thisToProp;
79+
while (((thisToProp = toPathCopy.shift())) != null) {
80+
prototypeCheck(thisToProp);
81+
if (!(thisToProp in subToObject)) {
82+
subToObject[thisToProp] = {};
83+
}
84+
subToObject = subToObject[thisToProp];
6985
}
7086
}
87+
7188
var pathCopy = thisPath.slice();
7289
var lastProp = pathCopy.pop();
7390
prototypeCheck(lastProp);
7491
if (lastProp == null) {
7592
return false;
7693
}
94+
7795
var thisProp;
7896
while (((thisProp = pathCopy.shift())) != null) {
7997
prototypeCheck(thisProp);
@@ -82,21 +100,50 @@ function diffApply(obj, diff, pathConverter) {
82100
}
83101
subObject = subObject[thisProp];
84102
}
85-
if (thisOp === REMOVE || thisOp === REPLACE) {
103+
if (thisOp === REMOVE || thisOp === REPLACE || thisOp === MOVE) {
104+
var path = thisOp === MOVE ? thisDiff.from : thisDiff.path;
86105
if (!subObject.hasOwnProperty(lastProp)) {
87-
throw new Error(['expected to find property', thisDiff.path, 'in object', obj].join(' '));
106+
throw new Error(['expected to find property', path, 'in object', obj].join(' '));
88107
}
89108
}
90-
if (thisOp === REMOVE) {
109+
if (thisOp === REMOVE || thisOp === MOVE) {
110+
if (thisOp === MOVE) {
111+
valueToMove = subObject[lastProp];
112+
}
91113
Array.isArray(subObject) ? subObject.splice(lastProp, 1) : delete subObject[lastProp];
92114
}
93115
if (thisOp === REPLACE || thisOp === ADD) {
94116
subObject[lastProp] = thisDiff.value;
95117
}
118+
119+
if (thisOp === MOVE) {
120+
subObject[lastToProp] = valueToMove;
121+
}
96122
}
97123
return subObject;
98124
}
99125

126+
function transformPath(pathConverter, thisPath) {
127+
if(pathConverter) {
128+
thisPath = pathConverter(thisPath);
129+
if(!Array.isArray(thisPath)) {
130+
throw new Error([
131+
'pathConverter must return an array, returned:',
132+
thisPath,
133+
].join(' '));
134+
}
135+
} else {
136+
if(!Array.isArray(thisPath)) {
137+
throw new Error([
138+
'diff path',
139+
thisPath,
140+
'must be an array, consider supplying a path converter']
141+
.join(' '));
142+
}
143+
}
144+
return thisPath;
145+
}
146+
100147
function jsonPatchPathConverter(stringPath) {
101148
return stringPath.split('/').slice(1);
102149
}

node_modules/just-diff-apply/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "just-diff-apply",
3-
"version": "5.2.0",
3+
"version": "5.3.1",
44
"description": "Apply a diff to an object. Optionally supports jsonPatch protocol",
55
"main": "index.js",
66
"module": "index.mjs",

package-lock.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -4396,9 +4396,10 @@
43964396
"inBundle": true
43974397
},
43984398
"node_modules/just-diff-apply": {
4399-
"version": "5.2.0",
4400-
"inBundle": true,
4401-
"license": "MIT"
4399+
"version": "5.3.1",
4400+
"resolved": "https://registry.npmjs.org/just-diff-apply/-/just-diff-apply-5.3.1.tgz",
4401+
"integrity": "sha512-dgFenZnMsc1xGNqgdtgnh7DK+Oy352CE3VZLbzcbQpsBs9iI2K3M0IRrdgREZ72eItTjbl0suRyvKRdVQa9GbA==",
4402+
"inBundle": true
44024403
},
44034404
"node_modules/lcov-parse": {
44044405
"version": "1.0.0",

0 commit comments

Comments
 (0)