Add indentString
This commit is contained in:
16
snippets/indentString.md
Normal file
16
snippets/indentString.md
Normal 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'
|
||||
```
|
||||
@ -113,6 +113,7 @@ httpGet:utility,url,browser,intermediate
|
||||
httpPost:utility,url,browser,intermediate
|
||||
httpsRedirect:browser,url,intermediate
|
||||
hz:function,intermediate
|
||||
indentString:string,utility,beginner
|
||||
indexOfAll:array,intermediate
|
||||
initial:array,beginner
|
||||
initialize2DArray:array,intermediate
|
||||
|
||||
3
test/indentString/indentString.js
Normal file
3
test/indentString/indentString.js
Normal file
@ -0,0 +1,3 @@
|
||||
const indentString = (str, count, indent = ' ') =>
|
||||
str.replace(/^/mg, indent.repeat(count));
|
||||
module.exports = indentString;
|
||||
12
test/indentString/indentString.test.js
Normal file
12
test/indentString/indentString.test.js
Normal file
@ -0,0 +1,12 @@
|
||||
const expect = require('expect');
|
||||
const indentString = require('./indentString.js');
|
||||
|
||||
test('indentString is a Function', () => {
|
||||
expect(indentString).toBeInstanceOf(Function);
|
||||
});
|
||||
test('indentString is a Function', () => {
|
||||
expect(indentString('Lorem\nIpsum', 2)).toBe(' Lorem\n Ipsum');
|
||||
});
|
||||
test('indentString is a Function', () => {
|
||||
expect(indentString('Lorem\nIpsum', 2, '_')).toBe('__Lorem\n__Ipsum');
|
||||
});
|
||||
Reference in New Issue
Block a user