stringifyComment.js 801 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. /**
  3. * Stringifies a comment.
  4. *
  5. * Empty comment lines are left empty,
  6. * lines consisting of a single space are replaced by `#`,
  7. * and all other lines are prefixed with a `#`.
  8. */
  9. const stringifyComment = (str) => str.replace(/^(?!$)(?: $)?/gm, '#');
  10. function indentComment(comment, indent) {
  11. if (/^\n+$/.test(comment))
  12. return comment.substring(1);
  13. return indent ? comment.replace(/^(?! *$)/gm, indent) : comment;
  14. }
  15. const lineComment = (str, indent, comment) => str.endsWith('\n')
  16. ? indentComment(comment, indent)
  17. : comment.includes('\n')
  18. ? '\n' + indentComment(comment, indent)
  19. : (str.endsWith(' ') ? '' : ' ') + comment;
  20. exports.indentComment = indentComment;
  21. exports.lineComment = lineComment;
  22. exports.stringifyComment = stringifyComment;