@biblioteksentralen/marc 0.0.4 → 0.1.0

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;
@@ -222,7 +230,7 @@ ${validator.errors ? JSON.stringify(validator.errors) : "Unknown error"}.
222
230
  Data:
223
231
  ${JSON.stringify(data)}`
224
232
  );
225
- throw new ValidationFailed(validator.errors ?? []);
233
+ throw new MarcRecordSchemaValidationError(validator.errors ?? []);
226
234
  }
227
235
  getControlFields() {
228
236
  return this.fields.filter(isControlField);
@@ -257,14 +265,6 @@ ${JSON.stringify(data)}`
257
265
  }
258
266
  };
259
267
 
260
- // src/marc-record/MarcParseError.ts
261
- var MarcParseError = class extends Error {
262
- constructor(message, record) {
263
- super(message);
264
- this.record = record;
265
- }
266
- };
267
-
268
268
  // src/marc-record/parseMarcXml.ts
269
269
  async function parseMarcXml(input, options = {}) {
270
270
  const xmlRecord = typeof input === "string" ? await xmlUtils.parseXml(input) : input;
@@ -370,7 +370,7 @@ var serializer = {
370
370
  var escapeSubfieldValue = (value) => {
371
371
  return value.replace(/\n/g, " ").replace(/\$/g, "@{2}");
372
372
  };
373
- function serializeMarcXml(input, pretty = false) {
373
+ function buildRecordXml(input) {
374
374
  const fields = [
375
375
  xmlUtils.createXmlElement("leader", { text: input.leader }),
376
376
  ...input.getControlFields().map(
@@ -396,20 +396,33 @@ function serializeMarcXml(input, pretty = false) {
396
396
  )
397
397
  ];
398
398
  const recordNode = xmlUtils.createXmlElement("record", {
399
- attributes: { xmlns: "http://www.loc.gov/MARC21/slim" },
400
399
  children: fields
401
400
  });
402
- return xmlUtils.serializeXml(recordNode, pretty);
401
+ return recordNode;
402
+ }
403
+ function serializeMarcXml(input, pretty = false) {
404
+ const record = buildRecordXml(input);
405
+ record.setAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
406
+ return xmlUtils.serializeXml(record, pretty);
403
407
  }
404
408
  var withoutEmptyValues = (obj) => Object.keys(obj).reduce(
405
409
  (acc, key) => obj[key] === void 0 ? { ...acc } : { ...acc, [key]: obj[key] },
406
410
  {}
407
411
  );
412
+ function serializeMarcXmlCollection(input, pretty = false) {
413
+ const records = input.map(buildRecordXml);
414
+ const collectionNode = xmlUtils.createXmlElement("collection", {
415
+ attributes: { xmlns: "http://www.loc.gov/MARC21/slim" },
416
+ children: records
417
+ });
418
+ return xmlUtils.serializeXml(collectionNode, pretty);
419
+ }
408
420
 
409
421
  exports.ControlField = ControlField;
410
422
  exports.DataField = DataField;
411
423
  exports.MarcParseError = MarcParseError;
412
424
  exports.MarcRecord = MarcRecord;
425
+ exports.MarcRecordSchemaValidationError = MarcRecordSchemaValidationError;
413
426
  exports.Subfield = Subfield;
414
427
  exports.createControlField = createControlField;
415
428
  exports.createDataField = createDataField;
@@ -417,3 +430,4 @@ exports.marcRecordZodSchema = marcRecordZodSchema;
417
430
  exports.parseMarcXml = parseMarcXml;
418
431
  exports.serializeLineMarc = serializeLineMarc;
419
432
  exports.serializeMarcXml = serializeMarcXml;
433
+ 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 {
@@ -191,10 +192,15 @@ declare const marcRecordZodSchema: z.ZodObject<{
191
192
  declare function serializeLineMarc(input: MarcRecord): string;
192
193
 
193
194
  declare function serializeMarcXml(input: MarcRecord, pretty?: boolean): string;
195
+ declare function serializeMarcXmlCollection(input: MarcRecord[], pretty?: boolean): string;
194
196
 
197
+ declare class MarcRecordSchemaValidationError extends Error {
198
+ readonly errors: ErrorObject[];
199
+ constructor(errors: ErrorObject[]);
200
+ }
195
201
  declare class MarcParseError extends Error {
196
202
  readonly record: string;
197
203
  constructor(message: string, record: string);
198
204
  }
199
205
 
200
- export { ControlField, DataField, type MarcField, MarcParseError, MarcRecord, type SerializedControlField, type SerializedDataField, type SerializedMarcField, type SerializedMarcRecord, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml };
206
+ 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 {
@@ -191,10 +192,15 @@ declare const marcRecordZodSchema: z.ZodObject<{
191
192
  declare function serializeLineMarc(input: MarcRecord): string;
192
193
 
193
194
  declare function serializeMarcXml(input: MarcRecord, pretty?: boolean): string;
195
+ declare function serializeMarcXmlCollection(input: MarcRecord[], pretty?: boolean): string;
194
196
 
197
+ declare class MarcRecordSchemaValidationError extends Error {
198
+ readonly errors: ErrorObject[];
199
+ constructor(errors: ErrorObject[]);
200
+ }
195
201
  declare class MarcParseError extends Error {
196
202
  readonly record: string;
197
203
  constructor(message: string, record: string);
198
204
  }
199
205
 
200
- export { ControlField, DataField, type MarcField, MarcParseError, MarcRecord, type SerializedControlField, type SerializedDataField, type SerializedMarcField, type SerializedMarcRecord, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml };
206
+ 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;
@@ -220,7 +228,7 @@ ${validator.errors ? JSON.stringify(validator.errors) : "Unknown error"}.
220
228
  Data:
221
229
  ${JSON.stringify(data)}`
222
230
  );
223
- throw new ValidationFailed(validator.errors ?? []);
231
+ throw new MarcRecordSchemaValidationError(validator.errors ?? []);
224
232
  }
225
233
  getControlFields() {
226
234
  return this.fields.filter(isControlField);
@@ -255,14 +263,6 @@ ${JSON.stringify(data)}`
255
263
  }
256
264
  };
257
265
 
258
- // src/marc-record/MarcParseError.ts
259
- var MarcParseError = class extends Error {
260
- constructor(message, record) {
261
- super(message);
262
- this.record = record;
263
- }
264
- };
265
-
266
266
  // src/marc-record/parseMarcXml.ts
267
267
  async function parseMarcXml(input, options = {}) {
268
268
  const xmlRecord = typeof input === "string" ? await parseXml(input) : input;
@@ -368,7 +368,7 @@ var serializer = {
368
368
  var escapeSubfieldValue = (value) => {
369
369
  return value.replace(/\n/g, " ").replace(/\$/g, "@{2}");
370
370
  };
371
- function serializeMarcXml(input, pretty = false) {
371
+ function buildRecordXml(input) {
372
372
  const fields = [
373
373
  createXmlElement("leader", { text: input.leader }),
374
374
  ...input.getControlFields().map(
@@ -394,14 +394,26 @@ function serializeMarcXml(input, pretty = false) {
394
394
  )
395
395
  ];
396
396
  const recordNode = createXmlElement("record", {
397
- attributes: { xmlns: "http://www.loc.gov/MARC21/slim" },
398
397
  children: fields
399
398
  });
400
- return serializeXml(recordNode, pretty);
399
+ return recordNode;
400
+ }
401
+ function serializeMarcXml(input, pretty = false) {
402
+ const record = buildRecordXml(input);
403
+ record.setAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
404
+ return serializeXml(record, pretty);
401
405
  }
402
406
  var withoutEmptyValues = (obj) => Object.keys(obj).reduce(
403
407
  (acc, key) => obj[key] === void 0 ? { ...acc } : { ...acc, [key]: obj[key] },
404
408
  {}
405
409
  );
410
+ function serializeMarcXmlCollection(input, pretty = false) {
411
+ const records = input.map(buildRecordXml);
412
+ const collectionNode = createXmlElement("collection", {
413
+ attributes: { xmlns: "http://www.loc.gov/MARC21/slim" },
414
+ children: records
415
+ });
416
+ return serializeXml(collectionNode, pretty);
417
+ }
406
418
 
407
- export { ControlField, DataField, MarcParseError, MarcRecord, Subfield, createControlField, createDataField, marcRecordZodSchema, parseMarcXml, serializeLineMarc, serializeMarcXml };
419
+ 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.4",
3
+ "version": "0.1.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "MARC record parser and serializer",
@@ -28,7 +28,7 @@
28
28
  "ajv": "^8.17.1",
29
29
  "ts-log": "^2.2.5",
30
30
  "zod": "^3.23.8",
31
- "@biblioteksentralen/xml-utils": "^0.0.2"
31
+ "@biblioteksentralen/xml-utils": "^0.0.3"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@arethetypeswrong/cli": "^0.15.4",