-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.2.js
executable file
·59 lines (47 loc) · 1.07 KB
/
2.2.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
53
54
55
56
57
58
59
function contains(list, item) {
return list.indexOf(item) >= 0;
}
function nodesByTypes(types, ast) {
return ast.filter(function (node) {
return contains(types, node[0]);
});
}
function second(list) {
return list[1];
}
function addedFlags(ast) {
return nodesByTypes(['add-flags'], ast).map(second);
}
function splitBy(predicate, list) {
var result = [];
list.forEach(function (elem) {
if (predicate(elem)) {
result.push([]);
return result;
} else if (result.length === 0) {
result.push([]);
}
result[result.length - 1].push(elem);
});
return result;
}
function isParagraph(node) {
return node[0] === 'paragraph';
}
function makeParagraph(entries) {
return entries.map(second).join('\n');
}
function prepareView(page) {
return splitBy(
isParagraph,
nodesByTypes(['plainText', 'paragraph'], page.ast)
).map(makeParagraph);
}
var ast = [
['plainText', 'You are in a'],
['plainText', 'cold and dark place'],
['paragraph'],
['paragraph'],
['plainText', 'You are cold']
];
console.log(prepareView({ast: ast}));