-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute.js
108 lines (87 loc) · 2.34 KB
/
compute.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
// require the file
var VDDC = require ("./RO/json/verses")
, Books = require ("./RO/json/books")
, Verses = []
;
/*
* This function searches returns the objects from an array
* that contains objects
*
* {
* site_id: "61",
* name: "18"
* }
*
* */
function findQuery (array, query) {
// get the collection
var col = array
, res = []
;
// array empty or not valid
if (!col || !col.length || col.constructor !== Array) {
return res;
}
// start dance
itemsToFindForLoop:
// each item
for (var i = 0; i < col.length; ++i) {
// get current item
var cItem = col[i];
// each filter from query
for (var f in query) {
// get filter value
var fValue = query[f];
if (typeof cItem[f] === "string" && typeof fValue === "string") {
// a filter doesn't match to the query
if (cItem[f] !== fValue) continue itemsToFindForLoop;
} else if (typeof cItem[f] === "string" && fValue && fValue.constructor === Array) {
// a filter doesn't match to the query
if (fValue.indexOf(cItem[f]) === -1) continue itemsToFindForLoop;
}
}
// item matches to the query, push it
res.push(cItem);
}
// return array
return res;
}
/**
* private: getBookId
* Returns the book id providing the book name as the first parameter.
*
*/
function getBookName (id, books) {
// search for book name
var book = findQuery (books, {
id: id
})[0];
// not found
if (!book || !book.id) {
return null;
}
// return book id
return book.book;
}
// each object
for (var i = 0; i < VDDC.length; ++i) {
// get the current verse
var cVDDC = VDDC[i];
// save only verses
if (cVDDC.type !== "SCR") continue;
// remove HTML tags
cVDDC.text = cVDDC.text
.replace(/<\/?[^>]+(>|$)/g, " ")
.replace(/\*/g, "")
.replace(/ /g, " ")
.trim();
// push the new verse
Verses.push({
bookname: getBookName (cVDDC.book, Books)
, chapter: cVDDC.chapter
, verse: cVDDC.verse
, text: cVDDC.text
});
}
// write the fixed json
require("fs").writeFileSync("./RO/json/verses.json", JSON.stringify(Verses, null, 4));