format-lines.js 424 B

12345678910111213141516
  1. function formatLines (...lines) {
  2. return [...indentEach(0, lines)].join('\n') + '\n';
  3. }
  4. function *indentEach (indent, lines) {
  5. for (const line of lines) {
  6. if (Array.isArray(line)) {
  7. yield * indentEach(indent + 1, line);
  8. } else {
  9. const padding = ' '.repeat(indent);
  10. yield * line.split('\n').map(subline => subline === '' ? '' : padding + subline);
  11. }
  12. }
  13. }
  14. module.exports = formatLines;