Created
October 21, 2018 08:14
-
-
Save maczam79/090c658f88d227ed7792fbcea0a03b15 to your computer and use it in GitHub Desktop.
Extract book, chapter and verse bible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse(text) { | |
var results = []; | |
var regex = /([\d ]*)([a-z]+) +([\d,;:-]+)/ig; | |
while (true) { | |
try { | |
var matcher = regex.exec(text); | |
if (!matcher) return results; | |
var bookIndex = getBookIndex(matcher); | |
var result = {}; | |
if (bookIndex) | |
result.BokyFaha = bookIndex; | |
result.Boky = matcher[2]; | |
result.Pointers = parsePointer(matcher); | |
results.push(result); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
} | |
function getBookIndex(matcher) { | |
var bookIndex = matcher[1]; | |
if (!bookIndex) return null; | |
if (!/^\d+ +$/.test(bookIndex)) | |
throw new Error('Invalid book index'); | |
return bookIndex.replace(/ +/g, ''); | |
} | |
function parsePointer(matcher) { | |
var subPointer = matcher[3].split(';'); | |
var pointers = []; | |
for (var i in subPointer) | |
pointers.push(parseSubPointer(subPointer[i])); | |
return pointers; | |
} | |
function parseSubPointer(rawSubPointer) { | |
var subPointerRegex = /^(\d{1,2}):(\d{1,2})([\d,-]*)$/; | |
var matcher = subPointerRegex.exec(rawSubPointer); | |
if (!matcher) | |
throw new Error('No chapter'); | |
var verse2 = matcher[3]; | |
if (verse2 && !/^[,-]\d+$/.test(verse2)) | |
throw new Error('Invalid verse'); | |
var chapter = matcher[1]; | |
var verse1 = matcher[2]; | |
if (verse2) | |
verse1 += verse2; | |
return { | |
Pointer: matcher[0], | |
Chapitre: chapter, | |
Verset: verse1 | |
}; | |
} | |
var text = [ | |
'Matio 24:14', | |
'1 Jaona 5:19', | |
'1 Salamo 83:18,19', | |
'Apokalypsy 21:14-18;22:3,4' | |
].join('\n'); | |
var results = parse(text); | |
console.log(JSON.stringify(results, null, ' ')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment