forked from WycliffeAssociates/analyze-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
387 lines (351 loc) · 11 KB
/
functions.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
const {
getFileFormat,
getZipContent,
getCategory,
normalizeSubject,
getBookSortOrder,
removeProperty,
flattenOnce,
} = require('./helpers');
function mapLanguages(languages) {
return languages.map(lang => ({
name: lang.title,
code: lang.identifier,
direction: lang.direction,
contents: lang.resources,
}));
}
function mapContents(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(content => ({
name: overrideContentTitle(content.title),
code: content.identifier,
subject: normalizeSubject(content.subject),
description: content.description,
checkingLevel: content.checking.checking_level,
links: content.formats,
subcontents: content.projects,
})),
}));
}
const contentTitleOverrides = require('./data/content_title_overrides.json');
function overrideContentTitle(title) {
if (title in contentTitleOverrides) {
return contentTitleOverrides[title];
} else {
return title;
}
}
function mapContentLinks(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(c => ({
...c,
links: c.links && c.links.length > 0
? c.links.map(link => ({
url: link.url,
format: getFileFormat(link.url),
zipContent: getZipContent(link.format),
quality: link.quality || null,
}))
: [],
})),
}));
}
const contentFilters = require('./data/content_filters.json');
function filterContents(languages) {
return languages.map(l => ({
...l,
contents: l.contents.filter((c) => {
// If the langauge and content codes exist in the filter, filter out
return !(
// e.g. "en": [ "obs" ]
(l.code in contentFilters && contentFilters[l.code].includes(c.code)) ||
// e.g. "_all": [ "obs" ]
("_all" in contentFilters && contentFilters["_all"].includes(c.code))
);
})
}));
}
function filterEmptySubcontents(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(c => ({
...c,
subcontents: c.subcontents && c.subcontents.length > 0
? c.subcontents.filter((s) => {
const hasLinks = s.links && s.links.length > 0;
return hasLinks;
})
: [],
})),
}));
}
function filterEmptyContents(languages) {
return languages.map(l => ({
...l,
contents: l.contents.filter((c) => {
linksExist = c.links && c.links.length > 0;
subcontentsExist = c.subcontents && c.subcontents.length > 0;
return linksExist || subcontentsExist;
})
}));
}
function filterContentLinks(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(c => ({
...c,
links: c.links && c.links.length > 0 ? c.links.filter((link) => {
// Special case: Per MS, only list download links for reg, ulb, udb
if (link.format === 'Download') {
const regDownload = c.code.startsWith("reg");
const ulbDownload = c.code.startsWith("ulb");
const udbDownload = c.code.startsWith("udb");
return regDownload || ulbDownload || udbDownload
}
return true;
})
: [],
})),
}));
}
function mapSubcontents(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(c => ({
...c,
subcontents: c.subcontents && c.subcontents.length > 0
? c.subcontents.map(s => ({
name: s.title,
code: s.identifier,
// Use bracket notation because .sort is an array function.
// eslint-disable-next-line dot-notation
sort: s['sort'],
category: getCategory(s.identifier),
links: s.formats,
}))
: [],
})),
}));
}
function filterSubcontents(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(c => ({
...c,
subcontents: c.subcontents && c.subcontents.length > 0
? c.subcontents.filter((s) => {
const hasLinks = s.links && s.links.length > 0;
// Special cases: Deborah wants them not to appear
const notTAIntro = !(c.code === 'ta' && s.code === 'intro');
const notTAProcessManual = !(c.code === 'ta' && s.code === 'process');
const notTACheckingManual = !(c.code === 'ta' && s.code === 'checking');
return hasLinks && notTAIntro && notTAProcessManual && notTACheckingManual;
})
: [],
})),
}));
}
function mapSubcontentLinks(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(c => ({
...c,
subcontents: c.subcontents && c.subcontents.length > 0
? c.subcontents.map(s => ({
...s,
links: s.links && s.links.length > 0
? s.links.map(link => ({
url: link.url,
format: getFileFormat(link.url),
zipContent: getZipContent(link.format),
quality: link.quality || null,
// OBS subcontent has, oddly, chapters inside each links.
chapters: link.chapters || [],
}))
: [],
}))
: [],
})),
}));
}
function filterSubcontentLinks(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(c => ({
...c,
subcontents: c.subcontents && c.subcontents.length > 0
? c.subcontents.map(s => ({
...s,
links: s.links && s.links.length > 0 ? s.links.filter((link) => {
// Special case: Per MS, only list download links for reg, ulb, udb
if (link.format === 'Download') {
const regDownload = c.code.startsWith("reg");
const ulbDownload = c.code.startsWith("ulb");
const udbDownload = c.code.startsWith("udb");
return regDownload || ulbDownload || udbDownload
}
return true;
})
: [],
}))
: [],
})),
}));
}
function addEnglishNames(langNameData, languages) {
return languages.map((l) => {
const data = langNameData.find(lang => lang.lc === l.code);
return {
...l,
englishName: (data && data.ang) || '',
};
});
}
function sortLanguageByNameOrEnglishName(languages) {
return languages.sort((first, second) => {
let nameOfFirst = first.englishName || first.name;
let nameOfSecond = second.englishName || second.name;
// "Normalize" strings for sorting by replacing accented characters with plain ones
// Adapted from: https://stackoverflow.com/a/37511463
nameOfFirst = nameOfFirst.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
nameOfSecond = nameOfSecond.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
if (nameOfFirst === nameOfSecond) {
return 0;
}
return nameOfFirst < nameOfSecond ? -1 : 1;
});
}
function sortContents(contentOrder, languages) {
return languages.map(l => ({
...l,
contents: l.contents.sort((first, second) => {
const orderOfFirst = contentOrder[first.code] || 100;
const orderOfSecond = contentOrder[second.code] || 100;
if (orderOfFirst === orderOfSecond) {
return 0;
}
return orderOfFirst < orderOfSecond ? -1 : 1;
}),
}));
}
function sortSubContents(languages) {
return languages.map(l => ({
...l,
contents: l.contents.map(c => ({
...c,
subcontents: (c.subcontents || []).sort((first, second) => {
// If sort field is provided use that
const sortOrderOfFirst = first.sort;
const sortOrderOfSecond = second.sort;
if (sortOrderOfFirst !== sortOrderOfSecond) {
return sortOrderOfFirst < sortOrderOfSecond ? -1 : 1;
}
// Else lookup book sort order
const bookSortOrderOfFirst = getBookSortOrder(first.code);
const bookSortOrderOfSecond = getBookSortOrder(second.code);
if (bookSortOrderOfFirst === bookSortOrderOfSecond) {
return 0;
}
return bookSortOrderOfFirst < bookSortOrderOfSecond ? -1 : 1;
})
})),
}));
}
function unnestSubcontents(languages) {
const subcontentsToPromote = ['obs', 'obs-tn', 'obs-tq', 'tw'];
return languages.map(l => ({
...l,
contents: l.contents.map(c => {
if (!subcontentsToPromote.includes(c.code) || !c.subcontents || c.subcontents.length <= 0) {
return c;
}
const chosenSubcontent = c.subcontents[0];
return {
...c,
name: c.name || chosenSubcontent.name,
links: (c.links || [])
.concat(chosenSubcontent.links)
.map(l => removeProperty(l, 'chapters')),
subcontents: c.code !== 'obs'
? c.subcontents.slice(1)
: turnChaptersToSubcontents(c.subcontents),
};
}),
}));
}
function turnChaptersToSubcontents(obsProjects) {
const mergedChapters = obsProjects.map(p => {
// Just pick the chapters and quality from the links, if there are any
const links = p.links.map(l => ({
chapters: l.chapters || [],
quality: l.quality || null,
}));
// Inject quality into each element of the chapters
const chapters = links.map(l => ({
chapters: l.chapters.map(c => ({
...c,
quality: l.quality,
})),
}));
// Put all chapter objects into one array
const chapterArrays = chapters.reduce((array, c) => array.concat(c.chapters), []);
// Merge chapters that have the same code/identifier
const dedupedChapters = chapterArrays.reduce((array, c) => {
let format = '';
if (c.format) {
format = c.format.split('/').reverse()[0];
}
// Map chapter into BIEL-recognized form
const currentChapter = {
name: c.identifier,
category: 'obs',
links: [{
url: c.url,
format: format,
quality: c.quality,
}],
};
// Check for existing chapter with the same name
const existingChapterIndex = array.findIndex(item => item.name === currentChapter.name);
if (existingChapterIndex === -1) {
array.push(currentChapter);
} else {
const existingChapter = array[existingChapterIndex];
array[existingChapterIndex] = {
...existingChapter,
links: existingChapter.links.concat(currentChapter.links),
};
}
return array;
}, []);
const sortedChapters = dedupedChapters.sort((first, second) => {
if (first.name === second.name) {
return 0;
}
return first.name < second.name ? -1 : 1;
});
return sortedChapters;
});
return flattenOnce(mergedChapters);
}
module.exports = {
mapLanguages,
mapContents,
mapContentLinks,
filterContentLinks,
filterContents,
filterEmptySubcontents,
filterEmptyContents,
mapSubcontents,
filterSubcontents,
mapSubcontentLinks,
filterSubcontentLinks,
addEnglishNames,
sortLanguageByNameOrEnglishName,
sortContents,
sortSubContents,
unnestSubcontents,
};