index.js 743 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. module.exports = (string, count = 1, options) => {
  3. options = {
  4. indent: ' ',
  5. includeEmptyLines: false,
  6. ...options
  7. };
  8. if (typeof string !== 'string') {
  9. throw new TypeError(
  10. `Expected \`input\` to be a \`string\`, got \`${typeof string}\``
  11. );
  12. }
  13. if (typeof count !== 'number') {
  14. throw new TypeError(
  15. `Expected \`count\` to be a \`number\`, got \`${typeof count}\``
  16. );
  17. }
  18. if (typeof options.indent !== 'string') {
  19. throw new TypeError(
  20. `Expected \`options.indent\` to be a \`string\`, got \`${typeof options.indent}\``
  21. );
  22. }
  23. if (count === 0) {
  24. return string;
  25. }
  26. const regex = options.includeEmptyLines ? /^/gm : /^(?!\s*$)/gm;
  27. return string.replace(regex, options.indent.repeat(count));
  28. };