1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const SAFE_IDENTIFIER = /^[_a-zA-Z$][_a-zA-Z$0-9]*$/;
|
9 | const RESERVED_IDENTIFIER = new Set([
|
10 | "break",
|
11 | "case",
|
12 | "catch",
|
13 | "class",
|
14 | "const",
|
15 | "continue",
|
16 | "debugger",
|
17 | "default",
|
18 | "delete",
|
19 | "do",
|
20 | "else",
|
21 | "export",
|
22 | "extends",
|
23 | "finally",
|
24 | "for",
|
25 | "function",
|
26 | "if",
|
27 | "import",
|
28 | "in",
|
29 | "instanceof",
|
30 | "new",
|
31 | "return",
|
32 | "super",
|
33 | "switch",
|
34 | "this",
|
35 | "throw",
|
36 | "try",
|
37 | "typeof",
|
38 | "var",
|
39 | "void",
|
40 | "while",
|
41 | "with",
|
42 | "enum",
|
43 |
|
44 | "implements",
|
45 | "interface",
|
46 | "let",
|
47 | "package",
|
48 | "private",
|
49 | "protected",
|
50 | "public",
|
51 | "static",
|
52 | "yield",
|
53 | "yield",
|
54 |
|
55 | "await",
|
56 |
|
57 |
|
58 | "null",
|
59 | "true",
|
60 | "false"
|
61 | ]);
|
62 |
|
63 | const propertyAccess = (properties, start = 0) => {
|
64 | let str = "";
|
65 | for (let i = start; i < properties.length; i++) {
|
66 | const p = properties[i];
|
67 | if (`${+p}` === p) {
|
68 | str += `[${p}]`;
|
69 | } else if (SAFE_IDENTIFIER.test(p) && !RESERVED_IDENTIFIER.has(p)) {
|
70 | str += `.${p}`;
|
71 | } else {
|
72 | str += `[${JSON.stringify(p)}]`;
|
73 | }
|
74 | }
|
75 | return str;
|
76 | };
|
77 |
|
78 | module.exports = propertyAccess;
|