-
Notifications
You must be signed in to change notification settings - Fork 1
/
4.1.js
executable file
·78 lines (63 loc) · 1.48 KB
/
4.1.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function nth(n, list) {
return list[n];
}
function partial(fn) {
var args = [].slice.call(arguments, 1);
return function () {
var allArgs = args.concat([].slice.call(arguments));
return fn.apply(null, allArgs);
};
}
var second = partial(nth, 1);
function addedFlags(ast) {
return nodesByTypes(['add-flag'], ast).map(second);
}
function contains(list, item) {
return list.indexOf(item) >= 0;
}
function nodesByTypes(types, ast) {
return ast.filter(function (node) {
return contains(types, node[0]);
});
}
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);
}
console.log(prepareView({
id: 0,
ast: [
['add-flag', 'STARTED_2'],
['paragraph'],
['plainText', 'You are in a vast desert.'],
['paragraph'],
['paragraph'],
['alternative', 1, 'Head north'],
['alternative', 2, 'Head south'],
['alternative', 0, 'Hang around']
]
}));