1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// SPDX-License-Identifier: 0BSD
export function extractHeaderComments(code: string) {
const comments: string[] = [];
let currChar;
let nextChar;
let offset = 0;
while (offset < code.length) {
currChar = code[offset];
nextChar = code[offset + 1];
if (currChar + nextChar === "//") {
const commentStart = offset + 2;
const commentEnd = code.indexOf("\n", commentStart);
const comment = code.substring(commentStart, commentEnd).trim();
comments.push(comment);
offset = commentEnd + 2;
} else if (currChar + nextChar === "/*") {
const commentStart = offset + 2;
const commentEnd = code.indexOf("*/", commentStart);
const comment = code.substring(commentStart, commentEnd)
.split(/\r?\n/)
.map(line => line.substring(line.indexOf("*") + 1).trim())
.join("\n");
comments.push(comment);
offset = commentEnd + 2;
} else if (/\s/.test(currChar)) {
offset += 1;
} else {
break;
}
}
return comments;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
March 6, 2024