Skip to content

Commit 6eea13c

Browse files
feat: add options with escapeSpecialCharacters (#65)
Fixes #26. Fixes #63. As described in #63, this introduces the concept of options with single option, `escapeSpecialCharacters`. The option defaults to: * `true` when called for a template literal's string tag * `false` when called as a function ```js // "\$hello!" dedent.options({ escapeSpecialCharacters: false })` $hello! `; ``` I'd played with allowing passing it in as a first argument instead of a string or template literal strings array, but that got complex. I suppose we can do that as a followup if people really want. cc @G-Rath @sirian - what do you think?
1 parent 97d6cc0 commit 6eea13c

File tree

5 files changed

+340
-88
lines changed

5 files changed

+340
-88
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,62 @@ That's all.
5454
Wait! I lied. Dedent can also be used as a function.
5555
```
5656

57+
## Options
58+
59+
You can customize the options `dedent` runs with by calling its `withOptions` method with an object:
60+
61+
<!-- prettier-ignore -->
62+
```js
63+
import dedent from 'dedent';
64+
65+
dedent.withOptions({ /* ... */ })`input`;
66+
dedent.withOptions({ /* ... */ })(`input`);
67+
```
68+
69+
`options` returns a new `dedent` function, so if you'd like to reuse the same options, you can create a dedicated `dedent` function:
70+
71+
<!-- prettier-ignore -->
72+
```js
73+
import dedent from 'dedent';
74+
75+
const dedenter = dedent.withOptions({ /* ... */ });
76+
77+
dedenter`input`;
78+
dedenter(`input`);
79+
```
80+
81+
### `escapeSpecialCharacters`
82+
83+
JavaScript string tags by default add an extra `\` escape in front of some special characters such as `$` dollar signs.
84+
`dedent` will escape those special characters when called as a string tag.
85+
86+
If you'd like to change the behavior, an `escapeSpecialCharacters` option is available.
87+
It defaults to:
88+
89+
- `false`: when `dedent` is called as a function
90+
- `true`: when `dedent` is called as a string tag
91+
92+
```js
93+
import dedent from "dedent";
94+
95+
// "$hello!"
96+
dedent`
97+
$hello!
98+
`;
99+
100+
// "\$hello!"
101+
dedent.withOptions({ escapeSpecialCharacters: false })`
102+
$hello!
103+
`;
104+
105+
// "$hello!"
106+
dedent.withOptions({ escapeSpecialCharacters: true })`
107+
$hello!
108+
`;
109+
```
110+
111+
For more context, see [https://github.com/dmnd/dedent/issues/63](🚀 Feature: Add an option to disable special character escaping).
112+
57113
## License
58114

59115
MIT

__tests__/__snapshots__/dedent-tests.ts.snap

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,100 @@
22

33
exports[`dedent can be used as a function 1`] = `"A test argument."`;
44

5-
exports[`dedent doesn't strip exlicit newlines 1`] = `
5+
exports[`dedent doesn't strip explicit newlines 1`] = `
66
"<p>Hello world!</p>
77
"
88
`;
99

10-
exports[`dedent doesn't strip exlicit newlines with mindent 1`] = `
10+
exports[`dedent doesn't strip explicit newlines with mindent 1`] = `
1111
"<p>
1212
Hello world!
1313
</p>
1414
"
1515
`;
1616

17-
exports[`dedent escapes backticks 1`] = `"\`"`;
17+
exports[`dedent function character escapes default behavior does not escape backticks 1`] = `"\`"`;
1818

19-
exports[`dedent escapes dollar signs 1`] = `"$"`;
19+
exports[`dedent function character escapes default behavior does not escape dollar signs 1`] = `"$"`;
2020

21-
exports[`dedent escapes opening braces 1`] = `"{"`;
21+
exports[`dedent function character escapes default behavior does not escape opening braces 1`] = `"{"`;
2222
23-
exports[`dedent ignores closing braces 1`] = `"\\}"`;
23+
exports[`dedent function character escapes default behavior escapes double-escaped backticks 1`] = `"\\\`"`;
24+
25+
exports[`dedent function character escapes default behavior escapes double-escaped dollar signs 1`] = `"\\$"`;
26+
27+
exports[`dedent function character escapes default behavior escapes double-escaped opening braces 1`] = `"\\{"`;
28+
29+
exports[`dedent function character escapes default behavior ignores closing braces 1`] = `"}"`;
30+
31+
exports[`dedent function character escapes with escapeSpecialCharacters false backticks 1`] = `"\`"`;
32+
33+
exports[`dedent function character escapes with escapeSpecialCharacters false dollar signs 1`] = `"$"`;
34+
35+
exports[`dedent function character escapes with escapeSpecialCharacters false double-escaped backticks 1`] = `"\\\`"`;
36+
37+
exports[`dedent function character escapes with escapeSpecialCharacters false double-escaped dollar signs 1`] = `"\\$"`;
38+
39+
exports[`dedent function character escapes with escapeSpecialCharacters false double-escaped opening braces 1`] = `"\\{"`;
40+
41+
exports[`dedent function character escapes with escapeSpecialCharacters false opening braces 1`] = `"{"`;
42+
43+
exports[`dedent function character escapes with escapeSpecialCharacters true backticks 1`] = `"\`"`;
44+
45+
exports[`dedent function character escapes with escapeSpecialCharacters true dollar signs 1`] = `"$"`;
46+
47+
exports[`dedent function character escapes with escapeSpecialCharacters true double-escaped backticks 1`] = `"\`"`;
48+
49+
exports[`dedent function character escapes with escapeSpecialCharacters true double-escaped dollar signs 1`] = `"$"`;
50+
51+
exports[`dedent function character escapes with escapeSpecialCharacters true double-escaped opening braces 1`] = `"{"`;
52+
53+
exports[`dedent function character escapes with escapeSpecialCharacters true opening braces 1`] = `"{"`;
54+
55+
exports[`dedent function character escapes with escapeSpecialCharacters undefined backticks 1`] = `"\`"`;
56+
57+
exports[`dedent function character escapes with escapeSpecialCharacters undefined dollar signs 1`] = `"$"`;
58+
59+
exports[`dedent function character escapes with escapeSpecialCharacters undefined double-escaped backticks 1`] = `"\\\`"`;
60+
61+
exports[`dedent function character escapes with escapeSpecialCharacters undefined double-escaped dollar signs 1`] = `"\\$"`;
62+
63+
exports[`dedent function character escapes with escapeSpecialCharacters undefined double-escaped opening braces 1`] = `"\\{"`;
64+
65+
exports[`dedent function character escapes with escapeSpecialCharacters undefined opening braces 1`] = `"{"`;
2466
2567
exports[`dedent single line input works with single line and closing backtick on newline 1`] = `"A single line of input."`;
2668
2769
exports[`dedent single line input works with single line and inline closing backtick 1`] = `"A single line of input."`;
2870
2971
exports[`dedent single line input works with single line input 1`] = `"A single line of input."`;
3072
73+
exports[`dedent string tag character escapes default behavior escapes backticks 1`] = `"\`"`;
74+
75+
exports[`dedent string tag character escapes default behavior escapes dollar signs 1`] = `"$"`;
76+
77+
exports[`dedent string tag character escapes default behavior escapes opening braces 1`] = `"{"`;
78+
79+
exports[`dedent string tag character escapes default behavior ignores closing braces 1`] = `"\\}"`;
80+
81+
exports[`dedent string tag character escapes with escapeSpecialCharacters false backticks 1`] = `"\\\`"`;
82+
83+
exports[`dedent string tag character escapes with escapeSpecialCharacters false dollar signs 1`] = `"\\$"`;
84+
85+
exports[`dedent string tag character escapes with escapeSpecialCharacters false opening braces 1`] = `"\\{"`;
86+
87+
exports[`dedent string tag character escapes with escapeSpecialCharacters true backticks 1`] = `"\`"`;
88+
89+
exports[`dedent string tag character escapes with escapeSpecialCharacters true dollar signs 1`] = `"$"`;
90+
91+
exports[`dedent string tag character escapes with escapeSpecialCharacters true opening braces 1`] = `"{"`;
92+
93+
exports[`dedent string tag character escapes with escapeSpecialCharacters undefined backticks 1`] = `"\`"`;
94+
95+
exports[`dedent string tag character escapes with escapeSpecialCharacters undefined dollar signs 1`] = `"$"`;
96+
97+
exports[`dedent string tag character escapes with escapeSpecialCharacters undefined opening braces 1`] = `"{"`;
98+
3199
exports[`dedent works with blank first line 1`] = `
32100
"Some text that I might want to indent:
33101
* reasons

0 commit comments

Comments
 (0)