Rework scan screen, make lookups non-blocking, fix review findings

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-18 10:19:32 -07:00
1 parent 468cafc6db
commit b5defb4e4f
46 files changed
+1957 -406

No files matched your search

+26 -4
View File
@@ -7,6 +7,8 @@ export interface MarcFields {
lcc: Candidate[];
dewey: string | null;
meta: Partial<BookMeta>;
/** Every 100/700 $a, cleaned but still in "Surname, Forename" form; used to verify title/author matches. */
names: string[];
}
type Node = Record<string, unknown>;
@@ -60,6 +62,25 @@ function sub(f: Field, code: string): string {
function uniq(a: string[]): string[] { return [...new Set(a)]; }
/** Strip trailing punctuation and life dates from a MARC name: "Brooks, Frederick P., 1931-" -> "Brooks, Frederick P." */
export function cleanName(raw: string): string {
let s = raw.trim();
s = s.replace(/[,\s]*\(?\b(?:b\.\s*|d\.\s*|ca\.\s*)?\d{3,4}\??\s*-\s*(?:\d{3,4})?\)?[.,\s]*$/i, '');
s = s.replace(/[\s,;:\/]+$/, '');
// Keep the period of an initial or Jr./Sr.; drop the ISBD full stop after other words.
const last = /([^\s,]+)\.$/.exec(s)?.[1];
if (last && last.length > 1 && !/^(jr|sr)$/i.test(last)) s = s.slice(0, -1);
return s.trim();
}
/** "Herbert, Frank" -> "Frank Herbert"; "King, Martin Luther, Jr." -> "Martin Luther King Jr."; names without a comma are left alone. */
export function displayName(name: string): string {
const parts = name.split(',').map((p) => p.trim()).filter(Boolean);
if (parts.length < 2) return name.trim();
const [surname, forenames, ...suffix] = parts as [string, string, ...string[]];
return [forenames, surname, ...suffix].join(' ');
}
export function parseMarcRecord(rec: Node): MarcFields {
const fields = fieldsOf(rec);
const all = (tag: string) => fields.filter((f) => f.tag === tag);
@@ -83,9 +104,10 @@ export function parseMarcRecord(rec: Node): MarcFields {
const title = f245 ? trimPunct(sub(f245, 'a')) : '';
const subtitle = f245 ? trimPunct(sub(f245, 'b')) : '';
const authors = uniq(
[...all('100'), ...all('700')].map((f) => trimPunct(sub(f, 'a'))).filter(Boolean),
);
const f100 = all('100');
const names = uniq([...f100, ...all('700')].map((f) => cleanName(sub(f, 'a'))).filter(Boolean));
// The 100 is the author; 700s (editors, illustrators, co-authors) only stand in when there is none.
const authors = uniq((f100.length ? f100 : all('700')).map((f) => cleanName(sub(f, 'a'))).filter(Boolean).map(displayName));
const pub = all('260')[0] ?? all('264').find((f) => f.ind2 === '1') ?? all('264')[0];
const publisher = pub ? trimPunct(sub(pub, 'b')) : '';
@@ -101,7 +123,7 @@ export function parseMarcRecord(rec: Node): MarcFields {
if (publisher) meta.publisher = publisher;
if (ym) meta.year = Number(ym[1]);
if (lccn) meta.lccn = lccn;
return { lccn, lcc, dewey, meta };
return { lccn, lcc, dewey, meta, names };
}
/** Parse every MARC record in an SRU response or a bare LCCN permalink record. */