Skip to content

Commit f1d38b1

Browse files
committed
custom path parser to better work with normalized paths
1 parent b3d268a commit f1d38b1

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/pages/Compare.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import JSONPane from "../components/JsonPane";
33
import { useParams, useNavigate } from "react-router-dom";
44
import { useEffect, useRef, useState } from "react";
55
import { _IDBStorageItem, useAppContext } from "../context/AppContext";
6-
import { JSONEditor, Content, Mode, OnChangeStatus, parseJSONPath, JSONPath } from "vanilla-jsoneditor";
6+
import { JSONEditor, Content, Mode, OnChangeStatus, JSONPath } from "vanilla-jsoneditor";
77
import Loading from "./Loading";
88
import { BiSolidUpArrow, BiSolidDownArrow } from "react-icons/bi";
99
import { sortObj, cleanJSON } from "jsonabc";
1010
import styles from "./compare.module.css";
1111
import { JSONDiff, Difference, difference } from "../utils";
12+
import { parseJSONPath } from "../utils/butils";
1213
import * as utils from "../utils";
1314
import { useWorker, WORKER_STATUS } from "../worker";
1415

@@ -165,6 +166,7 @@ export default function Compare() {
165166
const navigate = useNavigate();
166167

167168
useEffect(() => {
169+
// (window as any).test = parseJSONPath;
168170
if (id === undefined) {
169171
navigate("/compare/new");
170172
return;
@@ -270,6 +272,7 @@ export default function Compare() {
270272

271273
function highlightPath(path: JSONPath) {
272274
let fn = async (p: JSONPath) => {
275+
// console.log('using json path', p);
273276
return encodeURIComponent(`/${p.map(v => v.replace('/', '~1')).join("/")}`);
274277
};
275278
let styleRules: Promise<string>[] = [];
@@ -290,11 +293,13 @@ export default function Compare() {
290293
}
291294
for (let i = 0; i < sideDiff.extra.length; i++) {
292295
let path = sideDiff.extra[i];
296+
// console.log('using path', path);
293297
let _path = parseJSONPath(path.substring(2));
294298
tasks.push(highlightPath(_path));
295299
}
296300
for (let i = 0; i < sideDiff.missing.length; i++) {
297301
let path = sideDiff.missing[i];
302+
// console.log('using path', path);
298303
let _path = parseJSONPath(path.substring(2));
299304
tasks.push(highlightPath(_path));
300305
}

src/utils/butils.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { JSONPath } from "vanilla-jsoneditor";
2+
3+
export function isNumericString(value: string) {
4+
return /^\d+$/.test(value);
5+
}
6+
7+
export function parseJSONPath(path: string): JSONPath {
8+
let parsed: JSONPath = [];
9+
let start_index = 0;
10+
let index = 0;
11+
while (index < path.length) {
12+
let char = path[index];
13+
switch (char) {
14+
case ".": {
15+
if (index > start_index) {
16+
parsed.push(path.slice(start_index, index));
17+
index += 1;
18+
start_index = index;
19+
} else {
20+
console.log('skipping', char);
21+
index += 1;
22+
}
23+
} break;
24+
case "]": {
25+
if (path[start_index] === '[') {
26+
let key = path.slice(start_index+1, index);
27+
if (isNumericString(key)) {
28+
parsed.push(key);
29+
index += 1;
30+
start_index = index;
31+
}
32+
else {
33+
index += 1;
34+
}
35+
}
36+
else {
37+
index += 1;
38+
}
39+
} break;
40+
case '"': {
41+
let start = index+1;
42+
let end = path.indexOf('"', start);
43+
if (path.length > end+1 && path[end+1] === '.') {
44+
parsed.push(path.slice(start, end));
45+
index = end+2;
46+
start_index = index;
47+
} else if (path.length == end+1) {
48+
parsed.push(path.slice(start, end));
49+
index = end+2;
50+
start_index = index;
51+
} else {
52+
index = end+1;
53+
}
54+
} break;
55+
case '\\': {
56+
index += 2;
57+
} break;
58+
default: {
59+
index += 1;
60+
} break;
61+
}
62+
}
63+
if (start_index < path.length) {
64+
parsed.push(path.slice(start_index));
65+
}
66+
// console.log('processing', path, 'returned', parsed, start_index);
67+
return parsed;
68+
}

0 commit comments

Comments
 (0)