Merge pull request #748 from 30-seconds/indentString

Add indentString
This commit is contained in:
Angelos Chalaris
2018-09-27 16:58:31 +03:00
committed by GitHub
4 changed files with 32 additions and 0 deletions

16
snippets/indentString.md Normal file
View File

@ -0,0 +1,16 @@
### indentString
Indents each line in the provided string.
Use `String.replace` and a regular expression to add the character specified by `indent` `count` times at the start of each line.
Omit the third parameter, `indent`, to use a default indentation character of `' '`.
```js
const indentString = (str, count, indent = ' ') =>
str.replace(/^/mg, indent.repeat(count));
```
```js
indentString('Lorem\nIpsum', 2); // ' Lorem\n Ipsum'
indentString('Lorem\nIpsum', 2, '_'); // '__Lorem\n__Ipsum'
```