Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active January 30, 2022 00:50
Show Gist options
  • Select an option

  • Save wilmoore/addda46b5e888790e759f98717ac8ea5 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/addda46b5e888790e759f98717ac8ea5 to your computer and use it in GitHub Desktop.
Bible Reference Tokenizer
// const reference = 'Song of Solomon 7 NLT'
// const reference = 'Song of Solomon NLT'
// const reference = 'Song of Solomon'
// const reference = 'Proverbs 3:21-26 NLT'
// const reference = 'Proverbs 3 NLT'
// const reference = 'Proverbs 3: NLT'
const reference = 'Proverbs 3:1 NLT'
const LINE = '@'
const TOKEN_TYPES = [
['BOOK', /\b(song of solomon|song of songs|psalms|proverbs)\b/ui],
['PASSAGE', /(\b([\d]+[:]?[\d]?[-]?[\d]?)\b)/ui],
['TRANSLATION', /\b(NLT|NKJV|NIV)\b/ui],
]
const tokenizer = (source) => {
let reference = source
console.log({ reference })
TOKEN_TYPES.forEach((tokenType, iteration) => {
console.log(LINE.repeat(10), iteration, LINE.repeat(50))
const [type, rule] = tokenType
console.log({type, rule})
const matched = reference.match(rule)
console.log({ matched })
if (matched) {
let value = matched[1]
reference = reference.slice(value.length).trim()
let token = { type, value }
console.log({ token, reference})
}
})
}
tokenizer(reference)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment