UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.escapeString = exports.escapeIdentifier = exports.maxHexLength = exports.digitsChars = exports.quoteChars = exports.whitespaceChars = exports.stringRenderEscapeChars = exports.identEscapeChars = exports.isHex = exports.isIdent = exports.isIdentStart = void 0;
4function isIdentStart(c) {
5 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c === '-' || c === '_' || c === '\\' || c >= '\u00a0';
6}
7exports.isIdentStart = isIdentStart;
8function isIdent(c) {
9 return ((c >= 'a' && c <= 'z') ||
10 (c >= 'A' && c <= 'Z') ||
11 (c >= '0' && c <= '9') ||
12 c === '-' ||
13 c === '_' ||
14 c >= '\u00a0');
15}
16exports.isIdent = isIdent;
17function isHex(c) {
18 return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9');
19}
20exports.isHex = isHex;
21exports.identEscapeChars = {
22 '!': true,
23 '"': true,
24 '#': true,
25 $: true,
26 '%': true,
27 '&': true,
28 "'": true,
29 '(': true,
30 ')': true,
31 '*': true,
32 '+': true,
33 ',': true,
34 '.': true,
35 '/': true,
36 ';': true,
37 '<': true,
38 '=': true,
39 '>': true,
40 '?': true,
41 '@': true,
42 '[': true,
43 '\\': true,
44 ']': true,
45 '^': true,
46 '`': true,
47 '{': true,
48 '|': true,
49 '}': true,
50 '~': true
51};
52exports.stringRenderEscapeChars = {
53 '\n': true,
54 '\r': true,
55 '\t': true,
56 '\f': true,
57 '\v': true
58};
59exports.whitespaceChars = {
60 ' ': true,
61 '\t': true,
62 '\n': true,
63 '\r': true,
64 '\f': true
65};
66exports.quoteChars = {
67 '"': true,
68 "'": true
69};
70exports.digitsChars = {
71 0: true,
72 1: true,
73 2: true,
74 3: true,
75 4: true,
76 5: true,
77 6: true,
78 7: true,
79 8: true,
80 9: true
81};
82exports.maxHexLength = 6;
83function escapeIdentifier(s) {
84 var len = s.length;
85 var result = '';
86 var i = 0;
87 while (i < len) {
88 var chr = s.charAt(i);
89 if (exports.identEscapeChars[chr] || (chr === '-' && i === 1 && s.charAt(0) === '-')) {
90 result += '\\' + chr;
91 }
92 else {
93 if (chr === '-' ||
94 chr === '_' ||
95 (chr >= 'A' && chr <= 'Z') ||
96 (chr >= 'a' && chr <= 'z') ||
97 (chr >= '0' && chr <= '9' && i !== 0 && !(i === 1 && s.charAt(0) === '-'))) {
98 result += chr;
99 }
100 else {
101 var charCode = chr.charCodeAt(0);
102 if ((charCode & 0xf800) === 0xd800) {
103 var extraCharCode = s.charCodeAt(i++);
104 if ((charCode & 0xfc00) !== 0xd800 || (extraCharCode & 0xfc00) !== 0xdc00) {
105 throw Error('UCS-2(decode): illegal sequence');
106 }
107 charCode = ((charCode & 0x3ff) << 10) + (extraCharCode & 0x3ff) + 0x10000;
108 }
109 result += '\\' + charCode.toString(16) + ' ';
110 }
111 }
112 i++;
113 }
114 return result.trim();
115}
116exports.escapeIdentifier = escapeIdentifier;
117function escapeString(s) {
118 var len = s.length;
119 var result = '';
120 var i = 0;
121 while (i < len) {
122 var chr = s.charAt(i);
123 if (chr === '"') {
124 chr = '\\"';
125 }
126 else if (chr === '\\') {
127 chr = '\\\\';
128 }
129 else if (exports.stringRenderEscapeChars[chr]) {
130 chr = '\\' + chr.charCodeAt(0).toString(16) + (i === len - 1 ? '' : ' ');
131 }
132 result += chr;
133 i++;
134 }
135 return "\"".concat(result, "\"");
136}
137exports.escapeString = escapeString;