stringifyComment.js 724 B

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