-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.js
50 lines (38 loc) · 931 Bytes
/
path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const DIGIT_RE = /^\s*\d+\s*$/;
export function initialize(obj, path) {
if (obj === null) {
return typeof path[0] === "number" ? [] : Object.create(null);
}
return obj;
}
export function getPath(name, path = []) {
if (name.length === 0) {
return path;
}
const open = name.indexOf("[");
let close = name.indexOf("]");
if (open <= 0 && close === -1) {
path.push(name);
return path;
}
while (close < open && close !== -1) {
close = name.slice(close).indexOf("]");
}
if (close === -1) {
path.push(name);
return path;
}
if (open > 0) {
path.push(name.slice(0, open));
return getPath(name.slice(open), path);
}
const next = name.slice(open + 1, close);
if (next === "") {
path.push(-1);
} else if (next.match(DIGIT_RE)) {
path.push(Number.parseInt(next, 10));
} else {
path.push(next);
}
return getPath(name.slice(close + 1), path);
}