File tree 5 files changed +118
-3
lines changed
5 files changed +118
-3
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,31 @@ dedent.withOptions({ escapeSpecialCharacters: true })`
135
135
136
136
For more context, see [ 🚀 Feature: Add an option to disable special character escaping] ( https://github.com/dmnd/dedent/issues/63 ) .
137
137
138
+ ### ` trimWhitespace `
139
+
140
+ By default, dedent will trim leading and trailing whitespace from the overall string.
141
+
142
+ This can be disabled by setting ` trimWhitespace: false ` .
143
+
144
+ ``` js
145
+ import dedent from " dedent" ;
146
+
147
+ // "hello!"
148
+ dedent`
149
+ hello!
150
+ ` ;
151
+
152
+ // "\nhello! \n"
153
+ dedent .withOptions ({ trimWhitespace: false })`
154
+ hello!
155
+ ` ;
156
+
157
+ // "hello!"
158
+ dedent .withOptions ({ trimWhitespace: true })`
159
+ hello!
160
+ ` ;
161
+ ```
162
+
138
163
## License
139
164
140
165
MIT
Original file line number Diff line number Diff line change @@ -96,6 +96,52 @@ exports[`dedent string tag character escapes with escapeSpecialCharacters undefi
96
96
97
97
exports [` dedent string tag character escapes with escapeSpecialCharacters undefined opening braces 1` ] = ` "{ " `;
98
98
99
+ exports [` dedent with trimWhitespace false with leading whitespace 1` ] = `
100
+ "
101
+
102
+
103
+ foo
104
+ "
105
+ ` ;
106
+
107
+ exports [` dedent with trimWhitespace false with trailing whitespace 1` ] = `
108
+ "
109
+ foo
110
+ bar
111
+ "
112
+ ` ;
113
+
114
+ exports [` dedent with trimWhitespace false without trailing whitespace 1` ] = `
115
+ "
116
+ foo
117
+ bar
118
+ "
119
+ ` ;
120
+
121
+ exports [` dedent with trimWhitespace true with leading whitespace 1` ] = ` "foo"` ;
122
+
123
+ exports [` dedent with trimWhitespace true with trailing whitespace 1` ] = `
124
+ "foo
125
+ bar"
126
+ ` ;
127
+
128
+ exports [` dedent with trimWhitespace true without trailing whitespace 1` ] = `
129
+ "foo
130
+ bar"
131
+ ` ;
132
+
133
+ exports [` dedent with trimWhitespace undefined with leading whitespace 1` ] = ` "foo"` ;
134
+
135
+ exports [` dedent with trimWhitespace undefined with trailing whitespace 1` ] = `
136
+ "foo
137
+ bar"
138
+ ` ;
139
+
140
+ exports [` dedent with trimWhitespace undefined without trailing whitespace 1` ] = `
141
+ "foo
142
+ bar"
143
+ ` ;
144
+
99
145
exports [` dedent works with blank first line 1` ] = `
100
146
"Some text that I might want to indent:
101
147
* reasons
Original file line number Diff line number Diff line change @@ -152,6 +152,43 @@ describe("dedent", () => {
152
152
) ;
153
153
} ) ;
154
154
155
+ describe . each ( [ undefined , false , true ] ) (
156
+ "with trimWhitespace %s" ,
157
+ ( trimWhitespace ) => {
158
+ test ( "with trailing whitespace" , ( ) => {
159
+ expect (
160
+ dedent . withOptions ( { trimWhitespace } ) (
161
+ `
162
+ foo---
163
+ bar---
164
+ ` . replace ( / - / g, " " ) ,
165
+ ) ,
166
+ ) . toMatchSnapshot ( ) ;
167
+ } ) ;
168
+
169
+ test ( "without trailing whitespace" , ( ) => {
170
+ expect (
171
+ dedent . withOptions ( { trimWhitespace } ) (
172
+ `
173
+ foo
174
+ bar
175
+ ` . replace ( / - / g, " " ) ,
176
+ ) ,
177
+ ) . toMatchSnapshot ( ) ;
178
+ } ) ;
179
+
180
+ test ( "with leading whitespace" , ( ) => {
181
+ expect (
182
+ dedent . withOptions ( { trimWhitespace } ) ( `
183
+
184
+
185
+ foo
186
+ ` ) ,
187
+ ) . toMatchSnapshot ( ) ;
188
+ } ) ;
189
+ } ,
190
+ ) ;
191
+
155
192
describe ( "string tag character escapes" , ( ) => {
156
193
describe ( "default behavior" , ( ) => {
157
194
it ( "escapes backticks" , ( ) => {
Original file line number Diff line number Diff line change @@ -19,7 +19,10 @@ function createDedent(options: DedentOptions) {
19
19
...values : unknown [ ]
20
20
) {
21
21
const raw = typeof strings === "string" ? [ strings ] : strings . raw ;
22
- const { escapeSpecialCharacters = Array . isArray ( strings ) } = options ;
22
+ const {
23
+ escapeSpecialCharacters = Array . isArray ( strings ) ,
24
+ trimWhitespace = true ,
25
+ } = options ;
23
26
24
27
// first, perform interpolation
25
28
let result = "" ;
@@ -69,9 +72,12 @@ function createDedent(options: DedentOptions) {
69
72
}
70
73
71
74
// dedent eats leading and trailing whitespace too
72
- result = result . trim ( ) ;
75
+ if ( trimWhitespace ) {
76
+ result = result . trim ( ) ;
77
+ }
78
+
79
+ // handle escaped newlines at the end to ensure they don't get stripped too
73
80
if ( escapeSpecialCharacters ) {
74
- // handle escaped newlines at the end to ensure they don't get stripped too
75
81
result = result . replace ( / \\ n / g, "\n" ) ;
76
82
}
77
83
Original file line number Diff line number Diff line change 1
1
export interface DedentOptions {
2
2
escapeSpecialCharacters ?: boolean ;
3
+ trimWhitespace ?: boolean ;
3
4
}
4
5
5
6
export interface Dedent {
You can’t perform that action at this time.
0 commit comments