blob: e1ab804f2fd8a15f93c6556fed470aa87c5a37e3 [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001'use strict';
2
3module.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};