-
Notifications
You must be signed in to change notification settings - Fork 4
/
makeDB.js
98 lines (87 loc) · 2.07 KB
/
makeDB.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
const fs = require("fs");
const categories = require("./categories");
const sounds = require("./sounds");
let DB = {};
fs.readdir(`${__dirname}/Hymns`, (err, files) => {
for (file of files) {
if (file.indexOf(".DS") == -1) {
const content = fs.readFileSync(`Hymns/${file}`, "utf8");
const chorus = getChorus(content);
const verses = getVerses(content);
const number = getHymnNumber(file);
const title = getHymnTitle(file);
const titleWithHymnNumber = getTitleWithHymnNumber(file);
const category = getCategory(Number(number));
const sound = getSound(Number(number));
// ADD HYMNS
DB = {
...DB,
hymns: {
...DB.hymns,
[number]: {
number,
title,
titleWithHymnNumber,
chorus,
verses,
sound,
category
}
}
};
}
}
// MAKE CATEGORIES
DB = {
...DB,
categories: categories
};
fs.writeFile(`${__dirname}/content/db.json`, JSON.stringify(DB), error => {
if (!error) console.log("CREATED DATABASE");
});
});
function getChorus(hymnText) {
const matches = [...hymnText.matchAll(/(?: )(?:.*)(?:\r\n|\r|\n)/gm)];
return matches.length == 0
? false
: matches
.join("")
.split(" ")
.join("")
.trim();
}
function getVerses(content) {
const x = content.split("\n\n");
return x.filter((x, i) => i !== 1);
}
function getHymnNumber(fileName) {
return fileName.split(" ")[0].trim();
}
function getTitleWithHymnNumber(fileName) {
return fileName
.split(".")
.slice(0, 1)
.join(" ")
.trim();
}
function getCategory(hymnNumber) {
for ([name, hymns] of Object.entries(categories)) {
if (hymns.includes(hymnNumber)) {
return name;
}
}
return "NOT FOUND";
}
function getHymnTitle(fileName) {
return fileName
.split(".")
.slice(0, 1)
.join(" ")
.split(" ")
.slice(1)
.join(" ")
.trim();
}
function getSound(hymnNumber) {
return sounds[hymnNumber] ? sounds[hymnNumber] : "";
}