forked from WycliffeAssociates/analyze-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
102 lines (88 loc) · 1.95 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
const books = require('./data/books.json');
const subjects = require('./data/subjects.json');
const contentOrder = require('./data/content_order.json');
/**
* Returns the piece of string that comes after the last dot. Don't trust this
* to get the real file type.
*/
function getFileFormat(fileName) {
return fileName.split('.').reverse()[0];
}
/**
*
*/
function getZipContent(formatString) {
const isZip = /application\/zip/.exec(formatString);
if (!isZip) {
return '';
}
// formatString will contain, for example: ... content=text/usfm
const content = /content=[\w]+\/([\w]+)/.exec(formatString);
if (!content) {
return '';
}
return content[1];
}
/**
*
*/
function flattenOnce(arrayOfArrays) {
return arrayOfArrays.reduce((all, array) => all.concat(array), []);
}
/**
*
*/
function removeProperty(object, property) {
const newObject = Object.assign({}, object);
delete newObject[property];
return newObject;
}
/**
*
*/
function orderContent(contents) {
const orderedContents = [];
contents.forEach((content) => {
const order = contentOrder[content.code];
let offset = 0;
if (order >= 0) {
orderedContents[order + offset] = content;
} else {
orderedContents.unshift(content);
offset += 1;
}
});
// Filter out the empty in-between spaces
return orderedContents.filter(content => content);
}
/**
*
*/
function getCategory(bookCode) {
const code = bookCode.toLowerCase();
return (books[code] && `bible-${books[code].anth}`) || '';
}
/**
*
*/
function normalizeSubject(subject) {
const subjectLowercase = subject.toLowerCase();
return subjects[subjectLowercase] || subject;
}
/**
*
*/
function getBookSortOrder(bookCode) {
const code = bookCode.toLowerCase();
return (books[code] && books[code]["num"]) || 0;
}
module.exports = {
getFileFormat,
getZipContent,
orderContent,
getCategory,
normalizeSubject,
getBookSortOrder,
removeProperty,
flattenOnce,
};