UNPKG

14.8 kBTypeScriptView Raw
1// Type definitions for jsesc 3.0
2// Project: https://github.com/mathiasbynens/jsesc
3// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
4// Lyanbin <https://github.com/Lyanbin>
5// Colin Reeder <https://github.com/vpzomtrrfrt>
6// BendingBender <https://github.com/BendingBender>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9export = jsesc;
10
11/**
12 * This function takes a value and returns an escaped version of the value where any characters
13 * that are not printable ASCII symbols are escaped using the shortest possible (but valid)
14 * escape sequences for use in JavaScript strings. The first supported value type is strings.
15 * Instead of a string, the value can also be an array, an object, a map, a set, or a buffer.
16 * In such cases, jsesc returns a stringified version of the value where any characters that
17 * are not printable ASCII symbols are escaped in the same way.
18 *
19 * @example
20 * import jsesc = require('jsesc');
21 *
22 * jsesc('Ich ♥ Bücher');
23 * // → 'Ich \\u2665 B\\xFCcher'
24 *
25 * jsesc('foo 𝌆 bar');
26 * // → 'foo \\uD834\\uDF06 bar'
27 *
28 * // Escaping an array
29 * jsesc([
30 * 'Ich ♥ Bücher', 'foo 𝌆 bar'
31 * ]);
32 * // → '[\'Ich \\u2665 B\\xFCcher\',\'foo \\uD834\\uDF06 bar\']'
33 *
34 * // Escaping an object
35 * jsesc({
36 * 'Ich ♥ Bücher': 'foo 𝌆 bar'
37 * });
38 * // → '{\'Ich \\u2665 B\\xFCcher\':\'foo \\uD834\\uDF06 bar\'}'
39 */
40declare function jsesc(argument: any, opts?: jsesc.Opts): string;
41
42declare namespace jsesc {
43 /**
44 * A string representing the semantic version number.
45 */
46 const version: string;
47
48 interface Opts {
49 /**
50 * The value `'single'` for the `quotes` option means that any occurrences of `'` in the input string are
51 * escaped as `\'`, so that the output can be used in a string literal wrapped in single quotes. If you want
52 * to use the output as part of a string literal wrapped in double quotes, set the `quotes` option to
53 * `'double'`. If you want to use the output as part of a template literal (i.e. wrapped in backticks), set
54 * the `quotes` option to `'backtick'`.
55 *
56 * @default 'single'
57 *
58 * @example
59 * import jsesc = require('jsesc');
60 *
61 * jsesc('`Lorem` ipsum "dolor" sit \'amet\' etc.');
62 * // → 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.'
63 *
64 * jsesc('`Lorem` ipsum "dolor" sit \'amet\' etc.', {
65 * quotes: 'single'
66 * });
67 * // → '`Lorem` ipsum "dolor" sit \\\'amet\\\' etc.'
68 * // → "`Lorem` ipsum \"dolor\" sit \\'amet\\' etc."
69 *
70 * jsesc('`Lorem` ipsum "dolor" sit \'amet\' etc.', {
71 * quotes: 'double'
72 * });
73 * // → '`Lorem` ipsum \\"dolor\\" sit \'amet\' etc.'
74 * // → "`Lorem` ipsum \\\"dolor\\\" sit 'amet' etc."
75 *
76 * jsesc('`Lorem` ipsum "dolor" sit \'amet\' etc.', {
77 * quotes: 'backtick'
78 * });
79 * // → '\\`Lorem\\` ipsum "dolor" sit \'amet\' etc.'
80 * // → "\\`Lorem\\` ipsum \"dolor\" sit 'amet' etc."
81 * // → `\\\`Lorem\\\` ipsum "dolor" sit 'amet' etc.`
82 *
83 * // This setting also affects the output for arrays and objects:
84 * jsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {
85 * quotes: 'double'
86 * });
87 * // → '{"Ich \\u2665 B\\xFCcher":"foo \\uD834\\uDF06 bar"}'
88 *
89 * jsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], {
90 * quotes: 'double'
91 * });
92 * // → '["Ich \\u2665 B\\xFCcher","foo \\uD834\\uDF06 bar"]'
93 */
94 quotes?: "single" | "double" | "backtick" | undefined;
95
96 /**
97 * The value `'decimal'` for the `numbers` option means that any numeric values are represented using decimal
98 * integer literals. Other valid options are `binary`, `octal`, and `hexadecimal`, which result in binary
99 * integer literals, octal integer literals, and hexadecimal integer literals, respectively.
100 *
101 * @default 'decimal'
102 *
103 * @example
104 * import jsesc = require('jsesc');
105 *
106 * jsesc(42, {
107 * numbers: 'binary'
108 * });
109 * // → '0b101010'
110 *
111 * jsesc(42, {
112 * numbers: 'octal'
113 * });
114 * // → '0o52'
115 *
116 * jsesc(42, {
117 * numbers: 'decimal'
118 * });
119 * // → '42'
120 *
121 * jsesc(42, {
122 * numbers: 'hexadecimal'
123 * });
124 * // → '0x2A'
125 */
126 numbers?: "binary" | "octal" | "decimal" | "hexadecimal" | undefined;
127
128 /**
129 * When enabled, the output is a valid JavaScript string literal wrapped in quotes. The type of quotes can be
130 * specified through the `quotes` setting.
131 *
132 * @default false (disabled)
133 *
134 * @example
135 * import jsesc = require('jsesc');
136 *
137 * jsesc('Lorem ipsum "dolor" sit \'amet\' etc.', {
138 * quotes: 'single',
139 * wrap: true
140 * });
141 * // → '\'Lorem ipsum "dolor" sit \\\'amet\\\' etc.\''
142 * // → "\'Lorem ipsum \"dolor\" sit \\\'amet\\\' etc.\'"
143 *
144 * jsesc('Lorem ipsum "dolor" sit \'amet\' etc.', {
145 * quotes: 'double',
146 * wrap: true
147 * });
148 * // → '"Lorem ipsum \\"dolor\\" sit \'amet\' etc."'
149 * // → "\"Lorem ipsum \\\"dolor\\\" sit \'amet\' etc.\""
150 */
151 wrap?: boolean | undefined;
152
153 /**
154 * When enabled, any astral Unicode symbols in the input are escaped using
155 * [ECMAScript 6 Unicode code point escape sequences](https://mathiasbynens.be/notes/javascript-escapes#unicode-code-point)
156 * instead of using separate escape sequences for each surrogate half. If backwards compatibility with ES5
157 * environments is a concern, don’t enable this setting. If the `json` setting is enabled, the value for the
158 * `es6` setting is ignored (as if it was `false`).
159 *
160 * @default false (disabled)
161 *
162 * @example
163 * import jsesc = require('jsesc');
164 *
165 * // By default, the `es6` option is disabled:
166 * jsesc('foo 𝌆 bar 💩 baz');
167 * // → 'foo \\uD834\\uDF06 bar \\uD83D\\uDCA9 baz'
168 *
169 * // To explicitly disable it:
170 * jsesc('foo 𝌆 bar 💩 baz', {
171 * es6: false
172 * });
173 * // → 'foo \\uD834\\uDF06 bar \\uD83D\\uDCA9 baz'
174 *
175 * // To enable it:
176 * jsesc('foo 𝌆 bar 💩 baz', {
177 * es6: true
178 * });
179 * // → 'foo \\u{1D306} bar \\u{1F4A9} baz'
180 */
181 es6?: boolean | undefined;
182
183 /**
184 * When enabled, all the symbols in the output are escaped — even printable ASCII symbols.
185 * This setting also affects the output for string literals within arrays and objects.
186 *
187 * @default false (disabled)
188 *
189 * @example
190 * import jsesc = require('jsesc');
191 *
192 * jsesc('lolwat"foo\'bar', {
193 * escapeEverything: true
194 * });
195 * // → '\\x6C\\x6F\\x6C\\x77\\x61\\x74\\"\\x66\\x6F\\x6F\\\'\\x62\\x61\\x72'
196 * // → "\\x6C\\x6F\\x6C\\x77\\x61\\x74\\\"\\x66\\x6F\\x6F\\'\\x62\\x61\\x72"
197 */
198 escapeEverything?: boolean | undefined;
199
200 /**
201 * When enabled, only a limited set of symbols in the output are escaped:
202 *
203 * - U+0000 `\0`
204 * - U+0008 `\b`
205 * - U+0009 `\t`
206 * - U+000A `\n`
207 * - U+000C `\f`
208 * - U+000D `\r`
209 * - U+005C `\\`
210 * - U+2028 `\u2028`
211 * - U+2029 `\u2029`
212 * - whatever symbol is being used for wrapping string literals (based on the `quotes` option)
213 * - [lone surrogates](https://esdiscuss.org/topic/code-points-vs-unicode-scalar-values#content-14)
214 *
215 * Note: with this option enabled, jsesc output is no longer guaranteed to be ASCII-safe.
216 *
217 * @default false (disabled)
218 *
219 * @example
220 * import jsesc = require('jsesc');
221 *
222 * jsesc('foo\u2029bar\nbaz©qux𝌆flops', {
223 * minimal: false
224 * });
225 * // → 'foo\\u2029bar\\nbaz©qux𝌆flops'
226 */
227 minimal?: boolean | undefined;
228
229 /**
230 * When enabled, occurrences of [`</script` and `</style`](https://mathiasbynens.be/notes/etago) in the output
231 * are escaped as `<\/script` and `<\/style`, and [`<!--`](https://mathiasbynens.be/notes/etago#comment-8) is
232 * escaped as `\x3C!--` (or `\u003C!--` when the `json` option is enabled). This setting is useful when jsesc’s
233 * output ends up as part of a `<script>` or `<style>` element in an HTML document.
234 *
235 * @default false (disabled)
236 *
237 * @example
238 * import jsesc = require('jsesc');
239 *
240 * jsesc('foo</script>bar', {
241 * isScriptContext: true
242 * });
243 * // → 'foo<\\/script>bar'
244 */
245 isScriptContext?: boolean | undefined;
246
247 /**
248 * When enabled, the output for arrays and objects is as compact as possible; it’s not formatted nicely.
249 *
250 * @default true (enabled)
251 *
252 * @example
253 * import jsesc = require('jsesc');
254 *
255 * jsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {
256 * compact: true // this is the default
257 * });
258 * // → '{\'Ich \u2665 B\xFCcher\':\'foo \uD834\uDF06 bar\'}'
259 *
260 * jsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {
261 * compact: false
262 * });
263 * // → '{\n\t\'Ich \u2665 B\xFCcher\': \'foo \uD834\uDF06 bar\'\n}'
264 *
265 * jsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], {
266 * compact: false
267 * });
268 * // → '[\n\t\'Ich \u2665 B\xFCcher\',\n\t\'foo \uD834\uDF06 bar\'\n]'
269 */
270 compact?: boolean | undefined;
271
272 /**
273 * When the `compact` setting is disabled (`false`), the value of the `indent` option is used to format the
274 * output for arrays and objects. This setting has no effect on the output for strings.
275 *
276 * @default '\t'
277 *
278 * @example
279 * import jsesc = require('jsesc');
280 *
281 * jsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {
282 * compact: false,
283 * indent: '\t' // this is the default
284 * });
285 * // → '{\n\t\'Ich \u2665 B\xFCcher\': \'foo \uD834\uDF06 bar\'\n}'
286 *
287 * jsesc({ 'Ich ♥ Bücher': 'foo 𝌆 bar' }, {
288 * compact: false,
289 * indent: ' '
290 * });
291 * // → '{\n \'Ich \u2665 B\xFCcher\': \'foo \uD834\uDF06 bar\'\n}'
292 *
293 * jsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], {
294 * compact: false,
295 * indent: ' '
296 * });
297 * // → '[\n \'Ich \u2665 B\xFCcher\',\n\ t\'foo \uD834\uDF06 bar\'\n]'
298 */
299 indent?: string | undefined;
300
301 /**
302 * It represents the current indentation level, i.e. the number of times the value of the `indent` option is
303 * repeated.
304 *
305 * @default 0
306 *
307 * @example
308 * import jsesc = require('jsesc');
309 *
310 * jsesc(['a', 'b', 'c'], {
311 * compact: false,
312 * indentLevel: 1
313 * });
314 * // → '[\n\t\t\'a\',\n\t\t\'b\',\n\t\t\'c\'\n\t]'
315 *
316 * jsesc(['a', 'b', 'c'], {
317 * compact: false,
318 * indentLevel: 2
319 * });
320 * // → '[\n\t\t\t\'a\',\n\t\t\t\'b\',\n\t\t\t\'c\'\n\t\t]'
321 */
322 indentLevel?: number | undefined;
323
324 /**
325 * When enabled, the output is valid JSON.
326 * [Hexadecimal character escape sequences](https://mathiasbynens.be/notes/javascript-escapes#hexadecimal) and
327 * [the `\v` or `\0` escape sequences](https://mathiasbynens.be/notes/javascript-escapes#single) are not used.
328 * Setting `json: true` implies `quotes: 'double', wrap: true, es6: false`, although these values can still be
329 * overridden if needed — but in such cases, the output won’t be valid JSON anymore.
330 *
331 * **Note:** Using this option on objects or arrays that contain non-string values relies on `JSON.stringify()`.
332 * For legacy environments like IE ≤ 7, use [a `JSON` polyfill](http://bestiejs.github.io/json3/).
333 *
334 * @default false (disabled)
335 *
336 * @example
337 * import jsesc = require('jsesc');
338 *
339 * jsesc('foo\x00bar\xFF\uFFFDbaz', {
340 * json: true
341 * });
342 * // → '"foo\\u0000bar\\u00FF\\uFFFDbaz"'
343 *
344 * jsesc({ 'foo\x00bar\xFF\uFFFDbaz': 'foo\x00bar\xFF\uFFFDbaz' }, {
345 * json: true
346 * });
347 * // → '{"foo\\u0000bar\\u00FF\\uFFFDbaz":"foo\\u0000bar\\u00FF\\uFFFDbaz"}'
348 *
349 * jsesc([ 'foo\x00bar\xFF\uFFFDbaz', 'foo\x00bar\xFF\uFFFDbaz' ], {
350 * json: true
351 * });
352 * // → '["foo\\u0000bar\\u00FF\\uFFFDbaz","foo\\u0000bar\\u00FF\\uFFFDbaz"]'
353 *
354 * // Values that are acceptable in JSON but aren’t strings, arrays, or object
355 * // literals can’t be escaped, so they’ll just be preserved:
356 * jsesc([ 'foo\x00bar', [1, '©', { 'foo': true, 'qux': null }], 42 ], {
357 * json: true
358 * });
359 * // → '["foo\\u0000bar",[1,"\\u00A9",{"foo":true,"qux":null}],42]'
360 * // Values that aren’t allowed in JSON are run through `JSON.stringify()`:
361 * jsesc([ undefined, -Infinity ], {
362 * json: true
363 * });
364 * // → '[null,null]'
365 */
366 json?: boolean | undefined;
367
368 /**
369 * When enabled, any alphabetical hexadecimal digits in escape sequences as well as any hexadecimal integer
370 * literals (see the `numbers` option) in the output are in lowercase.
371 *
372 * @default false (disabled)
373 *
374 * @example
375 * import jsesc = require('jsesc');
376 *
377 * jsesc('Ich ♥ Bücher', {
378 * lowercaseHex: true
379 * });
380 * // → 'Ich \\u2665 B\\xfccher'
381 * // ^^
382 *
383 * jsesc(42, {
384 * numbers: 'hexadecimal',
385 * lowercaseHex: true
386 * });
387 * // → '0x2a'
388 * // ^^
389 */
390 lowercaseHex?: boolean | undefined;
391 }
392}