UNPKG

1.19 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const SAFE_IDENTIFIER = /^[_a-zA-Z$][_a-zA-Z$0-9]*$/;
9const 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 // strict mode
44 "implements",
45 "interface",
46 "let",
47 "package",
48 "private",
49 "protected",
50 "public",
51 "static",
52 "yield",
53 "yield",
54 // module code
55 "await",
56 // skip future reserved keywords defined under ES1 till ES3
57 // additional
58 "null",
59 "true",
60 "false"
61]);
62
63const 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
78module.exports = propertyAccess;