69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
|
|
export interface KJVVerseData {
|
||
|
|
verse: number
|
||
|
|
text: string
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface KJVChapterData {
|
||
|
|
chapter: string
|
||
|
|
verses: KJVVerseData[]
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface KJVBookData {
|
||
|
|
book: string
|
||
|
|
chapters: KJVChapterData[]
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface KJVData {
|
||
|
|
books: KJVBookData[]
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface Verse {
|
||
|
|
text: string
|
||
|
|
reference: string
|
||
|
|
}
|
||
|
|
|
||
|
|
let flat: Verse[] | null = null
|
||
|
|
|
||
|
|
export function daySeed(now: Date = new Date()): number {
|
||
|
|
return Math.floor(now.getTime() / 86_400_000)
|
||
|
|
}
|
||
|
|
|
||
|
|
export function msUntilMidnightUtc(now: Date = new Date()): number {
|
||
|
|
const next = new Date(now)
|
||
|
|
next.setUTCHours(24, 0, 0, 0)
|
||
|
|
return next.getTime() - now.getTime()
|
||
|
|
}
|
||
|
|
|
||
|
|
function mulberry32(seed: number): () => number {
|
||
|
|
let a = seed >>> 0
|
||
|
|
return () => {
|
||
|
|
a = (a + 0x6d2b79f5) | 0
|
||
|
|
let t = Math.imul(a ^ (a >>> 15), 1 | a)
|
||
|
|
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
|
||
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function pickForDay(verses: Verse[], seed: number): Verse {
|
||
|
|
const rand = mulberry32(seed)
|
||
|
|
const index = Math.floor(rand() * verses.length)
|
||
|
|
return verses[index]
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function loadKJV(): Promise<Verse[]> {
|
||
|
|
if (flat) return flat
|
||
|
|
const res = await fetch('/kjv.json')
|
||
|
|
if (!res.ok) throw new Error(`Failed to load scripture (${res.status})`)
|
||
|
|
const data = (await res.json()) as KJVData
|
||
|
|
flat = []
|
||
|
|
for (const book of data.books) {
|
||
|
|
const name = book.book === 'Psalms' ? 'Psalm' : book.book
|
||
|
|
for (const chapter of book.chapters) {
|
||
|
|
const chapterNumber = Number(chapter.chapter)
|
||
|
|
for (const v of chapter.verses) {
|
||
|
|
flat.push({ text: v.text, reference: `${name} ${chapterNumber}:${v.verse}` })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return flat
|
||
|
|
}
|