mirror of
https://github.com/nodejs/node.git
synced 2025-05-13 08:56:32 +00:00

PR-URL: https://github.com/nodejs/node/pull/35474 Reviewed-By: Ruy Adorno <ruyadorno@github.com> Reviewed-By: Ujjwal Sharma <ryzokuken@disroot.org> Reviewed-By: Ben Coe <bencoe@gmail.com> Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Shelley Vohr <codebytere@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
36 lines
743 B
JavaScript
36 lines
743 B
JavaScript
'use strict';
|
|
|
|
module.exports = (string, count = 1, options) => {
|
|
options = {
|
|
indent: ' ',
|
|
includeEmptyLines: false,
|
|
...options
|
|
};
|
|
|
|
if (typeof string !== 'string') {
|
|
throw new TypeError(
|
|
`Expected \`input\` to be a \`string\`, got \`${typeof string}\``
|
|
);
|
|
}
|
|
|
|
if (typeof count !== 'number') {
|
|
throw new TypeError(
|
|
`Expected \`count\` to be a \`number\`, got \`${typeof count}\``
|
|
);
|
|
}
|
|
|
|
if (typeof options.indent !== 'string') {
|
|
throw new TypeError(
|
|
`Expected \`options.indent\` to be a \`string\`, got \`${typeof options.indent}\``
|
|
);
|
|
}
|
|
|
|
if (count === 0) {
|
|
return string;
|
|
}
|
|
|
|
const regex = options.includeEmptyLines ? /^/gm : /^(?!\s*$)/gm;
|
|
|
|
return string.replace(regex, options.indent.repeat(count));
|
|
};
|