Skip to content

Commit 86902f7

Browse files
feat: add trimWhitespace option (#97)
Adds a `trimWhitespace` option which decides if we trim the leading and trailing whitespace of the result. Draft until the issue is accepting PRs ## PR Checklist - [x] Addresses an existing open issue: fixes #96 - [x] That issue was marked as [`status: accepting prs`](https://github.com/dmnd/dedent/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/dmnd/dedent/blob/main/.github/CONTRIBUTING.md) were taken --------- Co-authored-by: Josh Goldberg ✨ <[email protected]>
1 parent 90644fe commit 86902f7

File tree

5 files changed

+118
-3
lines changed

5 files changed

+118
-3
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ dedent.withOptions({ escapeSpecialCharacters: true })`
135135

136136
For more context, see [🚀 Feature: Add an option to disable special character escaping](https://github.com/dmnd/dedent/issues/63).
137137

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+
138163
## License
139164

140165
MIT

src/__snapshots__/dedent.test.ts.snap

+46
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,52 @@ exports[`dedent string tag character escapes with escapeSpecialCharacters undefi
9696
9797
exports[`dedent string tag character escapes with escapeSpecialCharacters undefined opening braces 1`] = `"{"`;
9898
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+
99145
exports[`dedent works with blank first line 1`] = `
100146
"Some text that I might want to indent:
101147
* reasons

src/dedent.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,43 @@ describe("dedent", () => {
152152
);
153153
});
154154

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+
155192
describe("string tag character escapes", () => {
156193
describe("default behavior", () => {
157194
it("escapes backticks", () => {

src/dedent.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ function createDedent(options: DedentOptions) {
1919
...values: unknown[]
2020
) {
2121
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;
2326

2427
// first, perform interpolation
2528
let result = "";
@@ -69,9 +72,12 @@ function createDedent(options: DedentOptions) {
6972
}
7073

7174
// 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
7380
if (escapeSpecialCharacters) {
74-
// handle escaped newlines at the end to ensure they don't get stripped too
7581
result = result.replace(/\\n/g, "\n");
7682
}
7783

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface DedentOptions {
22
escapeSpecialCharacters?: boolean;
3+
trimWhitespace?: boolean;
34
}
45

56
export interface Dedent {

0 commit comments

Comments
 (0)