-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcfrp-schema.js
229 lines (201 loc) · 5.81 KB
/
cfrp-schema.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* CFRP categories and formatting
*
* Copyright (c) 2015 MIT Hyperstudio
* Christopher York, 06/2015
*
*/
const d3 = require('d3')
import {fr_spec, fr, en_spec, en} from './js/util/i18n'
const subscript = (range, items) => {
var result = range.map( (i) => items.map( (item) => item + "_" + i ) )
return result.reduce( (a, b) => a.concat(b) )
}
const playbills = ['n'].concat(d3.range(1,4))
const schema = {
performance:
subscript(playbills,
[ "title"]),
time:
[ "decade",
"season",
"month",
// TODO. day yields 40,000 results -- query latency unacceptable
// "day",
"weekday" ],
author:
subscript(playbills,
[ "author"]),
genre:
subscript(playbills,
[ "genre"]),
// seating_area:
// subscript(playbills,
// [ "seating_area"]),
seating_area:
[ "seating_area" ],
acts:
subscript(playbills,
[ "acts"]),
// prose_vers:
// subscript(playbills,
// [ "prose_vers" ]),
/*performance_addl:
subscript(playbills,
[ "prologue",
"musique_danse_machine",
"free_entry",
"reprise",
"firstrun",
"newactor",
"debut" ,
"ex_attendance",
"ex_representation",
"ex_place" ]) */
}
const public_aggregates = [
"sum_receipts",
// "sum_receipts_weighted",
"performances_days",
"mean_receipts_day",
// "mean_receipts_day_weighted",
"mean_price" ]
function group() {
return schema
}
function dimension() {
return d3.values(schema).reduce( (a,b) => a.concat(b) )
}
function aggregate() {
return public_aggregates
}
function parse(field) {
// only necessary for non-text fields received via AJAX/CSV
switch(true) {
case /^decade(_.*)?/.test(field):
return parseInt
case /^weekday(_.*)?/.test(field):
return parseInt
case /^acts(_.*)?/.test(field):
return parseInt
case /^prologue(_.*)?/.test(field):
return parseBool
case /^musique_danse_machine(_.*)?/.test(field):
return parseBool
case /^free_entry(_.*)?/.test(field):
return parseBool
case /^reprise(_.*)?/.test(field):
return parseBool
case /^firstrun(_.*)?/.test(field):
return parseBool
// aggregates
case /sum_receipts/.test(field):
return parseFloat
case /sum_receipts_weighted/.test(field):
return parseFloat
case /performances_days/.test(field):
return parseInt
case /mean_receipts_day/.test(field):
return parseFloat
case /mean_receipts_day_weighted/.test(field):
return parseFloat
case /mean_price/.test(field):
return parseFloat
case /count_authors_(\d)/.test(field):
return parseInt
case /count_titles_(\d)/.test(field):
return parseInt
}
return (x) => x
function parseBool(s) {
switch(s) {
case 't': return true
case 'f': return false
default: return null
}
}
}
function format(lang, field, len) {
// TODO logic could be clearer... some much easier in ruby
// lets us use the same logic for both languages
var spec = (lang === 'en') ? en_spec : fr_spec
var fmt = (lang === 'en') ? en : fr
switch(true) {
// dimensions
case /^decade(_.*)?/.test(field):
return (i) => +i // [i, +i+9].join(' - ')
case /^month(_.*)?/.test(field):
// NB. months and weekdays are kept in 1-indexed format (like postgresql; unlike javascript)
return (i) => (i === null) ? "" : spec.months[parseInt(i)-1]
case /^day(_.*)?/.test(field):
return fmt.timeFormat("%a %d %b %Y")
case /^weekday(_.*)?/.test(field):
// NB. months and weekdays are kept in 1-indexed format (like postgresql; unlike javascript)
return (i) => (i === null) ? "" : spec.days[+i-1]
case /^prologue(_.*)?/.test(field):
return formatBool
case /^musique_danse_machine(_.*)?/.test(field):
return formatBool
case /^free_entry(_.*)?/.test(field):
return formatBool
case /^reprise(_.*)?/.test(field):
return formatBool
case /^firstrun(_.*)?/.test(field):
return formatBool
// aggregates
case /sum_receipts/.test(field):
return fmt.numberFormat(",.2f")
case /sum_receipts_weighted/.test(field):
return fmt.numberFormat(",f")
case /performances_days/.test(field):
return fmt.numberFormat(",f")
case /mean_receipts_day/.test(field):
return fmt.numberFormat(",.2f")
case /mean_receipts_day_weighted/.test(field):
return fmt.numberFormat(",f")
case /mean_price/.test(field):
return fmt.numberFormat(",.2f")
case /count_authors_(\d)/.test(field):
return fmt.numberFormat(",f")
case /count_titles_(\d)/.test(field):
return fmt.numberFormat(",f")
}
/* When len is set, returns the earlier of
(1) a 'hard' delimiter: parens, semicolon, comma
(2) the first 'soft' (space) delimiter after the len characters
TODO move into an editable short value in the database ? */
return (x) => {
if(!x) return ""
x = "" + x
if(!len) return x
let hard = x.search(/\s*[(,;]/)
hard = (hard > -1) ? hard : Infinity
let soft = x.indexOf(' ', len)
soft = (soft > -1) ? soft : Infinity
return x.slice(0, Math.min(hard, soft))
}
function formatBool(b) {
if (b === true) { return lang === 'en' ? 'yes' : 'oui' }
if (b === false) { return lang === 'en' ? 'no' : 'non' }
if (b === "undefined") { return ""; }
return b;
}
}
function filter(dim, val) {
switch(true) {
case /^acts(_.*)?/.test(dim):
const value = parseInt(val);
return isNaN(value) ? true : value % 2;
}
return val === val;
}
function sort(vals) {
if(vals && vals.hasOwnProperty("sort") && vals.length > 0) {
if(typeof vals[0] === "number") {
vals.sort(function(a, b) { return a - b; });
} else {
vals.sort();
}
}
}
export { group, dimension, aggregate, parse, format, sort, filter };