Skip to content

Commit da377ee

Browse files
wraithgarlukekarrys
authored andcommitted
deps: parse-conflict-json@2.0.2
1 parent 0f1cd60 commit da377ee

File tree

8 files changed

+241
-53
lines changed

8 files changed

+241
-53
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Definitions by: Eddie Atkinson <https://github.com/eddie-atkinson>
2+
3+
type Operation = "add" | "replace" | "remove";
4+
5+
type DiffOps = Array<{
6+
op: Operation;
7+
path: Array<string | number>;
8+
value?: any;
9+
}>;
10+
type PathConverter = (path: string) => string[];
11+
12+
export function diffApply<T extends object>(
13+
obj: T,
14+
diff: DiffOps,
15+
pathConverter?: PathConverter
16+
): T;
17+
export const jsonPatchPathConverter: PathConverter;

node_modules/just-diff-apply/index.js

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = {
4747
var REMOVE = 'remove';
4848
var REPLACE = 'replace';
4949
var ADD = 'add';
50+
var MOVE = 'move';
5051

5152
function diffApply(obj, diff, pathConverter) {
5253
if (!obj || typeof obj != 'object') {
@@ -62,44 +63,99 @@ function diffApply(obj, diff, pathConverter) {
6263
var thisDiff = diff[i];
6364
var subObject = obj;
6465
var thisOp = thisDiff.op;
65-
var thisPath = thisDiff.path;
66-
if (pathConverter) {
67-
thisPath = pathConverter(thisPath);
68-
if (!Array.isArray(thisPath)) {
69-
throw new Error('pathConverter must return an array');
66+
67+
var thisPath = transformPath(pathConverter, thisDiff.path);
68+
var thisFromPath = thisDiff.from && transformPath(pathConverter, thisDiff.from);
69+
var toPath, toPathCopy, lastToProp, subToObject, valueToMove;
70+
71+
if (thisFromPath) {
72+
// MOVE only, "fromPath" is effectively path and "path" is toPath
73+
toPath = thisPath;
74+
thisPath = thisFromPath;
75+
76+
toPathCopy = toPath.slice();
77+
lastToProp = toPathCopy.pop();
78+
prototypeCheck(lastToProp);
79+
if (lastToProp == null) {
80+
return false;
7081
}
71-
} else {
72-
if (!Array.isArray(thisPath)) {
73-
throw new Error('diff path must be an array, consider supplying a path converter');
82+
83+
var thisToProp;
84+
while (((thisToProp = toPathCopy.shift())) != null) {
85+
prototypeCheck(thisToProp);
86+
if (!(thisToProp in subToObject)) {
87+
subToObject[thisToProp] = {};
88+
}
89+
subToObject = subToObject[thisToProp];
7490
}
7591
}
92+
7693
var pathCopy = thisPath.slice();
7794
var lastProp = pathCopy.pop();
95+
prototypeCheck(lastProp);
7896
if (lastProp == null) {
7997
return false;
8098
}
99+
81100
var thisProp;
82101
while (((thisProp = pathCopy.shift())) != null) {
102+
prototypeCheck(thisProp);
83103
if (!(thisProp in subObject)) {
84104
subObject[thisProp] = {};
85105
}
86106
subObject = subObject[thisProp];
87107
}
88-
if (thisOp === REMOVE || thisOp === REPLACE) {
108+
if (thisOp === REMOVE || thisOp === REPLACE || thisOp === MOVE) {
109+
var path = thisOp === MOVE ? thisDiff.from : thisDiff.path;
89110
if (!subObject.hasOwnProperty(lastProp)) {
90-
throw new Error(['expected to find property', thisDiff.path, 'in object', obj].join(' '));
111+
throw new Error(['expected to find property', path, 'in object', obj].join(' '));
91112
}
92113
}
93-
if (thisOp === REMOVE) {
114+
if (thisOp === REMOVE || thisOp === MOVE) {
115+
if (thisOp === MOVE) {
116+
valueToMove = subObject[lastProp];
117+
}
94118
Array.isArray(subObject) ? subObject.splice(lastProp, 1) : delete subObject[lastProp];
95119
}
96120
if (thisOp === REPLACE || thisOp === ADD) {
97121
subObject[lastProp] = thisDiff.value;
98122
}
123+
124+
if (thisOp === MOVE) {
125+
subObject[lastToProp] = valueToMove;
126+
}
99127
}
100128
return subObject;
101129
}
102130

131+
function transformPath(pathConverter, thisPath) {
132+
if(pathConverter) {
133+
thisPath = pathConverter(thisPath);
134+
if(!Array.isArray(thisPath)) {
135+
throw new Error([
136+
'pathConverter must return an array, returned:',
137+
thisPath,
138+
].join(' '));
139+
}
140+
} else {
141+
if(!Array.isArray(thisPath)) {
142+
throw new Error([
143+
'diff path',
144+
thisPath,
145+
'must be an array, consider supplying a path converter']
146+
.join(' '));
147+
}
148+
}
149+
return thisPath;
150+
}
151+
103152
function jsonPatchPathConverter(stringPath) {
104153
return stringPath.split('/').slice(1);
105154
}
155+
156+
function prototypeCheck(prop) {
157+
// coercion is intentional to catch prop values like `['__proto__']`
158+
if (prop == '__proto__' || prop == 'constructor' || prop == 'prototype') {
159+
throw new Error('setting of prototype values not supported');
160+
}
161+
}

node_modules/just-diff-apply/index.mjs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,30 @@ function diffApply(obj, diff, pathConverter) {
6565
}
6666
} else {
6767
if (!Array.isArray(thisPath)) {
68-
throw new Error(
69-
'diff path must be an array, consider supplying a path converter'
70-
);
68+
throw new Error('diff path must be an array, consider supplying a path converter');
7169
}
7270
}
7371
var pathCopy = thisPath.slice();
7472
var lastProp = pathCopy.pop();
73+
prototypeCheck(lastProp);
7574
if (lastProp == null) {
7675
return false;
7776
}
7877
var thisProp;
79-
while ((thisProp = pathCopy.shift()) != null) {
78+
while (((thisProp = pathCopy.shift())) != null) {
79+
prototypeCheck(thisProp);
8080
if (!(thisProp in subObject)) {
8181
subObject[thisProp] = {};
8282
}
8383
subObject = subObject[thisProp];
8484
}
8585
if (thisOp === REMOVE || thisOp === REPLACE) {
8686
if (!subObject.hasOwnProperty(lastProp)) {
87-
throw new Error(
88-
['expected to find property', thisDiff.path, 'in object', obj].join(
89-
' '
90-
)
91-
);
87+
throw new Error(['expected to find property', thisDiff.path, 'in object', obj].join(' '));
9288
}
9389
}
9490
if (thisOp === REMOVE) {
95-
Array.isArray(subObject)
96-
? subObject.splice(lastProp, 1)
97-
: delete subObject[lastProp];
91+
Array.isArray(subObject) ? subObject.splice(lastProp, 1) : delete subObject[lastProp];
9892
}
9993
if (thisOp === REPLACE || thisOp === ADD) {
10094
subObject[lastProp] = thisDiff.value;
@@ -107,4 +101,11 @@ function jsonPatchPathConverter(stringPath) {
107101
return stringPath.split('/').slice(1);
108102
}
109103

104+
function prototypeCheck(prop) {
105+
// coercion is intentional to catch prop values like `['__proto__']`
106+
if (prop == '__proto__' || prop == 'constructor' || prop == 'prototype') {
107+
throw new Error('setting of prototype values not supported');
108+
}
109+
}
110+
110111
export {diffApply, jsonPatchPathConverter};
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import * as diffObj from "./index";
2+
3+
const { diffApply, jsonPatchPathConverter } = diffObj;
4+
const obj1 = {
5+
a: 2,
6+
b: 3,
7+
c: {
8+
d: 5
9+
}
10+
};
11+
const arr1 = [1, "bee"];
12+
13+
const objOps: diffObj.DiffOps = [
14+
{
15+
op: "replace",
16+
path: ["a"],
17+
value: 10
18+
},
19+
{
20+
op: "remove",
21+
path: ["b"]
22+
},
23+
{
24+
op: "add",
25+
path: ["e"],
26+
value: 15
27+
},
28+
{
29+
op: "remove",
30+
path: ["c", "d"]
31+
}
32+
];
33+
34+
const arrOps: diffObj.DiffOps = [
35+
{
36+
op: "replace",
37+
path: [1],
38+
value: 10
39+
},
40+
{
41+
op: "remove",
42+
path: [2]
43+
},
44+
{
45+
op: "add",
46+
path: [7],
47+
value: 15
48+
}
49+
];
50+
51+
//OK
52+
diffApply(obj1, objOps);
53+
diffApply(obj1, []);
54+
diffApply(arr1, arrOps);
55+
diffApply(arr1, []);
56+
diffApply(obj1, objOps, jsonPatchPathConverter);
57+
diffApply(arr1, arrOps, jsonPatchPathConverter);
58+
59+
// not OK
60+
// @ts-expect-error
61+
diffApply(obj1);
62+
// @ts-expect-error
63+
diffApply(arr2);
64+
// @ts-expect-error
65+
diffApply("a");
66+
// @ts-expect-error
67+
diffApply(true);
68+
69+
// @ts-expect-error
70+
diffApply(obj1, 1);
71+
// @ts-expect-error
72+
diffApply(3, arr2);
73+
// @ts-expect-error
74+
diffApply(obj1, "a");
75+
// @ts-expect-error
76+
diffApply("b", arr2);
77+
78+
// @ts-expect-error
79+
diffApply(obj1, [{ op: "delete", path: ["a"] }]);
80+
// @ts-expect-error
81+
diffApply(obj1, [{ op: "delete", path: ["a"] }], jsonPatchPathConverter);
82+
// @ts-expect-error
83+
diffApply(obj1, "a", jsonPatchPathConverter);
84+
// @ts-expect-error
85+
diffApply(obj1, ["a", "b", "c"], jsonPatchPathConverter);
86+
87+
// @ts-expect-error
88+
diff("a", jsonPatchPathConverter);
89+
// @ts-expect-error
90+
diff(true, jsonPatchPathConverter);
91+
92+
// @ts-expect-error
93+
diff(obj1, 1, jsonPatchPathConverter);
94+
// @ts-expect-error
95+
diff(3, arr2, jsonPatchPathConverter);
96+
// @ts-expect-error
97+
diff(obj1, "a", jsonPatchPathConverter);
98+
// @ts-expect-error
99+
diff("b", arr2, jsonPatchPathConverter);
100+
101+
// @ts-expect-error
102+
diff(obj1, obj2, "a");
103+
// @ts-expect-error
104+
diff(arr1, arr2, 1);
105+
// @ts-expect-error
106+
diff(obj1, arr1, "bee");
107+
// @ts-expect-error
108+
diff(obj2, arr2, "nope");

node_modules/just-diff-apply/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "just-diff-apply",
3-
"version": "4.0.1",
3+
"version": "5.2.0",
44
"description": "Apply a diff to an object. Optionally supports jsonPatch protocol",
55
"main": "index.js",
66
"module": "index.mjs",
@@ -10,6 +10,7 @@
1010
"default": "./index.mjs"
1111
}
1212
},
13+
"types": "index.d.ts",
1314
"scripts": {
1415
"test": "echo \"Error: no test specified\" && exit 1",
1516
"build": "rollup -c"
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parse-conflict-json",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Parse a JSON string that has git merge conflicts, resolving if possible",
55
"author": "GitHub Inc.",
66
"license": "ISC",
@@ -11,34 +11,39 @@
1111
"preversion": "npm test",
1212
"postversion": "npm publish",
1313
"postpublish": "git push origin --follow-tags",
14-
"lint": "eslint '**/*.js'",
15-
"postlint": "npm-template-check",
14+
"lint": "eslint \"**/*.js\"",
15+
"postlint": "template-oss-check",
1616
"lintfix": "npm run lint -- --fix",
1717
"prepublishOnly": "git push origin --follow-tags",
18-
"posttest": "npm run lint"
18+
"posttest": "npm run lint",
19+
"template-oss-apply": "template-oss-apply --force"
1920
},
2021
"tap": {
2122
"check-coverage": true
2223
},
2324
"devDependencies": {
24-
"@npmcli/template-oss": "^2.3.1",
25-
"tap": "^15.1.5"
25+
"@npmcli/eslint-config": "^3.0.1",
26+
"@npmcli/template-oss": "3.2.0",
27+
"tap": "^16.0.1"
2628
},
2729
"dependencies": {
2830
"json-parse-even-better-errors": "^2.3.1",
2931
"just-diff": "^5.0.1",
30-
"just-diff-apply": "^4.0.1"
32+
"just-diff-apply": "^5.2.0"
3133
},
3234
"repository": {
3335
"type": "git",
34-
"url": "git+https://github.com/npm/parse-conflict-json.git"
36+
"url": "https://github.com/npm/parse-conflict-json.git"
3537
},
3638
"files": [
37-
"bin",
38-
"lib"
39+
"bin/",
40+
"lib/"
3941
],
40-
"templateVersion": "2.3.1",
4142
"engines": {
42-
"node": "^12.13.0 || ^14.15.0 || >=16"
43+
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
44+
},
45+
"templateOSS": {
46+
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
47+
"version": "3.2.0"
4348
}
4449
}

0 commit comments

Comments
 (0)