Files
booksorter/src/web/shelve.ts
T
swaitsandClaude Fable 5.2 60fa4ef6f7 Show author names in natural order everywhere
Names read from MARC personal-name fields are stored as "Steve Allen",
and names already stored as "Allen, Steve" are shown and printed in
natural order. Search matches both forms.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 14:09:09 -07:00

30 lines
1.5 KiB
TypeScript

import type { Book } from '../shared/types.ts';
import { cardLines, formatCallNumber, parseCallNumber, withYear } from '../shared/callnumber.ts';
import { displayAuthors } from '../shared/names.ts';
import { cutterFor, mainEntry } from '../shared/cutter.ts';
/** A call number for `authors`/`title` that shelves next to `neighbour`: its class part, a new cutter, then the year. */
export function composeNumber(neighbour: string, authors: string[], title: string, year: number | null): string | null {
const cn = parseCallNumber(neighbour);
if (!cn) return null;
const classPart = parseCallNumber(cardLines(cn)[0]);
const cutter = parseCallNumber(`A1 ${cutterFor(mainEntry(authors, title))}`)?.cutters[0];
if (!classPart || !cutter) return null;
const next = { ...classPart, cutters: [...classPart.cutters] };
if (next.cutters.length < 3) next.cutters.push(cutter);
return formatCallNumber(year ? withYear(next, year) : next);
}
/** Up to `limit` of the library's books with a readable call number whose title or author matches. */
export function findNeighbours(books: Book[], text: string, limit = 8): Book[] {
const needle = text.trim().toLowerCase();
if (!needle) return [];
const out: Book[] = [];
for (const b of books) {
if (!b.callNumber || !parseCallNumber(b.callNumber)) continue;
if ([b.title, b.authors.join(' '), displayAuthors(b.authors).join(' ')].some(s => s.toLowerCase().includes(needle))) out.push(b);
if (out.length >= limit) break;
}
return out;
}