@bible-api/bible-api
Version:
A simple API for the Bible
474 lines • 12.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const localVersions_1 = __importDefault(require("./localVersions"));
/**
* Create a map of book name synonyms to book IDs or remote version name
* synonyms to remote version IDs.
*
* @param synonyms The book name synonyms.
* @return The map of book name synonyms to book IDs.
*/
function createSynonymsMap(synonyms) {
const map = new Map();
for (const synonymGroup of synonyms) {
for (const synonym of synonymGroup) {
map.set(synonym.toLowerCase().replace(/\s+/g, ''), synonymGroup[0]);
}
}
return map;
}
/** An array of book name synonyms. */
const bookNameSynonyms = [
['Genesis', 'Gen', 'Gn'],
['Exodus', 'Exod', 'Ex'],
['Leviticus', 'Lev', 'Lv'],
['Numbers', 'Num', 'Nm'],
['Deuteronomy', 'Deut', 'Dt'],
['Joshua', 'Josh', 'Jos', 'Jsh'],
['Judges', 'Judg', 'Jdg', 'Jg', 'Jdgs'],
['Ruth', 'Rth', 'Ru'],
[
'1 Samuel',
'1 Sam',
'1 Sa',
'1S',
'I Samuel',
'I Sam',
'I Sa',
'1st Samuel',
'First Samuel'
],
[
'2 Samuel',
'2 Sam',
'2 Sa',
'2S',
'II Samuel',
'II Sam',
'II Sa',
'2nd Samuel',
'Second Samuel'
],
[
'1 Kings',
'1 Kgs',
'1 Ki',
'1K',
'I Kings',
'I Kgs',
'I Ki',
'1st Kings',
'First Kings'
],
[
'2 Kings',
'2 Kgs',
'2 Ki',
'2K',
'II Kings',
'II Kgs',
'II Ki',
'2nd Kings',
'Second Kings'
],
[
'1 Chronicles',
'1 Chron',
'1 Ch',
'1 Chr',
'I Chronicles',
'I Chron',
'I Ch',
'1st Chronicles',
'First Chronicles'
],
[
'2 Chronicles',
'2 Chron',
'2 Ch',
'II Chronicles',
'II Chron',
'II Ch',
'2nd Chronicles',
'Second Chronicles'
],
['Ezra', 'Ezr'],
['Nehemiah', 'Neh', 'Ne'],
['Esther', 'Esth', 'Est', 'Es'],
['Job', 'Jb'],
['Psalms', 'Psalm', 'Ps', 'Psa', 'Psm', 'Pss'],
['Proverbs', 'Prov', 'Prv', 'Pr', 'Pro'],
['Ecclesiastes', 'Eccles', 'Eccl', 'Ecc', 'Qoh', 'Qoheleth'],
[
'Song of Solomon',
'Song of Songs',
'Song',
'SOS',
'Canticle of Canticles',
'Canticles',
'Cant',
'Can'
],
['Isaiah', 'Isa', 'Is'],
['Jeremiah', 'Jer', 'Je', 'Jr'],
['Lamentations', 'Lam', 'La'],
['Ezekiel', 'Ezek', 'Eze', 'Ezk'],
['Daniel', 'Dan', 'Da', 'Dn'],
['Hosea', 'Hos', 'Ho'],
['Joel', 'Jl', 'Joe'],
['Amos', 'Am'],
['Obadiah', 'Obad', 'Ob'],
['Jonah', 'Jon'],
['Micah', 'Mic'],
['Nahum', 'Nah'],
['Habakkuk', 'Hab', 'Hb'],
['Zephaniah', 'Zeph', 'Zep', 'Zp'],
['Haggai', 'Hag', 'Hg'],
['Zechariah', 'Zech', 'Zec', 'Zc'],
['Malachi', 'Mal', 'Ml'],
['Matthew', 'Matt', 'Mat', 'Mt'],
['Mark', 'Mk', 'Mr'],
['Luke', 'Lk', 'Lu'],
['John', 'Jn', 'Jhn'],
['Acts', 'Act', 'Ac'],
['Romans', 'Rom', 'Ro', 'Rm'],
[
'1 Corinthians',
'1 Cor',
'1 Co',
'I Corinthians',
'I Cor',
'I Co',
'1st Corinthians',
'First Corinthians'
],
[
'2 Corinthians',
'2 Cor',
'2 Co',
'II Corinthians',
'II Cor',
'II Co',
'2nd Corinthians',
'Second Corinthians'
],
['Galatians', 'Gal', 'Ga'],
['Ephesians', 'Eph', 'Ep'],
['Philippians', 'Phil', 'Php', 'Pp'],
['Colossians', 'Col', 'Co'],
[
'1 Thessalonians',
'1 Thess',
'1 Th',
'I Thessalonians',
'I Thess',
'I Th',
'1st Thessalonians',
'First Thessalonians'
],
[
'2 Thessalonians',
'2 Thess',
'2 Th',
'II Thessalonians',
'II Thess',
'II Th',
'2nd Thessalonians',
'Second Thessalonians'
],
[
'1 Timothy',
'1 Tim',
'1 Ti',
'I Timothy',
'I Tim',
'I Ti',
'1st Timothy',
'First Timothy'
],
[
'2 Timothy',
'2 Tim',
'2 Ti',
'II Timothy',
'II Tim',
'II Ti',
'2nd Timothy',
'Second Timothy'
],
['Titus', 'Ti', 'Tt'],
['Philemon', 'Philem', 'Phm', 'Pm'],
['Hebrews', 'Heb', 'He'],
['James', 'Jas', 'Jm'],
[
'1 Peter',
'1 Pet',
'1 Pe',
'I Peter',
'I Pet',
'I Pe',
'1st Peter',
'First Peter'
],
[
'2 Peter',
'2 Pet',
'2 Pe',
'II Peter',
'II Pet',
'II Pe',
'2nd Peter',
'Second Peter'
],
[
'1 John',
'1 Jn',
'1 Jhn',
'I John',
'I Jn',
'I Jhn',
'1st John',
'1st Jn',
'1st Jhn',
'First John',
'First Jn',
'First Jhn'
],
[
'2 John',
'2 Jn',
'2 Jhn',
'II John',
'II Jn',
'II Jhn',
'2nd John',
'2nd Jn',
'2nd Jhn',
'Second John',
'Second Jn',
'Second Jhn'
],
[
'3 John',
'3 Jn',
'3 Jhn',
'III John',
'III Jn',
'III Jhn',
'3rd John',
'3rd Jn',
'3rd Jhn',
'Third John',
'Third Jn',
'Third Jhn'
],
['Jude', 'Jud', 'Jd'],
['Revelation', 'Revelations', 'Rev', 'Re']
];
/** A map of book name synonyms to book IDs. */
const bookNameSynonymMap = createSynonymsMap(bookNameSynonyms);
function createVersionSynonymsMap(localVersions) {
const map = new Map();
for (const version of localVersions) {
for (const synonym of version.synonyms) {
const preexistingVersion = map.get(synonym.toLowerCase().replace(/\s/g, ''));
if (!preexistingVersion || version.year > preexistingVersion.year) {
map.set(synonym.toLowerCase().replace(/\s/g, ''), version);
}
}
}
return map;
}
const versionSynonyms = Object.values(localVersions_1.default).flatMap(x => x.synonyms);
const versionSynonymsMap = createVersionSynonymsMap(Object.values(localVersions_1.default));
const remoteVersionSynonyms = [
[
'KJV1769',
'King James Version (1769)',
'King James Version 1769',
'King James Bible (1769)',
'King James Bible 1769',
'King James (1769)',
'King James 1769'
],
[
'PCE',
'Pure Cambridge Edition',
'KJV1900',
'KJV',
'King James',
'King James Version',
'King James Version (1900)',
'King James Version 1900',
'King James Bible (1900)',
'King James Bible 1900',
'King James (1900)',
'King James 1900'
],
['TR1624', 'Textus Receptus (1624)', 'Textus Receptus 1624', 'Elzevir'],
[
'TR1894',
'TR',
'Textus Receptus',
'Textus Receptus (1894)',
'Textus Receptus 1894',
'Scrivener'
],
[
'BG',
'Biblia Gdańska',
'Biblia Gdanska',
'Polish Biblia Gdańska',
'Polish Biblia Gdanska',
'PBG'
],
[
'Vulgate',
'Vulgata',
'Biblia Vugata',
'Latina Vulgata',
'Latina Vulgate',
'Latin Vulgate',
'Vulgata Latina',
'Latina',
'Latin'
]
];
const remoteVersionSynonymsMap = createSynonymsMap(remoteVersionSynonyms);
/**
* Create a regular expression to parse Bible references.
*
* @return The regular expression to parse Bible references.
*/
function createRegex() {
const bookNames = bookNameSynonyms.flat();
return new RegExp(`(?:^|[-\\s^!"#$%&'()*+,./:;<=>?@\\\\\\]^_\`{|}~])(?<bookName>${bookNames
.join('|')
.replace(/\s+/g, '\\.?[^\\S\\r\\n]*')})\\.?[^\\S\\r\\n]*(?<firstChapter>\\d+)(?::(?![^\\S\\r\\n]*\\d))?(?:\\.?[^\\S\\r\\n]*[-–—]\\.?[^\\S\\r\\n]*(?<lastChapter1>\\d+)(?::(?![^\\S\\r\\n]*\\d))?(?:\\.?[^\\S\\r\\n]*[.:;]\\.?[^\\S\\r\\n]*(?<lastVerse1>\\d+))?|\\.?[^\\S\\r\\n]*[.:;]\\.?[^\\S\\r\\n]*(?<firstVerse>\\d+)(?:\\.?[^\\S\\r\\n]*[-–—]\\.?[^\\S\\r\\n]*(?<lastChapter2>\\d+)[.:;](?<lastVerse2>\\d+)|\\.?[^\\S\\r\\n]*[-–—]\\.?[^\\S\\r\\n]*(?<lastVerse3>\\d+)|(?<onwards>[-–—]))?)?\\.?[^\\S\\r\\n]*\\(?\\.?[^\\S\\r\\n]*(?<version>${[
...versionSynonyms,
...remoteVersionSynonyms.flat()
]
.sort((a, b) => b.length - a.length)
.join('|')
.replace(/\s+/g, '\\.?[^\\S\\r\\n]*')})?`, 'gi');
}
/** A regular expression to parse Bible references. */
const regex = createRegex();
/**
* Parse a Bible version string.
*
* @param str The string to parse.
* @param defaultVersion The default version to fall back to.
* @return The parsed Bible version.
*/
function parseBibleVersionString(str, defaultVersion) {
return (versionSynonymsMap.get(str?.toLowerCase().replace(/\.?\s+/g, '') ?? '') ??
versionSynonymsMap.get(defaultVersion.toLowerCase().replace(/\.?\s+/g, '')) ??
remoteVersionSynonymsMap.get(str?.toLowerCase().replace(/\.?\s+/g, '') ?? '') ??
defaultVersion);
}
/**
* Parse a Bible reference.
*
* @param match The match to parse.
* @param defaultVersion The default version to fall back to.
* @return Options for `version.getPassage()`.
*/
function parseMatch(match, defaultVersion = 'KJV1769') {
const { groups } = match;
if (!groups)
return null;
const version = parseBibleVersionString(groups['version'], defaultVersion);
const bookName = groups['bookName']?.toLowerCase()?.replace(/\.|\s+/g, '');
const bookId = bookNameSynonymMap.get(bookName ?? '');
if (!bookId)
return null;
if ((version === 'TR1624' ||
version === 'TR1894' ||
version?.shortName === 'TR1624' ||
version?.shortName === 'TR1894') &&
![
'Matthew',
'Mark',
'Luke',
'John',
'Acts',
'Romans',
'1 Corinthians',
'2 Corinthians',
'Galatians',
'Ephesians',
'Philippians',
'Colossians',
'1 Thessalonians',
'2 Thessalonians',
'1 Timothy',
'2 Timothy',
'Titus',
'Philemon',
'Hebrews',
'James',
'1 Peter',
'2 Peter',
'1 John',
'2 John',
'3 John',
'Jude',
'Revelation'
].includes(bookId)) {
return null;
}
const firstChapterNumber = Number(groups['firstChapter']);
const firstVerseNumber = Number(groups['firstVerse'] ?? 1);
const lastChapterNumber = Number(groups['lastChapter1'] ?? groups['lastChapter2'] ?? firstChapterNumber);
const lastVerseNumber = groups['onwards']
? Infinity
: Number(groups['lastVerse1'] ??
groups['lastVerse2'] ??
groups['lastVerse3'] ??
groups['firstVerse'] ??
Infinity);
if (firstChapterNumber > lastChapterNumber)
return null;
if (firstChapterNumber === lastChapterNumber &&
firstVerseNumber > lastVerseNumber)
return null;
return {
match,
version,
getPassageOptions: {
bookId,
start: {
chapterNumber: firstChapterNumber,
verseNumber: firstVerseNumber
},
end: {
chapterNumber: lastChapterNumber,
verseNumber: lastVerseNumber
}
}
};
}
/**
* Parse a string for Bible references.
*
* @param options The options for parsing.
* @return An array of options for `version.getPassage()` along with the version
* to use.
*/
function parse(options) {
const { defaultVersion = 'PCE', text } = options;
const returnData = [];
for (const match of text.matchAll(regex)) {
const parserReturn = parseMatch(match, defaultVersion);
if (!parserReturn)
continue;
returnData.push(parserReturn);
}
return returnData.sort((a, b) => (a.match.index ?? 0) - (b.match.index ?? 0));
}
exports.default = parse;
//# sourceMappingURL=parse.js.map