@biblioteksentralen/marc
v0.0.2
Published
MARC record parser and serializer
Downloads
6
Keywords
Readme
@biblioteksentralen/marc
Package for representating MARC records in TypeScript and serialize to/from MARC XML and JSON.
The JSON serialization is compatible with schema defined by https://www.npmjs.com/package/@natlibfi/marc-record
Usage
Parsing XML
import { parseMarcXml } from "@biblioteksentralen/marc";
const xmlRecord = `
<record xmlns="http://www.loc.gov/MARC21/slim">
<leader>00000cam^a22001937i^4500</leader>
<controlfield tag="001">000030000</controlfield>
<controlfield tag="003">FI-MELINDA</controlfield>
<controlfield tag="005">20141221175522.0</controlfield>
<controlfield tag="008">890208s1988^^^^sw^|||||||||||||||||swe||</controlfield>
<datafield tag="020" ind1=" " ind2=" ">
<subfield code="a">91-38-61844-3</subfield>
<subfield code="q">inb.</subfield>
</datafield>
<datafield tag="100" ind1="0" ind2=" ">
<subfield code="a">Durö, Robert.</subfield>
</datafield>
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">Konkurrensöverlägsenhet :</subfield>
<subfield code="b">i tio konkreta steg /</subfield>
<subfield code="c">Robert Durö.</subfield>
</datafield>
</record>
`;
const record = parseMarcXml(xmlRecord);
const title = record
.getSubfieldValues("245", /a|b|n|p/)
.join(" ")
?.trim();
Serializing / deserializing
The MarcRecord class can be JSON serialized:
const serializedRecord = JSON.stringify(record);
and deserialized:
const record = MarcRecord.fromJSON(JSON.parse(serializedRecord));
Standalone fields can also be deserialized using the createDataField
function – useful for tests:
const field = createDataField({
tag: "020",
subfields: [{ code: "a", value: "9788283982701" }],
});
JSON Schema validation
The package exports a JSON schema that can be used to validate JSON serialized MARC record structure using AJV or other JSON Schema validators.
import Ajv from "ajv";
import { createMarcSchema } from "@biblioteksentralen/marc";
const schema = createMarcSchema();
const ajv = new Ajv();
const validate = ajv.compile(schema);
const result = validate(record.toJSON());