-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetscriptures.js
More file actions
144 lines (141 loc) · 2.67 KB
/
getscriptures.js
File metadata and controls
144 lines (141 loc) · 2.67 KB
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
const bcv_parser = require("bible-passage-reference-parser/js/en_bcv_parser").bcv_parser;
const bookIds = {
Gen: 1,
Exod: 2,
Lev: 3,
Num: 4,
Deut: 5,
Josh: 6,
Judg: 7,
Ruth: 8,
"1Sam": 9,
"2Sam": 10,
"1Kgs": 11,
"2Kgs": 12,
"1Chr": 13,
"2Chr": 14,
Ezra: 15,
Neh: 16,
Esth: 17,
Job: 18,
Ps: 19,
Prov: 20,
Eccl: 21,
Song: 22,
Isa: 23,
Jer: 24,
Lam: 25,
Ezek: 26,
Dan: 27,
Hos: 28,
Joel: 29,
Amos: 30,
Obad: 31,
Jonah: 32,
Mic: 33,
Nah: 34,
Hab: 35,
Zeph: 36,
Hag: 37,
Zech: 38,
Mal: 39,
Matt: 40,
Mark: 41,
Luke: 42,
John: 43,
Acts: 44,
Rom: 45,
"1Cor": 46,
"2Cor": 47,
Gal: 48,
Eph: 49,
Phil: 50,
Col: 51,
"1Thess": 52,
"2Thess": 53,
"1Tim": 54,
"2Tim": 55,
Titus: 56,
Phlm: 57,
Heb: 58,
Jas: 59,
"1Pet": 60,
"2Pet": 61,
"1John": 62,
"2John": 63,
"3John": 64,
Jude: 65,
Rev: 66
};
var bibleVersion = 13;
/**
*
* @param {array} sources Array of texts to scan for scripture references
* @param {number} bibleVersion ID of bible translation to use. See bible.json
* @returns {array} Nucleus JSON array of scripture references.
*/
function getScriptures(sources, bibleVersion = 13) {
let ret = [];
let bcv = new bcv_parser();
bcv.set_options({ sequence_combination_strategy: "separate" });
sources.forEach(src => {
let osis = bcv.parse(src).osis();
if (osis) {
let refs = osis.split(",");
refs.forEach(ref => {
ret.push(_osisToNucleus(ref));
});
}
});
return removeDuplicates(ret);
}
function removeDuplicates(arr) {
let cleaned = [];
arr.forEach(function(itm) {
let unique = true;
cleaned.forEach(function(itm2) {
if (isEqual(itm, itm2)) unique = false;
});
if (unique) cleaned.push(itm);
});
return cleaned;
}
function isEqual(a, b) {
//Who would have thought! This is actually quite performant!!
return JSON.stringify(a) === JSON.stringify(b);
}
const osisRegex = /([\w]+).([\d]+)(?:.([\d]+))?/g; //(?<book>[\w]+).(?<chapter>[\d]+)(?:.(?<verse>[\d]+))?/g;
function _osisToNucleus(ref) {
/*
John.13.33-John.13.35
John.15.8-John.15.17
Luke.7.36-Luke.7.50
Dan.10-Dan.12
*/
let book = null;
let chapter = null;
let verseRange = "";
let m;
do {
m = osisRegex.exec(ref);
if (m) {
book = m[1];
chapter = m[2];
if (!verseRange) {
verseRange = m[3];
} else {
verseRange += "-" + m[3];
}
}
} while (m);
let scripture = {
bible_version_id: bibleVersion,
bible_book_id: bookIds[book],
chapter: chapter
};
if (verseRange) {
scripture.verses = verseRange;
}
return scripture;
}
module.exports = getScriptures;