UNPKG

11.9 kBMarkdownView Raw
1<h1 align="center">dedent</h1>
2
3<p align="center">A string tag that strips indentation from multi-line strings. ⬅️</p>
4
5<p align="center">
6 <a href="#contributors" target="_blank">
7<!-- prettier-ignore-start -->
8<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
9<img alt="All Contributors: 18 πŸ‘ͺ" src="https://img.shields.io/badge/all_contributors-18_πŸ‘ͺ-21bb42.svg" />
10<!-- ALL-CONTRIBUTORS-BADGE:END -->
11<!-- prettier-ignore-end -->
12</a>
13 <a href="https://codecov.io/gh/dmnd/dedent" target="_blank">
14 <img alt="Codecov Test Coverage" src="https://codecov.io/gh/dmnd/dedent/branch/main/graph/badge.svg"/>
15 </a>
16 <a href="https://github.com/dmnd/dedent/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank">
17 <img alt="Contributor Covenant" src="https://img.shields.io/badge/code_of_conduct-enforced-21bb42" />
18 </a>
19 <a href="https://github.com/dmnd/dedent/blob/main/LICENSE.md" target="_blank">
20 <img alt="License: MIT" src="https://img.shields.io/github/license/dmnd/dedent?color=21bb42">
21 </a>
22 <img alt="Style: Prettier" src="https://img.shields.io/badge/style-prettier-21bb42.svg" />
23 <img alt="TypeScript: Strict" src="https://img.shields.io/badge/typescript-strict-21bb42.svg" />
24 <img alt="npm package version" src="https://img.shields.io/npm/v/dedent?color=21bb42" />
25 <img alt="Contributor Covenant" src="https://img.shields.io/badge/code_of_conduct-enforced-21bb42" />
26</p>
27
28## Usage
29
30```shell
31npm i dedent
32```
33
34```js
35import dedent from "dedent";
36
37function usageExample() {
38 const first = dedent`A string that gets so long you need to break it over
39 multiple lines. Luckily dedent is here to keep it
40 readable without lots of spaces ending up in the string
41 itself.`;
42
43 const second = dedent`
44 Leading and trailing lines will be trimmed, so you can write something like
45 this and have it work as you expect:
46
47 * how convenient it is
48 * that I can use an indented list
49 - and still have it do the right thing
50
51 That's all.
52 `;
53
54 const third = dedent(`
55 Wait! I lied. Dedent can also be used as a function.
56 `);
57
58 return first + "\n\n" + second + "\n\n" + third;
59}
60
61console.log(usageExample());
62```
63
64```plaintext
65A string that gets so long you need to break it over
66multiple lines. Luckily dedent is here to keep it
67readable without lots of spaces ending up in the string
68itself.
69
70Leading and trailing lines will be trimmed, so you can write something like
71this and have it work as you expect:
72
73 * how convenient it is
74 * that I can use an indented list
75 - and still have it do the right thing
76
77That's all.
78
79Wait! I lied. Dedent can also be used as a function.
80```
81
82## Options
83
84You can customize the options `dedent` runs with by calling its `withOptions` method with an object:
85
86<!-- prettier-ignore -->
87```js
88import dedent from 'dedent';
89
90dedent.withOptions({ /* ... */ })`input`;
91dedent.withOptions({ /* ... */ })(`input`);
92```
93
94`options` returns a new `dedent` function, so if you'd like to reuse the same options, you can create a dedicated `dedent` function:
95
96<!-- prettier-ignore -->
97```js
98import dedent from 'dedent';
99
100const dedenter = dedent.withOptions({ /* ... */ });
101
102dedenter`input`;
103dedenter(`input`);
104```
105
106### `escapeSpecialCharacters`
107
108JavaScript string tags by default add an extra `\` escape in front of some special characters such as `$` dollar signs.
109`dedent` will escape those special characters when called as a string tag.
110
111If you'd like to change the behavior, an `escapeSpecialCharacters` option is available.
112It defaults to:
113
114- `false`: when `dedent` is called as a function
115- `true`: when `dedent` is called as a string tag
116
117```js
118import dedent from "dedent";
119
120// "$hello!"
121dedent`
122 $hello!
123`;
124
125// "\$hello!"
126dedent.withOptions({ escapeSpecialCharacters: false })`
127 $hello!
128`;
129
130// "$hello!"
131dedent.withOptions({ escapeSpecialCharacters: true })`
132 $hello!
133`;
134```
135
136For more context, see [πŸš€ Feature: Add an option to disable special character escaping](https://github.com/dmnd/dedent/issues/63).
137
138## License
139
140MIT
141
142## Contributors
143
144<!-- spellchecker: disable -->
145<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
146<!-- prettier-ignore-start -->
147<!-- markdownlint-disable -->
148<table>
149 <tbody>
150 <tr>
151 <td align="center" valign="top" width="14.28%"><a href="https://adrianjost.dev/"><img src="https://avatars.githubusercontent.com/u/22987140?v=4?s=100" width="100px;" alt="Adrian Jost"/><br /><sub><b>Adrian Jost</b></sub></a><br /><a href="https://github.com/dmnd/dedent/commits?author=adrianjost" title="Code">πŸ’»</a></td>
152 <td align="center" valign="top" width="14.28%"><a href="https://m811.com/"><img src="https://avatars.githubusercontent.com/u/156837?v=4?s=100" width="100px;" alt="Andri MΓΆll"/><br /><sub><b>Andri MΓΆll</b></sub></a><br /><a href="https://github.com/dmnd/dedent/issues?q=author%3Amoll" title="Bug reports">πŸ›</a></td>
153 <td align="center" valign="top" width="14.28%"><a href="https://bennypowers.dev/"><img src="https://avatars.githubusercontent.com/u/1466420?v=4?s=100" width="100px;" alt="Benny Powers - גם Χ™Χ©Χ¨ΧΧœ Χ—Χ™!"/><br /><sub><b>Benny Powers - גם Χ™Χ©Χ¨ΧΧœ Χ—Χ™!</b></sub></a><br /><a href="#tool-bennypowers" title="Tools">πŸ”§</a></td>
154 <td align="center" valign="top" width="14.28%"><a href="https://github.com/phenomnomnominal"><img src="https://avatars.githubusercontent.com/u/1086286?v=4?s=100" width="100px;" alt="Craig Spence"/><br /><sub><b>Craig Spence</b></sub></a><br /><a href="https://github.com/dmnd/dedent/commits?author=phenomnomnominal" title="Code">πŸ’»</a></td>
155 <td align="center" valign="top" width="14.28%"><a href="https://synthesis.com/"><img src="https://avatars.githubusercontent.com/u/4427?v=4?s=100" width="100px;" alt="Desmond Brand"/><br /><sub><b>Desmond Brand</b></sub></a><br /><a href="https://github.com/dmnd/dedent/issues?q=author%3Admnd" title="Bug reports">πŸ›</a> <a href="https://github.com/dmnd/dedent/commits?author=dmnd" title="Code">πŸ’»</a> <a href="https://github.com/dmnd/dedent/commits?author=dmnd" title="Documentation">πŸ“–</a> <a href="#ideas-dmnd" title="Ideas, Planning, & Feedback">πŸ€”</a> <a href="#infra-dmnd" title="Infrastructure (Hosting, Build-Tools, etc)">πŸš‡</a> <a href="#maintenance-dmnd" title="Maintenance">🚧</a> <a href="#projectManagement-dmnd" title="Project Management">πŸ“†</a> <a href="#tool-dmnd" title="Tools">πŸ”§</a></td>
156 <td align="center" valign="top" width="14.28%"><a href="https://github.com/G-Rath"><img src="https://avatars.githubusercontent.com/u/3151613?v=4?s=100" width="100px;" alt="Gareth Jones"/><br /><sub><b>Gareth Jones</b></sub></a><br /><a href="https://github.com/dmnd/dedent/commits?author=G-Rath" title="Code">πŸ’»</a> <a href="https://github.com/dmnd/dedent/issues?q=author%3AG-Rath" title="Bug reports">πŸ›</a></td>
157 <td align="center" valign="top" width="14.28%"><a href="https://github.com/otakustay"><img src="https://avatars.githubusercontent.com/u/639549?v=4?s=100" width="100px;" alt="Gray Zhang"/><br /><sub><b>Gray Zhang</b></sub></a><br /><a href="https://github.com/dmnd/dedent/issues?q=author%3Aotakustay" title="Bug reports">πŸ›</a></td>
158 </tr>
159 <tr>
160 <td align="center" valign="top" width="14.28%"><a href="https://haroen.me/"><img src="https://avatars.githubusercontent.com/u/6270048?v=4?s=100" width="100px;" alt="Haroen Viaene"/><br /><sub><b>Haroen Viaene</b></sub></a><br /><a href="https://github.com/dmnd/dedent/commits?author=Haroenv" title="Code">πŸ’»</a> <a href="#maintenance-Haroenv" title="Maintenance">🚧</a></td>
161 <td align="center" valign="top" width="14.28%"><a href="https://blog.cometkim.kr/"><img src="https://avatars.githubusercontent.com/u/9696352?v=4?s=100" width="100px;" alt="Hyeseong Kim"/><br /><sub><b>Hyeseong Kim</b></sub></a><br /><a href="#tool-cometkim" title="Tools">πŸ”§</a> <a href="#infra-cometkim" title="Infrastructure (Hosting, Build-Tools, etc)">πŸš‡</a></td>
162 <td align="center" valign="top" width="14.28%"><a href="https://github.com/jlarmstrongiv"><img src="https://avatars.githubusercontent.com/u/20903247?v=4?s=100" width="100px;" alt="John L. Armstrong IV"/><br /><sub><b>John L. Armstrong IV</b></sub></a><br /><a href="https://github.com/dmnd/dedent/issues?q=author%3Ajlarmstrongiv" title="Bug reports">πŸ›</a></td>
163 <td align="center" valign="top" width="14.28%"><a href="http://www.joshuakgoldberg.com/"><img src="https://avatars.githubusercontent.com/u/3335181?v=4?s=100" width="100px;" alt="Josh Goldberg ✨"/><br /><sub><b>Josh Goldberg ✨</b></sub></a><br /><a href="https://github.com/dmnd/dedent/issues?q=author%3AJoshuaKGoldberg" title="Bug reports">πŸ›</a> <a href="https://github.com/dmnd/dedent/commits?author=JoshuaKGoldberg" title="Code">πŸ’»</a> <a href="https://github.com/dmnd/dedent/commits?author=JoshuaKGoldberg" title="Documentation">πŸ“–</a> <a href="#ideas-JoshuaKGoldberg" title="Ideas, Planning, & Feedback">πŸ€”</a> <a href="#infra-JoshuaKGoldberg" title="Infrastructure (Hosting, Build-Tools, etc)">πŸš‡</a> <a href="#maintenance-JoshuaKGoldberg" title="Maintenance">🚧</a> <a href="#projectManagement-JoshuaKGoldberg" title="Project Management">πŸ“†</a> <a href="#tool-JoshuaKGoldberg" title="Tools">πŸ”§</a></td>
164 <td align="center" valign="top" width="14.28%"><a href="https://pratapvardhan.com/"><img src="https://avatars.githubusercontent.com/u/3757165?v=4?s=100" width="100px;" alt="Pratap Vardhan"/><br /><sub><b>Pratap Vardhan</b></sub></a><br /><a href="https://github.com/dmnd/dedent/commits?author=pratapvardhan" title="Code">πŸ’»</a></td>
165 <td align="center" valign="top" width="14.28%"><a href="https://github.com/lydell"><img src="https://avatars.githubusercontent.com/u/2142817?v=4?s=100" width="100px;" alt="Simon Lydell"/><br /><sub><b>Simon Lydell</b></sub></a><br /><a href="https://github.com/dmnd/dedent/issues?q=author%3Alydell" title="Bug reports">πŸ›</a></td>
166 <td align="center" valign="top" width="14.28%"><a href="https://github.com/yinm"><img src="https://avatars.githubusercontent.com/u/13295106?v=4?s=100" width="100px;" alt="Yusuke Iinuma"/><br /><sub><b>Yusuke Iinuma</b></sub></a><br /><a href="https://github.com/dmnd/dedent/commits?author=yinm" title="Code">πŸ’»</a></td>
167 </tr>
168 <tr>
169 <td align="center" valign="top" width="14.28%"><a href="https://github.com/yvele"><img src="https://avatars.githubusercontent.com/u/4225430?v=4?s=100" width="100px;" alt="Yves M."/><br /><sub><b>Yves M.</b></sub></a><br /><a href="#tool-yvele" title="Tools">πŸ”§</a></td>
170 <td align="center" valign="top" width="14.28%"><a href="https://github.com/d07RiV"><img src="https://avatars.githubusercontent.com/u/3448203?v=4?s=100" width="100px;" alt="d07riv"/><br /><sub><b>d07riv</b></sub></a><br /><a href="https://github.com/dmnd/dedent/issues?q=author%3Ad07RiV" title="Bug reports">πŸ›</a></td>
171 <td align="center" valign="top" width="14.28%"><a href="https://mizdra.net/"><img src="https://avatars.githubusercontent.com/u/9639995?v=4?s=100" width="100px;" alt="mizdra"/><br /><sub><b>mizdra</b></sub></a><br /><a href="https://github.com/dmnd/dedent/commits?author=mizdra" title="Code">πŸ’»</a></td>
172 <td align="center" valign="top" width="14.28%"><a href="https://github.com/sirian"><img src="https://avatars.githubusercontent.com/u/897643?v=4?s=100" width="100px;" alt="sirian"/><br /><sub><b>sirian</b></sub></a><br /><a href="https://github.com/dmnd/dedent/issues?q=author%3Asirian" title="Bug reports">πŸ›</a></td>
173 </tr>
174 </tbody>
175</table>
176
177<!-- markdownlint-restore -->
178<!-- prettier-ignore-end -->
179
180<!-- ALL-CONTRIBUTORS-LIST:END -->
181<!-- spellchecker: enable -->
182
183> πŸ’™ This package was templated with [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).