-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
130 lines (112 loc) · 3.08 KB
/
helpers.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var operators = {};
var helpers = {
operators: operators
};
operators.is = function() {
var types = Array.prototype.slice.call(arguments);
return types.indexOf(this.type) !== -1;
};
operators.is_dialogue = function() {
return this.is("character", "parenthetical", "dialogue");
};
operators.name = function() {
var character = this.text;
var p = character.indexOf("(");
if (p !== -1) {
character = character.substring(0, p);
}
character = character.trim();
return character;
};
operators.location = function() {
var location = this.text.trim();
location = location.replace(/^(INT\.?\/EXT\.?)|(I\/E)|(INT\.?)|(EXT\.?)/, "");
var dash = location.lastIndexOf(" - ");
if (dash !== -1) {
location = location.substring(0, dash);
}
return location.trim();
};
operators.has_scene_time = function(time) {
var suffix = this.text.substring(this.text.indexOf(" - "));
return this.is("scene_heading") && suffix.indexOf(time) !== -1;
};
operators.location_type = function() {
var location = this.text.trim();
if (/^I(NT.?)?\/E(XT.?)?/.test(location)) {
return "mixed";
}
else if (/^INT.?/.test(location)) {
return "int";
}
else if (/^EXT.?/.test(location)) {
return "ext";
}
return "other";
};
var enrich_token = function(token) {
for (var name in operators) {
token[name] = operators[name];
}
return token;
};
var create_token_delegator = function(line, name) {
return function() {
return line.token ? line.token[name].apply(line.token, arguments) : null;
};
};
var create_fquery_delegator = function(name) {
return function() {
var args = arguments;
return function(item) {
return item[name].apply(item, args);
};
};
};
helpers.fq = {};
for (var name in operators) {
helpers.fq[name] = create_fquery_delegator(name);
}
var enrich_line = function(line) {
for (var name in operators) {
line[name] = create_token_delegator(line, name);
}
return line;
};
helpers.first_text = function(type, list, default_value) {
for (var i = 0; i < list.length; i++) {
if (list[i].type === type) {
return list[i].text;
}
}
return default_value;
};
helpers.create_line = function(line) {
line.text = line.text || "";
line.type = line.type || "unknown";
line.start = line.start || 0;
line.end = line.end || 0;
line.token = line.token || helpers.create_token({
type: line.type
});
line.token.lines = line.token.lines || [line];
return enrich_line(line);
};
helpers.create_token = function(token) {
token.text = token.text || "";
token.type = token.type || "unknown";
token.start = token.start || 0;
token.end = token.end || 0;
token.lines = token.lines || [];
return enrich_token(token);
};
helpers.create_separator = function(start, end) {
return helpers.create_token({
text: "",
start: start,
end: end,
lines: [""],
type: "separator"
});
};
module.exports = helpers;