Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame^] | 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 | }; |