UNPKG

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