Files
booksorter/scripts/lcc-parse.test.ts
T
swaitsandClaude Fable 5.2 7ae1bcef0a Print the subject hierarchy as a tree on cards, remove the QR code
Each card now shows, under the call number, the Library of Congress
outline levels for its class (from the published outline, down to the
narrowest range) followed by the place or topic from the catalogue
record's subject heading, one level per line. Existing books get the
subject part filled in by a background job at startup that changes
nothing else about them.

The QR code and everything that read it are removed; the subjects line
on the card is replaced by the tree.

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

68 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { cleanCaption, parseBounds, parseOutlineText } from './lcc-parse.ts';
test('number ranges in every form the outline uses', () => {
assert.deepEqual(parseBounds('191.2-200.66'), { lo: 191.2, hi: 200.66 });
assert.deepEqual(parseBounds('1201.5'), { lo: 1201.5, hi: 1201.5 });
assert.deepEqual(parseBounds('1115-(1193)'), { lo: 1115, hi: 1193 });
assert.deepEqual(parseBounds('(1)-1281'), { lo: 1, hi: 1281 });
assert.deepEqual(parseBounds('1569.C2'), { lo: 1569, hi: 1569, cutterLo: 'C2', cutterHi: 'C2' });
assert.deepEqual(parseBounds('469.M21-.M38'), { lo: 469, hi: 469, cutterLo: 'M21', cutterHi: 'M38' });
assert.deepEqual(parseBounds('990.U45-U46', 'DA'), { lo: 990, hi: 990, cutterLo: 'U45', cutterHi: 'U46' });
assert.deepEqual(parseBounds('1.A5-3.3'), { lo: 1, hi: 3.3, cutterLo: 'A5' });
assert.deepEqual(parseBounds('12.5-.9'), { lo: 12.5, hi: 12.9 });
assert.deepEqual(parseBounds('1003-Z1005.5', 'Z'), { lo: 1003, hi: 1005.5 });
assert.equal(parseBounds('4440-4406'), null);
assert.equal(parseBounds('274.5-6'), null);
});
test('captions: wraps joined, notes dropped, headers and breaks ignored, nesting kept in order', () => {
const text = [
' LIBRARY OF CONGRESS CLASSIFICATION OUTLINE',
'Subclass GV Recreation. Leisure',
'Subclass GV',
'',
'GV1-1860 Recreation. Leisure',
'GV191.2-200.66 Outdoor life. Outdoor recreation',
'GV199-199.62 Hiking. Pedestrian tours',
'GV346-351.5 School and college athletics. Intramural',
' and interscholastic athletics',
'GV401-433 Physical education facilities.',
' Including gymnasiums, athletic fields,',
' playgrounds, etc.',
'\fGV1470-1511 Parties. Party games and stunts',
'GV1201.5 Hobbies (General)',
'Subclass GX',
'GX5 Heading',
' Individual states',
].join('\n');
const { ranges, problems } = parseOutlineText(text);
assert.deepEqual(problems, []);
assert.deepEqual(ranges.get('GV'), [
[1, 1860, 'Recreation. Leisure'],
[191.2, 200.66, 'Outdoor life. Outdoor recreation'],
[199, 199.62, 'Hiking. Pedestrian tours'],
[346, 351.5, 'School and college athletics. Intramural and interscholastic athletics'],
[401, 433, 'Physical education facilities'],
[1470, 1511, 'Parties. Party games and stunts'],
[1201.5, 1201.5, 'Hobbies (General)'],
]);
assert.deepEqual(ranges.get('GX'), [[5, 5, 'Heading']]);
});
test('unreadable ranges are reported, not guessed', () => {
const { ranges, problems } = parseOutlineText('Subclass KZ\nKZD4440-4406 Liability for accidents\nKZ5 Fine\n');
assert.equal(problems.length, 1);
assert.deepEqual(ranges.get('KZ'), [[5, 5, 'Fine']]);
});
test('caption clean-up trims debris only', () => {
assert.equal(cleanCaption(' Law and\n order , '), 'Law and order');
assert.equal(cleanCaption('Museology. Museum methods, technique, etc.'), 'Museology. Museum methods, technique, etc.');
assert.equal(cleanCaption('Purānas .'), 'Purānas');
assert.equal(cleanCaption('1789­'), '1789-');
assert.equal(cleanCaption('1871- . Later 19th century'), '1871- . Later 19th century');
assert.equal(cleanCaption('Persian wars, 499-479 B.C.'), 'Persian wars, 499-479 B.C.');
});