58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { getBooks, loadBible, type Verse } from './bible'
|
|
|
|
const ROLLOVER_LOCAL_HOUR = 2
|
|
|
|
export interface DailyVerse extends Verse {
|
|
book: string
|
|
chapter: number
|
|
verse: number
|
|
}
|
|
|
|
let flatNT: DailyVerse[] | null = null
|
|
|
|
export function daySeed(now: Date = new Date()): number {
|
|
const shifted = new Date(now.getTime() - ROLLOVER_LOCAL_HOUR * 3600_000)
|
|
return Math.floor(Date.UTC(shifted.getFullYear(), shifted.getMonth(), shifted.getDate()) / 86_400_000)
|
|
}
|
|
|
|
export function msUntilRollover(now: Date = new Date()): number {
|
|
const next = new Date(now)
|
|
next.setHours(ROLLOVER_LOCAL_HOUR, 0, 0, 0)
|
|
if (next.getTime() <= now.getTime()) next.setDate(next.getDate() + 1)
|
|
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: DailyVerse[], seed: number): DailyVerse {
|
|
const rand = mulberry32(seed)
|
|
const index = Math.floor(rand() * verses.length)
|
|
return verses[index]
|
|
}
|
|
|
|
export async function loadKJV(): Promise<DailyVerse[]> {
|
|
if (flatNT) return flatNT
|
|
await loadBible()
|
|
flatNT = []
|
|
let inNewTestament = false
|
|
for (const book of getBooks()) {
|
|
if (book.book === 'Matthew') inNewTestament = true
|
|
if (!inNewTestament) continue
|
|
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) {
|
|
flatNT.push({ text: v.text, reference: `${name} ${chapterNumber}:${v.verse}`, book: book.book, chapter: chapterNumber, verse: v.verse })
|
|
}
|
|
}
|
|
}
|
|
return flatNT
|
|
} |