@natlibfi/marc-record

MARC record implementation in JavaScript

Usage no npm install needed!

<script type="module">
  import natlibfiMarcRecord from 'https://cdn.skypack.dev/@natlibfi/marc-record';
</script>

README

MARC record implementation in JavaScript

NPM Version Build Status Test Coverage

MARC record implementation in JavaScript. A JSON schema file specifies the data format.

This a fork of the original marc-record-js. The new implementation uses ES6 syntax and adds validation of the record structure.

Usage

import {MarcRecord} from '@natlibfi/marc-record';
const record = new MarcRecord();

Create record from object

const record = new MarcRecord({leader: 'foo', fields: [
  {tag: '001', value: 'bar'}
]})

Validation options

MarcRecord.getValidationOptions(); // {fields: true, subfields: true, subfieldValues: true }
MarcRecord.setValidationOptions({fields: false});

const record = new MarcRecord({leader: 'foo', fields: []}); // This is ok because setting strict field validation to false

try {
  const record = new MarcRecord({leader: 'foo', fields: []}, {fields: true); // No longer ok
 } catch (err) {
   MarcRecord.setValidationOptions({}); // Reset to default
 }

Mutating the record

record.leader = "00000cam^a22001817i^4500";

// Insert field to the record. Proper ordering is handled automatically.
record.insertField({
    tag: "001"
    value: "007045872"
});

// Append fields to the end of the record
record.appendField({
    tag: '245',
    ind2: '1',
    subfields: [
        {
            code: "a"
            value: "The title of the book"
        },
        {
            code: "c",
            value: "Some author"
        }
    ]
});

Querying for fields

record.getControlfields();
record.getDatafields();
record.get(/^001$/)
record.fields;
record.getFields('245', [{code: 'a', value: 'foo'}]);
record.getFields('001', 'foo');

Cloning a record

const recordB = MarcRecord.clone(recordA)

Record equality check

MarcRecord.isEqual(recordA, recordB);
recordA.equalsTo(recordB);

Simple assertions

record.containsFieldWithValue('245', [{code: 'a', value: 'foo'}]);
record.containsFieldWithValue('001', 'foo');

See also

To serialize and unserialize MARC records, see marc-record-serializers

License and copyright

Copyright (c) 2014-2017 Pasi Tuominen pasi.tuominen@gmail.com

Copyright (c) 2018 University Of Helsinki (The National Library Of Finland)

This project's source code is licensed under the terms of MIT License or any later version.