2016-10-10 03:43:44 +02:00
|
|
|
'use strict';
|
2017-08-14 05:01:11 +02:00
|
|
|
module.exports = (str, count, opts) => {
|
|
|
|
// Support older versions: use the third parameter as options.indent
|
|
|
|
// TODO: Remove the workaround in the next major version
|
|
|
|
const options = typeof opts === 'object' ? Object.assign({indent: ' '}, opts) : {indent: opts || ' '};
|
2017-05-27 17:36:13 +02:00
|
|
|
count = count === undefined ? 1 : count;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
if (typeof str !== 'string') {
|
|
|
|
throw new TypeError(`Expected \`input\` to be a \`string\`, got \`${typeof str}\``);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 17:36:13 +02:00
|
|
|
if (typeof count !== 'number') {
|
|
|
|
throw new TypeError(`Expected \`count\` to be a \`number\`, got \`${typeof count}\``);
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if (typeof options.indent !== 'string') {
|
|
|
|
throw new TypeError(`Expected \`options.indent\` to be a \`string\`, got \`${typeof options.indent}\``);
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count === 0) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const regex = options.includeEmptyLines ? /^/mg : /^(?!\s*$)/mg;
|
|
|
|
return str.replace(regex, options.indent.repeat(count));
|
|
|
|
}
|
|
|
|
;
|