finki-main
main
|
Last change
on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
743 bytes
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | module.exports = (string, count = 1, options) => {
|
|---|
| 4 | options = {
|
|---|
| 5 | indent: ' ',
|
|---|
| 6 | includeEmptyLines: false,
|
|---|
| 7 | ...options
|
|---|
| 8 | };
|
|---|
| 9 |
|
|---|
| 10 | if (typeof string !== 'string') {
|
|---|
| 11 | throw new TypeError(
|
|---|
| 12 | `Expected \`input\` to be a \`string\`, got \`${typeof string}\``
|
|---|
| 13 | );
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | if (typeof count !== 'number') {
|
|---|
| 17 | throw new TypeError(
|
|---|
| 18 | `Expected \`count\` to be a \`number\`, got \`${typeof count}\``
|
|---|
| 19 | );
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | if (typeof options.indent !== 'string') {
|
|---|
| 23 | throw new TypeError(
|
|---|
| 24 | `Expected \`options.indent\` to be a \`string\`, got \`${typeof options.indent}\``
|
|---|
| 25 | );
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | if (count === 0) {
|
|---|
| 29 | return string;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | const regex = options.includeEmptyLines ? /^/gm : /^(?!\s*$)/gm;
|
|---|
| 33 |
|
|---|
| 34 | return string.replace(regex, options.indent.repeat(count));
|
|---|
| 35 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.