import { mainEntry } from '../../shared/cutter.ts'; import type { MarcFields } from '../sources/marc.ts'; const plain = (s: string): string => s.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim(); /** Lower-case, main title only (before any ":"), no punctuation, no leading article. */ export function normalizeTitle(title: string): string { return plain(title.split(':')[0]!).replace(/^(a|an|the) /, ''); } /** Equal, or one is a prefix of the other (a record may carry a longer or shorter main title). */ export function titlesMatch(a: string, b: string): boolean { const x = normalizeTitle(a); const y = normalizeTitle(b); if (!x || !y) return false; return x === y || x.startsWith(y + ' ') || y.startsWith(x + ' '); } /** Does a LoC record plausibly describe this book? Title must match and a 100/700 name must contain the surname. */ export function recordMatches(rec: MarcFields, title: string, author: string): boolean { if (!rec.meta.title || !titlesMatch(rec.meta.title, title)) return false; const surname = plain(mainEntry([author], title)); if (!surname) return false; return rec.names.some((n) => (' ' + plain(n) + ' ').includes(' ' + surname + ' ')); }