@biblioteksentralen/marc 0.0.3 → 0.0.5

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
@@ -199,9 +199,10 @@ var MarcRecord = class _MarcRecord {
199
199
  this.leader = leader;
200
200
  this.fields = fields;
201
201
  }
202
- static fromJSON(data) {
202
+ static fromJSON(data, log) {
203
203
  const { format, leader, fields } = this.validateJSON(
204
- fixInvalidMarcRecordSerialization(data)
204
+ fixInvalidMarcRecordSerialization(data),
205
+ log
205
206
  );
206
207
  return new _MarcRecord({
207
208
  leader,
@@ -211,10 +212,16 @@ var MarcRecord = class _MarcRecord {
211
212
  )
212
213
  });
213
214
  }
214
- static validateJSON(data) {
215
+ static validateJSON(data, log) {
215
216
  if (validator(data)) {
216
217
  return data;
217
218
  }
219
+ log.error(
220
+ `MarcRecord validation failed:
221
+ ${validator.errors ? JSON.stringify(validator.errors) : "Unknown error"}.
222
+ Data:
223
+ ${JSON.stringify(data)}`
224
+ );
218
225
  throw new ValidationFailed(validator.errors ?? []);
219
226
  }
220
227
  getControlFields() {
@@ -346,7 +353,7 @@ function serializeLineMarc(input) {
346
353
  `;
347
354
  }
348
355
  var serializer = {
349
- leader: (leader) => `*LDR ${leader}
356
+ leader: (leader) => `*LDR${leader}
350
357
  `,
351
358
  controlfield: (field) => `*${field.tag}${field.value}
352
359
  `,
@@ -361,7 +368,7 @@ var serializer = {
361
368
  }
362
369
  };
363
370
  var escapeSubfieldValue = (value) => {
364
- return value.replace(/\$/g, "$$");
371
+ return value.replace(/\n/g, " ").replace(/\$/g, "@{2}");
365
372
  };
366
373
  function serializeMarcXml(input, pretty = false) {
367
374
  const fields = [
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { XmlElement } from '@biblioteksentralen/xml-utils';
2
+ import { Logger } from 'ts-log';
2
3
  import { z } from 'zod';
3
4
 
4
5
  type SerializedMarcField = SerializedDataField | SerializedControlField;
@@ -73,8 +74,8 @@ declare class MarcRecord {
73
74
  fields: MarcField[];
74
75
  format?: string;
75
76
  });
76
- static fromJSON(data: unknown): MarcRecord;
77
- static validateJSON(data: unknown): SerializedMarcRecord;
77
+ static fromJSON(data: unknown, log: Logger): MarcRecord;
78
+ static validateJSON(data: unknown, log: Logger): SerializedMarcRecord;
78
79
  getControlFields(): ControlField[];
79
80
  getControlField(tag: string): ControlField | undefined;
80
81
  getDataFields(tag?: string | RegExp, indicators?: Indicators): DataField[];
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { XmlElement } from '@biblioteksentralen/xml-utils';
2
+ import { Logger } from 'ts-log';
2
3
  import { z } from 'zod';
3
4
 
4
5
  type SerializedMarcField = SerializedDataField | SerializedControlField;
@@ -73,8 +74,8 @@ declare class MarcRecord {
73
74
  fields: MarcField[];
74
75
  format?: string;
75
76
  });
76
- static fromJSON(data: unknown): MarcRecord;
77
- static validateJSON(data: unknown): SerializedMarcRecord;
77
+ static fromJSON(data: unknown, log: Logger): MarcRecord;
78
+ static validateJSON(data: unknown, log: Logger): SerializedMarcRecord;
78
79
  getControlFields(): ControlField[];
79
80
  getControlField(tag: string): ControlField | undefined;
80
81
  getDataFields(tag?: string | RegExp, indicators?: Indicators): DataField[];
package/dist/index.js CHANGED
@@ -197,9 +197,10 @@ var MarcRecord = class _MarcRecord {
197
197
  this.leader = leader;
198
198
  this.fields = fields;
199
199
  }
200
- static fromJSON(data) {
200
+ static fromJSON(data, log) {
201
201
  const { format, leader, fields } = this.validateJSON(
202
- fixInvalidMarcRecordSerialization(data)
202
+ fixInvalidMarcRecordSerialization(data),
203
+ log
203
204
  );
204
205
  return new _MarcRecord({
205
206
  leader,
@@ -209,10 +210,16 @@ var MarcRecord = class _MarcRecord {
209
210
  )
210
211
  });
211
212
  }
212
- static validateJSON(data) {
213
+ static validateJSON(data, log) {
213
214
  if (validator(data)) {
214
215
  return data;
215
216
  }
217
+ log.error(
218
+ `MarcRecord validation failed:
219
+ ${validator.errors ? JSON.stringify(validator.errors) : "Unknown error"}.
220
+ Data:
221
+ ${JSON.stringify(data)}`
222
+ );
216
223
  throw new ValidationFailed(validator.errors ?? []);
217
224
  }
218
225
  getControlFields() {
@@ -344,7 +351,7 @@ function serializeLineMarc(input) {
344
351
  `;
345
352
  }
346
353
  var serializer = {
347
- leader: (leader) => `*LDR ${leader}
354
+ leader: (leader) => `*LDR${leader}
348
355
  `,
349
356
  controlfield: (field) => `*${field.tag}${field.value}
350
357
  `,
@@ -359,7 +366,7 @@ var serializer = {
359
366
  }
360
367
  };
361
368
  var escapeSubfieldValue = (value) => {
362
- return value.replace(/\$/g, "$$");
369
+ return value.replace(/\n/g, " ").replace(/\$/g, "@{2}");
363
370
  };
364
371
  function serializeMarcXml(input, pretty = false) {
365
372
  const fields = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biblioteksentralen/marc",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "MARC record parser and serializer",
@@ -28,23 +28,23 @@
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.1"
31
+ "@biblioteksentralen/xml-utils": "^0.0.3"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@arethetypeswrong/cli": "^0.15.4",
35
35
  "@types/json-schema": "^7.0.15",
36
- "@types/node": "^20.14.14",
37
- "@vitest/coverage-v8": "^1.5.0",
36
+ "@types/node": "^22.15.3",
37
+ "eslint": "^9.26.0",
38
38
  "rimraf": "^5.0.5",
39
39
  "tsup": "^8.0.2",
40
40
  "typescript": "^5.6.2",
41
- "vitest": "^1.5.0",
41
+ "vitest": "^3.1.2",
42
42
  "@dataplattform/eslint-config": "1.0.0"
43
43
  },
44
44
  "scripts": {
45
- "dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput",
45
+ "dev": "tsup src/index.ts --format cjs,esm --dts --treeshake --watch",
46
46
  "build": "tsup src/index.ts --format cjs,esm --dts --treeshake",
47
- "test": "vitest run --poolOptions.threads.singleThread --reporter=verbose --coverage",
47
+ "test": "vitest run --poolOptions.threads.singleThread --reporter=verbose",
48
48
  "test:watch": "vitest",
49
49
  "clean": "rimraf dist",
50
50
  "lint": "eslint .",