Skip to content

Commit

Permalink
fix: \\n should be changed to \n only when escapeSpecialCharacters is…
Browse files Browse the repository at this point in the history
… set (#91)

This PR proposes a fix to the issue #90.

## PR Checklist

- [x] Addresses an existing open issue: fixes #90
- [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
  • Loading branch information
fuenfundachtzig authored Apr 10, 2024
1 parent e634f0e commit 4c133c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/dedent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,8 @@ describe("dedent", () => {
it("works with escaped tabs for indentation", () => {
expect(dedent("\t\tfirst\n\t\t\tsecond\n\t\t\t\tthird")).toMatchSnapshot();
});

it("does not replace \\n when called as a function", () => {
expect(dedent(`\\nu`)).toBe("\\nu");
});
});
15 changes: 8 additions & 7 deletions src/dedent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ function createDedent(options: DedentOptions) {
.join("\n");
}

return (
result
// dedent eats leading and trailing whitespace too
.trim()
// handle escaped newlines at the end to ensure they don't get stripped too
.replace(/\\n/g, "\n")
);
// dedent eats leading and trailing whitespace too
result = result.trim();
if (escapeSpecialCharacters) {
// handle escaped newlines at the end to ensure they don't get stripped too
result = result.replace(/\\n/g, "\n");
}

return result;
}
}

0 comments on commit 4c133c1

Please sign in to comment.