|
1 | 1 | const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
|
2 | 2 | const reserved = /^(?:do|if|in|for|int|let|new|try|var|byte|case|char|else|enum|goto|long|this|void|with|await|break|catch|class|const|final|float|short|super|throw|while|yield|delete|double|export|import|native|return|switch|throws|typeof|boolean|default|extends|finally|package|private|abstract|continue|debugger|function|volatile|interface|protected|transient|implements|instanceof|synchronized)$/;
|
3 |
| - |
4 |
| -function getName(num: number) { |
5 |
| - let name = ''; |
6 |
| - |
7 |
| - do { |
8 |
| - name = chars[num % chars.length] + name; |
9 |
| - num = ~~(num / chars.length) - 1; |
10 |
| - } while (num >= 0); |
11 |
| - |
12 |
| - return reserved.test(name) ? `${name}_` : name; |
13 |
| -} |
| 3 | +const unsafe = /[<>\/\u2028\u2029]/g; |
| 4 | +const escaped: Record<string, string> = { '<': '\\u003C', '>' : '\\u003E', '/': '\\u002F', '\u2028': '\\u2028', '\u2029': '\\u2029' }; |
14 | 5 |
|
15 | 6 | export default function devalue(value: any) {
|
16 | 7 | const repeated = new Map();
|
@@ -173,12 +164,27 @@ export default function devalue(value: any) {
|
173 | 164 | }
|
174 | 165 | }
|
175 | 166 |
|
| 167 | +function getName(num: number) { |
| 168 | + let name = ''; |
| 169 | + |
| 170 | + do { |
| 171 | + name = chars[num % chars.length] + name; |
| 172 | + num = ~~(num / chars.length) - 1; |
| 173 | + } while (num >= 0); |
| 174 | + |
| 175 | + return reserved.test(name) ? `${name}_` : name; |
| 176 | +} |
| 177 | + |
176 | 178 | function isPrimitive(thing: any) {
|
177 | 179 | return Object(thing) !== thing;
|
178 | 180 | }
|
179 | 181 |
|
| 182 | +function escape(char: string) { |
| 183 | + return escaped[char]; |
| 184 | +} |
| 185 | + |
180 | 186 | function stringifyPrimitive(thing: any) {
|
181 |
| - if (typeof thing === 'string') return JSON.stringify(thing); |
| 187 | + if (typeof thing === 'string') return JSON.stringify(thing).replace(unsafe, escape); |
182 | 188 | if (thing === void 0) return 'void 0';
|
183 | 189 | if (thing === 0 && 1 / thing < 0) return '-0';
|
184 | 190 | return String(thing);
|
|
0 commit comments