Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options with escapeSpecialCharacters #65

Merged
merged 2 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,62 @@ That's all.
Wait! I lied. Dedent can also be used as a function.
```

## Options

You can customize the options `dedent` runs with by calling its `withOptions` method with an object:

<!-- prettier-ignore -->
```js
import dedent from 'dedent';

dedent.withOptions({ /* ... */ })`input`;
dedent.withOptions({ /* ... */ })(`input`);
```

`options` returns a new `dedent` function, so if you'd like to reuse the same options, you can create a dedicated `dedent` function:

<!-- prettier-ignore -->
```js
import dedent from 'dedent';

const dedenter = dedent.withOptions({ /* ... */ });

dedenter`input`;
dedenter(`input`);
```

### `escapeSpecialCharacters`

JavaScript string tags by default add an extra `\` escape in front of some special characters such as `$` dollar signs.
`dedent` will escape those special characters when called as a string tag.

If you'd like to change the behavior, an `escapeSpecialCharacters` option is available.
It defaults to:

- `false`: when `dedent` is called as a function
- `true`: when `dedent` is called as a string tag

```js
import dedent from "dedent";

// "$hello!"
dedent`
$hello!
`;

// "\$hello!"
dedent.withOptions({ escapeSpecialCharacters: false })`
$hello!
`;

// "$hello!"
dedent.withOptions({ escapeSpecialCharacters: true })`
$hello!
`;
```

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

## License

MIT
80 changes: 74 additions & 6 deletions __tests__/__snapshots__/dedent-tests.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,100 @@

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

exports[`dedent doesn't strip exlicit newlines 1`] = `
exports[`dedent doesn't strip explicit newlines 1`] = `
"<p>Hello world!</p>
"
`;

exports[`dedent doesn't strip exlicit newlines with mindent 1`] = `
exports[`dedent doesn't strip explicit newlines with mindent 1`] = `
"<p>
Hello world!
</p>
"
`;

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

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

exports[`dedent escapes opening braces 1`] = `"{"`;
exports[`dedent function character escapes default behavior does not escape opening braces 1`] = `"{"`;

exports[`dedent ignores closing braces 1`] = `"\\}"`;
exports[`dedent function character escapes default behavior escapes double-escaped backticks 1`] = `"\\\`"`;

exports[`dedent function character escapes default behavior escapes double-escaped dollar signs 1`] = `"\\$"`;

exports[`dedent function character escapes default behavior escapes double-escaped opening braces 1`] = `"\\{"`;

exports[`dedent function character escapes default behavior ignores closing braces 1`] = `"}"`;

exports[`dedent function character escapes with escapeSpecialCharacters false backticks 1`] = `"\`"`;

exports[`dedent function character escapes with escapeSpecialCharacters false dollar signs 1`] = `"$"`;

exports[`dedent function character escapes with escapeSpecialCharacters false double-escaped backticks 1`] = `"\\\`"`;

exports[`dedent function character escapes with escapeSpecialCharacters false double-escaped dollar signs 1`] = `"\\$"`;

exports[`dedent function character escapes with escapeSpecialCharacters false double-escaped opening braces 1`] = `"\\{"`;

exports[`dedent function character escapes with escapeSpecialCharacters false opening braces 1`] = `"{"`;

exports[`dedent function character escapes with escapeSpecialCharacters true backticks 1`] = `"\`"`;

exports[`dedent function character escapes with escapeSpecialCharacters true dollar signs 1`] = `"$"`;

exports[`dedent function character escapes with escapeSpecialCharacters true double-escaped backticks 1`] = `"\`"`;

exports[`dedent function character escapes with escapeSpecialCharacters true double-escaped dollar signs 1`] = `"$"`;

exports[`dedent function character escapes with escapeSpecialCharacters true double-escaped opening braces 1`] = `"{"`;

exports[`dedent function character escapes with escapeSpecialCharacters true opening braces 1`] = `"{"`;

exports[`dedent function character escapes with escapeSpecialCharacters undefined backticks 1`] = `"\`"`;

exports[`dedent function character escapes with escapeSpecialCharacters undefined dollar signs 1`] = `"$"`;

exports[`dedent function character escapes with escapeSpecialCharacters undefined double-escaped backticks 1`] = `"\\\`"`;

exports[`dedent function character escapes with escapeSpecialCharacters undefined double-escaped dollar signs 1`] = `"\\$"`;

exports[`dedent function character escapes with escapeSpecialCharacters undefined double-escaped opening braces 1`] = `"\\{"`;

exports[`dedent function character escapes with escapeSpecialCharacters undefined opening braces 1`] = `"{"`;

exports[`dedent single line input works with single line and closing backtick on newline 1`] = `"A single line of input."`;

exports[`dedent single line input works with single line and inline closing backtick 1`] = `"A single line of input."`;

exports[`dedent single line input works with single line input 1`] = `"A single line of input."`;

exports[`dedent string tag character escapes default behavior escapes backticks 1`] = `"\`"`;

exports[`dedent string tag character escapes default behavior escapes dollar signs 1`] = `"$"`;

exports[`dedent string tag character escapes default behavior escapes opening braces 1`] = `"{"`;

exports[`dedent string tag character escapes default behavior ignores closing braces 1`] = `"\\}"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters false backticks 1`] = `"\\\`"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters false dollar signs 1`] = `"\\$"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters false opening braces 1`] = `"\\{"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters true backticks 1`] = `"\`"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters true dollar signs 1`] = `"$"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters true opening braces 1`] = `"{"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters undefined backticks 1`] = `"\`"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters undefined dollar signs 1`] = `"$"`;

exports[`dedent string tag character escapes with escapeSpecialCharacters undefined opening braces 1`] = `"{"`;

exports[`dedent works with blank first line 1`] = `
"Some text that I might want to indent:
* reasons
Expand Down
Loading