/
parsePubSuffixList.js
52 lines (44 loc) · 1.36 KB
/
parsePubSuffixList.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
40
41
42
43
44
45
46
47
48
49
50
51
52
"use strict";
const matchNewLine = /\r?\n/;
const matchComments = /^\s*\/\//;
const matchWhitespace = /^\s*$/;
const lists = [
{
name: "icann",
markers: {
start: "// ===BEGIN ICANN DOMAINS===",
end: "// ===END ICANN DOMAINS===",
},
},
{
name: "private",
markers: {
start: "// ===BEGIN PRIVATE DOMAINS===",
end: "// ===END PRIVATE DOMAINS===",
},
},
];
function isWanted(line) {
return matchComments.test(line) === false && matchWhitespace.test(line) === false;
}
function parsePubSuffixList(listContent) {
return lists
.map(list => {
const start = listContent.indexOf(list.markers.start);
const end = listContent.indexOf(list.markers.end);
if (start === -1) {
throw new Error(`Missing start marker of ${ list.name } list`);
}
if (end === -1) {
throw new Error(`Missing end marker of ${ list.name } list`);
}
return listContent.slice(start, end);
})
.map(listContent => listContent.split(matchNewLine).filter(isWanted))
.reduce((result, lines, i) => {
const listName = lists[i].name;
result[listName] = lines;
return result;
}, {});
}
module.exports = parsePubSuffixList;