-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecomcon.js
39 lines (34 loc) · 973 Bytes
/
ecomcon.js
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
37
38
39
// ecomcon.js
// Douglas Crockford
// 2018-08-08
const rx_crlf = /\n|\r\n?/;
const rx_ecomcon = /^\/\/([a-zA-Z0-9_]+)\u0020?(.*)$/;
//. Capturing groups:
//. [1] The enabled comment tag
//. [2] The rest of the line
const rx_tag = /^[a-zA-Z0-9_]+$/;
export default Object.freeze(function ecomcon(source_string, tag_array) {
const tag = Object.create(null);
tag_array.forEach(
function (string) {
if (!rx_tag.test(string)) {
throw new Error("ecomcon: " + string);
}
tag[string] = true;
}
);
return source_string.split(rx_crlf).map(
function (line) {
const array = line.match(rx_ecomcon);
return (
Array.isArray(array)
? (
tag[array[1]] === true
? array[2] + "\n"
: ""
)
: line + "\n"
);
}
).join("");
});