@biblioteksentralen/marc 0.0.5 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -178,14 +178,22 @@ function createMarcSchema({
178
178
  };
179
179
  }
180
180
 
181
- // src/marc-record/MarcRecord.ts
182
- var validator = new ajv.Ajv().compile(createMarcSchema());
183
- var ValidationFailed = class extends Error {
181
+ // src/marc-record/errors.ts
182
+ var MarcRecordSchemaValidationError = class extends Error {
184
183
  constructor(errors) {
185
- super("MarcRecord validation failed");
184
+ super("MarcRecord schema validation failed");
186
185
  this.errors = errors;
187
186
  }
188
187
  };
188
+ var MarcParseError = class extends Error {
189
+ constructor(message, record) {
190
+ super(message);
191
+ this.record = record;
192
+ }
193
+ };
194
+
195
+ // src/marc-record/MarcRecord.ts
196
+ var validator = new ajv.Ajv().compile(createMarcSchema());
189
197
  var MarcRecord = class _MarcRecord {
190
198
  format;
191
199
  leader;
@@ -212,6 +220,13 @@ var MarcRecord = class _MarcRecord {
212
220
  )
213
221
  });
214
222
  }
223
+ toJSON() {
224
+ return {
225
+ format: this.format,
226
+ leader: this.leader,
227
+ fields: this.fields.map((field) => field.toJSON())
228
+ };
229
+ }
215
230
  static validateJSON(data, log) {
216
231
  if (validator(data)) {
217
232
  return data;
@@ -222,7 +237,7 @@ ${validator.errors ? JSON.stringify(validator.errors) : "Unknown error"}.
222
237
  Data:
223
238
  ${JSON.stringify(data)}`
224
239
  );
225
- throw new ValidationFailed(validator.errors ?? []);
240
+ throw new MarcRecordSchemaValidationError(validator.errors ?? []);
226
241
  }
227
242
  getControlFields() {
228
243
  return this.fields.filter(isControlField);
@@ -248,20 +263,43 @@ ${JSON.stringify(data)}`
248
263
  getFirstSubfieldValue(tag, code, indicators) {
249
264
  return this.getSubfieldValues(tag, code, indicators)[0];
250
265
  }
251
- toJSON() {
252
- return {
266
+ /**
267
+ * Returns a new MarcRecord without control fields matching the given predicate, without modifying
268
+ * the original one.
269
+ */
270
+ withoutControlFields(predicate) {
271
+ return new _MarcRecord({
253
272
  format: this.format,
254
273
  leader: this.leader,
255
- fields: this.fields.map((field) => field.toJSON())
256
- };
274
+ fields: this.fields.filter(
275
+ (field) => !isControlField(field) || !predicate(field)
276
+ )
277
+ });
257
278
  }
258
- };
259
-
260
- // src/marc-record/MarcParseError.ts
261
- var MarcParseError = class extends Error {
262
- constructor(message, record) {
263
- super(message);
264
- this.record = record;
279
+ /**
280
+ * Returns a new MarcRecord without data fields matching the given predicate, without modifying
281
+ * the original one.
282
+ */
283
+ withoutDataFields(predicate) {
284
+ return new _MarcRecord({
285
+ format: this.format,
286
+ leader: this.leader,
287
+ fields: this.fields.filter(
288
+ (field) => !isDataField(field) || !predicate(field)
289
+ )
290
+ });
291
+ }
292
+ /**
293
+ * Returns a new MarcRecord with the given fields added without modifying the original one.
294
+ */
295
+ withFields(fields) {
296
+ return new _MarcRecord({
297
+ format: this.format,
298
+ leader: this.leader,
299
+ fields: [...this.fields, ...fields].toSorted(
300
+ (a, b) => a.tag.localeCompare(b.tag)
301
+ )
302
+ });
265
303
  }
266
304
  };
267
305
 
@@ -370,7 +408,7 @@ var serializer = {
370
408
  var escapeSubfieldValue = (value) => {
371
409
  return value.replace(/\n/g, " ").replace(/\$/g, "@{2}");
372
410
  };
373
- function serializeMarcXml(input, pretty = false) {
411
+ function buildRecordXml(input) {
374
412
  const fields = [
375
413
  xmlUtils.createXmlElement("leader", { text: input.leader }),
376
414
  ...input.getControlFields().map(
@@ -396,20 +434,33 @@ function serializeMarcXml(input, pretty = false) {
396
434
  )
397
435
  ];
398
436
  const recordNode = xmlUtils.createXmlElement("record", {
399
- attributes: { xmlns: "http://www.loc.gov/MARC21/slim" },
400
437
  children: fields
401
438
  });
402
- return xmlUtils.serializeXml(recordNode, pretty);
439
+ return recordNode;
440
+ }
441
+ function serializeMarcXml(input, pretty = false) {
442
+ const record = buildRecordXml(input);
443
+ record.setAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
444
+ return xmlUtils.serializeXml(record, pretty);
403
445
  }
404
446
  var withoutEmptyValues = (obj) => Object.keys(obj).reduce(
405
447
  (acc, key) => obj[key] === void 0 ? { ...acc } : { ...acc, [key]: obj[key] },
406
448
  {}
407
449
  );
450
+ function serializeMarcXmlCollection(input, pretty = false) {
451
+ const records = input.map(buildRecordXml);
452
+ const collectionNode = xmlUtils.createXmlElement("collection", {
453
+ attributes: { xmlns: "http://www.loc.gov/MARC21/slim" },
454
+ children: records
455
+ });
456
+ return xmlUtils.serializeXml(collectionNode, pretty);
457
+ }
408
458
 
409
459
  exports.ControlField = ControlField;
410
460
  exports.DataField = DataField;
411
461
  exports.MarcParseError = MarcParseError;
412
462
  exports.MarcRecord = MarcRecord;
463
+ exports.MarcRecordSchemaValidationError = MarcRecordSchemaValidationError;
413
464
  exports.Subfield = Subfield;
414
465
  exports.createControlField = createControlField;
415
466
  exports.createDataField = createDataField;
@@ -417,3 +468,4 @@ exports.marcRecordZodSchema = marcRecordZodSchema;
417
468
  exports.parseMarcXml = parseMarcXml;
418
469
  exports.serializeLineMarc = serializeLineMarc;
419
470
  exports.serializeMarcXml = serializeMarcXml;
471
+ exports.serializeMarcXmlCollection = serializeMarcXmlCollection;
package/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { XmlElement } from '@biblioteksentralen/xml-utils';
2
2
  import { Logger } from 'ts-log';
3
3
  import { z } from 'zod';
4
+ import { ErrorObject } from 'ajv';
4
5
 
5
6
  type SerializedMarcField = SerializedDataField | SerializedControlField;
6
7
  interface SerializedControlField {
@@ -75,6 +76,7 @@ declare class MarcRecord {
75
76
  format?: string;
76
77
  });
77
78
  static fromJSON(data: unknown, log: Logger): MarcRecord;
79
+ toJSON(): SerializedMarcRecord;
78
80
  static validateJSON(data: unknown, log: Logger): SerializedMarcRecord;
79
81
  getControlFields(): ControlField[];
80
82
  getControlField(tag: string): ControlField | undefined;
@@ -83,7 +85,20 @@ declare class MarcRecord {
83
85
  getSubfields(tag: string | RegExp, code: string | RegExp, indicators?: Indicators): Subfield[];
84
86
  getSubfieldValues(tag: string | RegExp, code: string | RegExp, indicators?: Indicators): string[];
85
87
  getFirstSubfieldValue(tag: string | RegExp, code: string | RegExp, indicators?: Indicators): string | undefined;
86
- toJSON(): SerializedMarcRecord;
88
+ /**
89
+ * Returns a new MarcRecord without control fields matching the given predicate, without modifying
90
+ * the original one.
91
+ */
92
+ withoutControlFields(predicate: (field: ControlField) => boolean): MarcRecord;
93
+ /**
94
+ * Returns a new MarcRecord without data fields matching the given predicate, without modifying
95
+ * the original one.
96
+ */
97
+ withoutDataFields(predicate: (field: DataField) => boolean): MarcRecord;
98
+ /**
99
+ * Returns a new MarcRecord with the given fields added without modifying the original one.
100
+ */
101
+ withFields(fields: MarcField[]): MarcRecord;
87
102
  }
88
103
 
89
104
  interface MarcXmlOptions {
@@ -191,10 +206,15 @@ declare const marcRecordZodSchema: z.ZodObject<{
191
206
  declare function serializeLineMarc(input: MarcRecord): string;
192
207
 
193
208
  declare function serializeMarcXml(input: MarcRecord, pretty?: boolean): string;
209
+ declare function serializeMarcXmlCollection(input: MarcRecord[], pretty?: boolean): string;
194
210
 
211
+ declare class MarcRecordSchemaValidationError extends Error {
212
+ readonly errors: ErrorObject[];
213
+ constructor(errors: ErrorObject[]);
214
+ }
195
215
  declare class MarcParseError extends Error {
196
216
  readonly record: string;
197
217
  constructor(message: string, record: string);
198
218
  }
199
219
 
200
- export { ControlField, DataField, type MarcField, MarcParseError, MarcRecord, type SerializedControlField, type SerializedDataField, type SerializedMarcField, type SerializedMarcRecord, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml };
220
+ export { ControlField, DataField, type MarcField, MarcParseError, MarcRecord, MarcRecordSchemaValidationError, type SerializedControlField, type SerializedDataField, type SerializedMarcField, type SerializedMarcRecord, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml, serializeMarcXmlCollection };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { XmlElement } from '@biblioteksentralen/xml-utils';
2
2
  import { Logger } from 'ts-log';
3
3
  import { z } from 'zod';
4
+ import { ErrorObject } from 'ajv';
4
5
 
5
6
  type SerializedMarcField = SerializedDataField | SerializedControlField;
6
7
  interface SerializedControlField {
@@ -75,6 +76,7 @@ declare class MarcRecord {
75
76
  format?: string;
76
77
  });
77
78
  static fromJSON(data: unknown, log: Logger): MarcRecord;
79
+ toJSON(): SerializedMarcRecord;
78
80
  static validateJSON(data: unknown, log: Logger): SerializedMarcRecord;
79
81
  getControlFields(): ControlField[];
80
82
  getControlField(tag: string): ControlField | undefined;
@@ -83,7 +85,20 @@ declare class MarcRecord {
83
85
  getSubfields(tag: string | RegExp, code: string | RegExp, indicators?: Indicators): Subfield[];
84
86
  getSubfieldValues(tag: string | RegExp, code: string | RegExp, indicators?: Indicators): string[];
85
87
  getFirstSubfieldValue(tag: string | RegExp, code: string | RegExp, indicators?: Indicators): string | undefined;
86
- toJSON(): SerializedMarcRecord;
88
+ /**
89
+ * Returns a new MarcRecord without control fields matching the given predicate, without modifying
90
+ * the original one.
91
+ */
92
+ withoutControlFields(predicate: (field: ControlField) => boolean): MarcRecord;
93
+ /**
94
+ * Returns a new MarcRecord without data fields matching the given predicate, without modifying
95
+ * the original one.
96
+ */
97
+ withoutDataFields(predicate: (field: DataField) => boolean): MarcRecord;
98
+ /**
99
+ * Returns a new MarcRecord with the given fields added without modifying the original one.
100
+ */
101
+ withFields(fields: MarcField[]): MarcRecord;
87
102
  }
88
103
 
89
104
  interface MarcXmlOptions {
@@ -191,10 +206,15 @@ declare const marcRecordZodSchema: z.ZodObject<{
191
206
  declare function serializeLineMarc(input: MarcRecord): string;
192
207
 
193
208
  declare function serializeMarcXml(input: MarcRecord, pretty?: boolean): string;
209
+ declare function serializeMarcXmlCollection(input: MarcRecord[], pretty?: boolean): string;
194
210
 
211
+ declare class MarcRecordSchemaValidationError extends Error {
212
+ readonly errors: ErrorObject[];
213
+ constructor(errors: ErrorObject[]);
214
+ }
195
215
  declare class MarcParseError extends Error {
196
216
  readonly record: string;
197
217
  constructor(message: string, record: string);
198
218
  }
199
219
 
200
- export { ControlField, DataField, type MarcField, MarcParseError, MarcRecord, type SerializedControlField, type SerializedDataField, type SerializedMarcField, type SerializedMarcRecord, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml };
220
+ export { ControlField, DataField, type MarcField, MarcParseError, MarcRecord, MarcRecordSchemaValidationError, type SerializedControlField, type SerializedDataField, type SerializedMarcField, type SerializedMarcRecord, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml, serializeMarcXmlCollection };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { parseXml, createXmlElement, serializeXml } from '@biblioteksentralen/xml-utils';
1
+ import { parseXml, serializeXml, createXmlElement } from '@biblioteksentralen/xml-utils';
2
2
  import { Ajv } from 'ajv';
3
3
  import { z } from 'zod';
4
4
 
@@ -176,14 +176,22 @@ function createMarcSchema({
176
176
  };
177
177
  }
178
178
 
179
- // src/marc-record/MarcRecord.ts
180
- var validator = new Ajv().compile(createMarcSchema());
181
- var ValidationFailed = class extends Error {
179
+ // src/marc-record/errors.ts
180
+ var MarcRecordSchemaValidationError = class extends Error {
182
181
  constructor(errors) {
183
- super("MarcRecord validation failed");
182
+ super("MarcRecord schema validation failed");
184
183
  this.errors = errors;
185
184
  }
186
185
  };
186
+ var MarcParseError = class extends Error {
187
+ constructor(message, record) {
188
+ super(message);
189
+ this.record = record;
190
+ }
191
+ };
192
+
193
+ // src/marc-record/MarcRecord.ts
194
+ var validator = new Ajv().compile(createMarcSchema());
187
195
  var MarcRecord = class _MarcRecord {
188
196
  format;
189
197
  leader;
@@ -210,6 +218,13 @@ var MarcRecord = class _MarcRecord {
210
218
  )
211
219
  });
212
220
  }
221
+ toJSON() {
222
+ return {
223
+ format: this.format,
224
+ leader: this.leader,
225
+ fields: this.fields.map((field) => field.toJSON())
226
+ };
227
+ }
213
228
  static validateJSON(data, log) {
214
229
  if (validator(data)) {
215
230
  return data;
@@ -220,7 +235,7 @@ ${validator.errors ? JSON.stringify(validator.errors) : "Unknown error"}.
220
235
  Data:
221
236
  ${JSON.stringify(data)}`
222
237
  );
223
- throw new ValidationFailed(validator.errors ?? []);
238
+ throw new MarcRecordSchemaValidationError(validator.errors ?? []);
224
239
  }
225
240
  getControlFields() {
226
241
  return this.fields.filter(isControlField);
@@ -246,20 +261,43 @@ ${JSON.stringify(data)}`
246
261
  getFirstSubfieldValue(tag, code, indicators) {
247
262
  return this.getSubfieldValues(tag, code, indicators)[0];
248
263
  }
249
- toJSON() {
250
- return {
264
+ /**
265
+ * Returns a new MarcRecord without control fields matching the given predicate, without modifying
266
+ * the original one.
267
+ */
268
+ withoutControlFields(predicate) {
269
+ return new _MarcRecord({
251
270
  format: this.format,
252
271
  leader: this.leader,
253
- fields: this.fields.map((field) => field.toJSON())
254
- };
272
+ fields: this.fields.filter(
273
+ (field) => !isControlField(field) || !predicate(field)
274
+ )
275
+ });
255
276
  }
256
- };
257
-
258
- // src/marc-record/MarcParseError.ts
259
- var MarcParseError = class extends Error {
260
- constructor(message, record) {
261
- super(message);
262
- this.record = record;
277
+ /**
278
+ * Returns a new MarcRecord without data fields matching the given predicate, without modifying
279
+ * the original one.
280
+ */
281
+ withoutDataFields(predicate) {
282
+ return new _MarcRecord({
283
+ format: this.format,
284
+ leader: this.leader,
285
+ fields: this.fields.filter(
286
+ (field) => !isDataField(field) || !predicate(field)
287
+ )
288
+ });
289
+ }
290
+ /**
291
+ * Returns a new MarcRecord with the given fields added without modifying the original one.
292
+ */
293
+ withFields(fields) {
294
+ return new _MarcRecord({
295
+ format: this.format,
296
+ leader: this.leader,
297
+ fields: [...this.fields, ...fields].toSorted(
298
+ (a, b) => a.tag.localeCompare(b.tag)
299
+ )
300
+ });
263
301
  }
264
302
  };
265
303
 
@@ -368,7 +406,7 @@ var serializer = {
368
406
  var escapeSubfieldValue = (value) => {
369
407
  return value.replace(/\n/g, " ").replace(/\$/g, "@{2}");
370
408
  };
371
- function serializeMarcXml(input, pretty = false) {
409
+ function buildRecordXml(input) {
372
410
  const fields = [
373
411
  createXmlElement("leader", { text: input.leader }),
374
412
  ...input.getControlFields().map(
@@ -394,14 +432,26 @@ function serializeMarcXml(input, pretty = false) {
394
432
  )
395
433
  ];
396
434
  const recordNode = createXmlElement("record", {
397
- attributes: { xmlns: "http://www.loc.gov/MARC21/slim" },
398
435
  children: fields
399
436
  });
400
- return serializeXml(recordNode, pretty);
437
+ return recordNode;
438
+ }
439
+ function serializeMarcXml(input, pretty = false) {
440
+ const record = buildRecordXml(input);
441
+ record.setAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
442
+ return serializeXml(record, pretty);
401
443
  }
402
444
  var withoutEmptyValues = (obj) => Object.keys(obj).reduce(
403
445
  (acc, key) => obj[key] === void 0 ? { ...acc } : { ...acc, [key]: obj[key] },
404
446
  {}
405
447
  );
448
+ function serializeMarcXmlCollection(input, pretty = false) {
449
+ const records = input.map(buildRecordXml);
450
+ const collectionNode = createXmlElement("collection", {
451
+ attributes: { xmlns: "http://www.loc.gov/MARC21/slim" },
452
+ children: records
453
+ });
454
+ return serializeXml(collectionNode, pretty);
455
+ }
406
456
 
407
- export { ControlField, DataField, MarcParseError, MarcRecord, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml };
457
+ export { ControlField, DataField, MarcParseError, MarcRecord, MarcRecordSchemaValidationError, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml, serializeMarcXmlCollection };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biblioteksentralen/marc",
3
- "version": "0.0.5",
3
+ "version": "0.1.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "MARC record parser and serializer",