Skip to content

Commit 7eb8e32

Browse files
committed
fix(js-path): format numeric keys as array indices in JS Path output
1 parent 74061ee commit 7eb8e32

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/js/editor/jsonEditor.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,16 @@ export default class JsonEditor {
198198
}
199199

200200
getJsPath(pathArray) {
201-
return pathArray.map(key => typeof key === 'number' ? `[${key}]` : (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? `.${key}` : `["${String(key).replace(/"/g, '\\"')}"]`)).join('').replace(/^\./, '');
201+
return pathArray
202+
.map((key) =>
203+
typeof key === 'number' || /^\d+$/.test(key)
204+
? `[${key}]`
205+
: /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)
206+
? `.${key}`
207+
: `["${String(key).replace(/"/g, '\\"')}"]`
208+
)
209+
.join('')
210+
.replace(/^\./, '');
202211
}
203212

204213
getJsonPointer(pathArray) {

src/js/editor/treeView.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class TreeView {
218218
getJsPath(pathArray) {
219219
let path = '';
220220
pathArray.forEach((key) => {
221-
if (typeof key === 'number') {
221+
if (typeof key === 'number' || /^\d+$/.test(key)) {
222222
path += `[${key}]`;
223223
} else if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)) {
224224
path += `.${key}`;

0 commit comments

Comments
 (0)