@biblioteksentralen/marc 0.1.2 → 0.3.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/README.md +5 -0
- package/dist/index.cjs +15 -12
- package/dist/index.d.cts +15 -67
- package/dist/index.d.ts +15 -67
- package/dist/index.js +15 -12
- package/package.json +2 -3
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -59,8 +59,14 @@ var DataField = class {
|
|
|
59
59
|
subfields: this.subfields.map((subfield) => subfield.toJSON())
|
|
60
60
|
};
|
|
61
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns a human-readable string representation of this MARC data field.
|
|
64
|
+
*
|
|
65
|
+
* The result is intended for debugging, logging, and test output. The format is not guaranteed to
|
|
66
|
+
* be stable and should not be parsed or relied upon.
|
|
67
|
+
*/
|
|
62
68
|
toString() {
|
|
63
|
-
return `${this.tag} ${this.subfields.map((subfield) => subfield.toString()).join(" ")}`;
|
|
69
|
+
return `${this.tag} ${this.ind1 ?? " "}${this.ind2 ?? " "} ${this.subfields.map((subfield) => subfield.toString()).join(" ")}`;
|
|
64
70
|
}
|
|
65
71
|
};
|
|
66
72
|
var isControlField = (field) => field instanceof ControlField;
|
|
@@ -180,9 +186,10 @@ function createMarcSchema({
|
|
|
180
186
|
|
|
181
187
|
// src/marc-record/errors.ts
|
|
182
188
|
var MarcRecordSchemaValidationError = class extends Error {
|
|
183
|
-
constructor(errors) {
|
|
189
|
+
constructor(errors, sourceData) {
|
|
184
190
|
super("MarcRecord schema validation failed");
|
|
185
191
|
this.errors = errors;
|
|
192
|
+
this.sourceData = sourceData;
|
|
186
193
|
}
|
|
187
194
|
};
|
|
188
195
|
var MarcParseError = class extends Error {
|
|
@@ -281,10 +288,9 @@ var MarcRecord = class _MarcRecord {
|
|
|
281
288
|
this.leader = leader;
|
|
282
289
|
this.fields = fields;
|
|
283
290
|
}
|
|
284
|
-
static fromJSON(data
|
|
291
|
+
static fromJSON(data) {
|
|
285
292
|
const { format, leader, fields } = this.validateJSON(
|
|
286
|
-
fixInvalidMarcRecordSerialization(data)
|
|
287
|
-
log
|
|
293
|
+
fixInvalidMarcRecordSerialization(data)
|
|
288
294
|
);
|
|
289
295
|
return new _MarcRecord({
|
|
290
296
|
leader,
|
|
@@ -310,17 +316,14 @@ var MarcRecord = class _MarcRecord {
|
|
|
310
316
|
toLineMarc() {
|
|
311
317
|
return serializeLineMarc(this);
|
|
312
318
|
}
|
|
313
|
-
static validateJSON(data
|
|
319
|
+
static validateJSON(data) {
|
|
314
320
|
if (validator(data)) {
|
|
315
321
|
return data;
|
|
316
322
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
Data:
|
|
321
|
-
${JSON.stringify(data)}`
|
|
323
|
+
throw new MarcRecordSchemaValidationError(
|
|
324
|
+
validator.errors ?? [],
|
|
325
|
+
JSON.stringify(data)
|
|
322
326
|
);
|
|
323
|
-
throw new MarcRecordSchemaValidationError(validator.errors ?? []);
|
|
324
327
|
}
|
|
325
328
|
getControlFields() {
|
|
326
329
|
return this.fields.filter(isControlField);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { XmlElement } from '@biblioteksentralen/xml-utils';
|
|
2
|
-
import { Logger } from 'ts-log';
|
|
3
2
|
import { z } from 'zod';
|
|
4
3
|
import { ErrorObject } from 'ajv';
|
|
5
4
|
|
|
@@ -42,6 +41,12 @@ declare class DataField {
|
|
|
42
41
|
getFirstSubfield(code: string | RegExp): Subfield | undefined;
|
|
43
42
|
getFirstSubfieldValue(code: string | RegExp): string | undefined;
|
|
44
43
|
toJSON(): SerializedDataField;
|
|
44
|
+
/**
|
|
45
|
+
* Returns a human-readable string representation of this MARC data field.
|
|
46
|
+
*
|
|
47
|
+
* The result is intended for debugging, logging, and test output. The format is not guaranteed to
|
|
48
|
+
* be stable and should not be parsed or relied upon.
|
|
49
|
+
*/
|
|
45
50
|
toString(): string;
|
|
46
51
|
}
|
|
47
52
|
type MarcField = ControlField | DataField;
|
|
@@ -75,12 +80,12 @@ declare class MarcRecord {
|
|
|
75
80
|
fields: MarcField[];
|
|
76
81
|
format?: string;
|
|
77
82
|
});
|
|
78
|
-
static fromJSON(data: unknown
|
|
83
|
+
static fromJSON(data: unknown): MarcRecord;
|
|
79
84
|
toJSON(): SerializedMarcRecord;
|
|
80
85
|
toXML(pretty?: boolean): string;
|
|
81
86
|
toXmlElement(): XmlElement;
|
|
82
87
|
toLineMarc(): string;
|
|
83
|
-
static validateJSON(data: unknown
|
|
88
|
+
static validateJSON(data: unknown): SerializedMarcRecord;
|
|
84
89
|
getControlFields(): ControlField[];
|
|
85
90
|
getControlField(tag: string): ControlField | undefined;
|
|
86
91
|
getDataFields(tag?: string | RegExp, indicators?: Indicators): DataField[];
|
|
@@ -134,77 +139,19 @@ declare function parseMarcXml(input: string | XmlElement, options?: MarcXmlOptio
|
|
|
134
139
|
declare const marcRecordZodSchema: z.ZodObject<{
|
|
135
140
|
format: z.ZodOptional<z.ZodString>;
|
|
136
141
|
leader: z.ZodString;
|
|
137
|
-
fields: z.ZodArray<z.ZodUnion<[z.ZodObject<{
|
|
142
|
+
fields: z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
138
143
|
tag: z.ZodString;
|
|
139
144
|
value: z.ZodString;
|
|
140
|
-
},
|
|
141
|
-
tag: string;
|
|
142
|
-
value: string;
|
|
143
|
-
}, {
|
|
144
|
-
tag: string;
|
|
145
|
-
value: string;
|
|
146
|
-
}>, z.ZodObject<{
|
|
145
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
147
146
|
tag: z.ZodString;
|
|
148
147
|
ind1: z.ZodOptional<z.ZodString>;
|
|
149
148
|
ind2: z.ZodOptional<z.ZodString>;
|
|
150
149
|
subfields: z.ZodArray<z.ZodObject<{
|
|
151
150
|
code: z.ZodString;
|
|
152
151
|
value: z.ZodString;
|
|
153
|
-
},
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}, {
|
|
157
|
-
value: string;
|
|
158
|
-
code: string;
|
|
159
|
-
}>, "many">;
|
|
160
|
-
}, "strip", z.ZodTypeAny, {
|
|
161
|
-
tag: string;
|
|
162
|
-
subfields: {
|
|
163
|
-
value: string;
|
|
164
|
-
code: string;
|
|
165
|
-
}[];
|
|
166
|
-
ind1?: string | undefined;
|
|
167
|
-
ind2?: string | undefined;
|
|
168
|
-
}, {
|
|
169
|
-
tag: string;
|
|
170
|
-
subfields: {
|
|
171
|
-
value: string;
|
|
172
|
-
code: string;
|
|
173
|
-
}[];
|
|
174
|
-
ind1?: string | undefined;
|
|
175
|
-
ind2?: string | undefined;
|
|
176
|
-
}>]>, "many">;
|
|
177
|
-
}, "strip", z.ZodTypeAny, {
|
|
178
|
-
fields: ({
|
|
179
|
-
tag: string;
|
|
180
|
-
value: string;
|
|
181
|
-
} | {
|
|
182
|
-
tag: string;
|
|
183
|
-
subfields: {
|
|
184
|
-
value: string;
|
|
185
|
-
code: string;
|
|
186
|
-
}[];
|
|
187
|
-
ind1?: string | undefined;
|
|
188
|
-
ind2?: string | undefined;
|
|
189
|
-
})[];
|
|
190
|
-
leader: string;
|
|
191
|
-
format?: string | undefined;
|
|
192
|
-
}, {
|
|
193
|
-
fields: ({
|
|
194
|
-
tag: string;
|
|
195
|
-
value: string;
|
|
196
|
-
} | {
|
|
197
|
-
tag: string;
|
|
198
|
-
subfields: {
|
|
199
|
-
value: string;
|
|
200
|
-
code: string;
|
|
201
|
-
}[];
|
|
202
|
-
ind1?: string | undefined;
|
|
203
|
-
ind2?: string | undefined;
|
|
204
|
-
})[];
|
|
205
|
-
leader: string;
|
|
206
|
-
format?: string | undefined;
|
|
207
|
-
}>;
|
|
152
|
+
}, z.core.$strip>>;
|
|
153
|
+
}, z.core.$strip>]>>;
|
|
154
|
+
}, z.core.$strip>;
|
|
208
155
|
|
|
209
156
|
declare function serializeLineMarc(input: MarcRecord): string;
|
|
210
157
|
|
|
@@ -213,7 +160,8 @@ declare function serializeMarcXmlCollection(input: MarcRecord[], pretty?: boolea
|
|
|
213
160
|
|
|
214
161
|
declare class MarcRecordSchemaValidationError extends Error {
|
|
215
162
|
readonly errors: ErrorObject[];
|
|
216
|
-
|
|
163
|
+
readonly sourceData?: string | undefined;
|
|
164
|
+
constructor(errors: ErrorObject[], sourceData?: string | undefined);
|
|
217
165
|
}
|
|
218
166
|
declare class MarcParseError extends Error {
|
|
219
167
|
readonly record: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { XmlElement } from '@biblioteksentralen/xml-utils';
|
|
2
|
-
import { Logger } from 'ts-log';
|
|
3
2
|
import { z } from 'zod';
|
|
4
3
|
import { ErrorObject } from 'ajv';
|
|
5
4
|
|
|
@@ -42,6 +41,12 @@ declare class DataField {
|
|
|
42
41
|
getFirstSubfield(code: string | RegExp): Subfield | undefined;
|
|
43
42
|
getFirstSubfieldValue(code: string | RegExp): string | undefined;
|
|
44
43
|
toJSON(): SerializedDataField;
|
|
44
|
+
/**
|
|
45
|
+
* Returns a human-readable string representation of this MARC data field.
|
|
46
|
+
*
|
|
47
|
+
* The result is intended for debugging, logging, and test output. The format is not guaranteed to
|
|
48
|
+
* be stable and should not be parsed or relied upon.
|
|
49
|
+
*/
|
|
45
50
|
toString(): string;
|
|
46
51
|
}
|
|
47
52
|
type MarcField = ControlField | DataField;
|
|
@@ -75,12 +80,12 @@ declare class MarcRecord {
|
|
|
75
80
|
fields: MarcField[];
|
|
76
81
|
format?: string;
|
|
77
82
|
});
|
|
78
|
-
static fromJSON(data: unknown
|
|
83
|
+
static fromJSON(data: unknown): MarcRecord;
|
|
79
84
|
toJSON(): SerializedMarcRecord;
|
|
80
85
|
toXML(pretty?: boolean): string;
|
|
81
86
|
toXmlElement(): XmlElement;
|
|
82
87
|
toLineMarc(): string;
|
|
83
|
-
static validateJSON(data: unknown
|
|
88
|
+
static validateJSON(data: unknown): SerializedMarcRecord;
|
|
84
89
|
getControlFields(): ControlField[];
|
|
85
90
|
getControlField(tag: string): ControlField | undefined;
|
|
86
91
|
getDataFields(tag?: string | RegExp, indicators?: Indicators): DataField[];
|
|
@@ -134,77 +139,19 @@ declare function parseMarcXml(input: string | XmlElement, options?: MarcXmlOptio
|
|
|
134
139
|
declare const marcRecordZodSchema: z.ZodObject<{
|
|
135
140
|
format: z.ZodOptional<z.ZodString>;
|
|
136
141
|
leader: z.ZodString;
|
|
137
|
-
fields: z.ZodArray<z.ZodUnion<[z.ZodObject<{
|
|
142
|
+
fields: z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
138
143
|
tag: z.ZodString;
|
|
139
144
|
value: z.ZodString;
|
|
140
|
-
},
|
|
141
|
-
tag: string;
|
|
142
|
-
value: string;
|
|
143
|
-
}, {
|
|
144
|
-
tag: string;
|
|
145
|
-
value: string;
|
|
146
|
-
}>, z.ZodObject<{
|
|
145
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
147
146
|
tag: z.ZodString;
|
|
148
147
|
ind1: z.ZodOptional<z.ZodString>;
|
|
149
148
|
ind2: z.ZodOptional<z.ZodString>;
|
|
150
149
|
subfields: z.ZodArray<z.ZodObject<{
|
|
151
150
|
code: z.ZodString;
|
|
152
151
|
value: z.ZodString;
|
|
153
|
-
},
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}, {
|
|
157
|
-
value: string;
|
|
158
|
-
code: string;
|
|
159
|
-
}>, "many">;
|
|
160
|
-
}, "strip", z.ZodTypeAny, {
|
|
161
|
-
tag: string;
|
|
162
|
-
subfields: {
|
|
163
|
-
value: string;
|
|
164
|
-
code: string;
|
|
165
|
-
}[];
|
|
166
|
-
ind1?: string | undefined;
|
|
167
|
-
ind2?: string | undefined;
|
|
168
|
-
}, {
|
|
169
|
-
tag: string;
|
|
170
|
-
subfields: {
|
|
171
|
-
value: string;
|
|
172
|
-
code: string;
|
|
173
|
-
}[];
|
|
174
|
-
ind1?: string | undefined;
|
|
175
|
-
ind2?: string | undefined;
|
|
176
|
-
}>]>, "many">;
|
|
177
|
-
}, "strip", z.ZodTypeAny, {
|
|
178
|
-
fields: ({
|
|
179
|
-
tag: string;
|
|
180
|
-
value: string;
|
|
181
|
-
} | {
|
|
182
|
-
tag: string;
|
|
183
|
-
subfields: {
|
|
184
|
-
value: string;
|
|
185
|
-
code: string;
|
|
186
|
-
}[];
|
|
187
|
-
ind1?: string | undefined;
|
|
188
|
-
ind2?: string | undefined;
|
|
189
|
-
})[];
|
|
190
|
-
leader: string;
|
|
191
|
-
format?: string | undefined;
|
|
192
|
-
}, {
|
|
193
|
-
fields: ({
|
|
194
|
-
tag: string;
|
|
195
|
-
value: string;
|
|
196
|
-
} | {
|
|
197
|
-
tag: string;
|
|
198
|
-
subfields: {
|
|
199
|
-
value: string;
|
|
200
|
-
code: string;
|
|
201
|
-
}[];
|
|
202
|
-
ind1?: string | undefined;
|
|
203
|
-
ind2?: string | undefined;
|
|
204
|
-
})[];
|
|
205
|
-
leader: string;
|
|
206
|
-
format?: string | undefined;
|
|
207
|
-
}>;
|
|
152
|
+
}, z.core.$strip>>;
|
|
153
|
+
}, z.core.$strip>]>>;
|
|
154
|
+
}, z.core.$strip>;
|
|
208
155
|
|
|
209
156
|
declare function serializeLineMarc(input: MarcRecord): string;
|
|
210
157
|
|
|
@@ -213,7 +160,8 @@ declare function serializeMarcXmlCollection(input: MarcRecord[], pretty?: boolea
|
|
|
213
160
|
|
|
214
161
|
declare class MarcRecordSchemaValidationError extends Error {
|
|
215
162
|
readonly errors: ErrorObject[];
|
|
216
|
-
|
|
163
|
+
readonly sourceData?: string | undefined;
|
|
164
|
+
constructor(errors: ErrorObject[], sourceData?: string | undefined);
|
|
217
165
|
}
|
|
218
166
|
declare class MarcParseError extends Error {
|
|
219
167
|
readonly record: string;
|
package/dist/index.js
CHANGED
|
@@ -57,8 +57,14 @@ var DataField = class {
|
|
|
57
57
|
subfields: this.subfields.map((subfield) => subfield.toJSON())
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns a human-readable string representation of this MARC data field.
|
|
62
|
+
*
|
|
63
|
+
* The result is intended for debugging, logging, and test output. The format is not guaranteed to
|
|
64
|
+
* be stable and should not be parsed or relied upon.
|
|
65
|
+
*/
|
|
60
66
|
toString() {
|
|
61
|
-
return `${this.tag} ${this.subfields.map((subfield) => subfield.toString()).join(" ")}`;
|
|
67
|
+
return `${this.tag} ${this.ind1 ?? " "}${this.ind2 ?? " "} ${this.subfields.map((subfield) => subfield.toString()).join(" ")}`;
|
|
62
68
|
}
|
|
63
69
|
};
|
|
64
70
|
var isControlField = (field) => field instanceof ControlField;
|
|
@@ -178,9 +184,10 @@ function createMarcSchema({
|
|
|
178
184
|
|
|
179
185
|
// src/marc-record/errors.ts
|
|
180
186
|
var MarcRecordSchemaValidationError = class extends Error {
|
|
181
|
-
constructor(errors) {
|
|
187
|
+
constructor(errors, sourceData) {
|
|
182
188
|
super("MarcRecord schema validation failed");
|
|
183
189
|
this.errors = errors;
|
|
190
|
+
this.sourceData = sourceData;
|
|
184
191
|
}
|
|
185
192
|
};
|
|
186
193
|
var MarcParseError = class extends Error {
|
|
@@ -279,10 +286,9 @@ var MarcRecord = class _MarcRecord {
|
|
|
279
286
|
this.leader = leader;
|
|
280
287
|
this.fields = fields;
|
|
281
288
|
}
|
|
282
|
-
static fromJSON(data
|
|
289
|
+
static fromJSON(data) {
|
|
283
290
|
const { format, leader, fields } = this.validateJSON(
|
|
284
|
-
fixInvalidMarcRecordSerialization(data)
|
|
285
|
-
log
|
|
291
|
+
fixInvalidMarcRecordSerialization(data)
|
|
286
292
|
);
|
|
287
293
|
return new _MarcRecord({
|
|
288
294
|
leader,
|
|
@@ -308,17 +314,14 @@ var MarcRecord = class _MarcRecord {
|
|
|
308
314
|
toLineMarc() {
|
|
309
315
|
return serializeLineMarc(this);
|
|
310
316
|
}
|
|
311
|
-
static validateJSON(data
|
|
317
|
+
static validateJSON(data) {
|
|
312
318
|
if (validator(data)) {
|
|
313
319
|
return data;
|
|
314
320
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
Data:
|
|
319
|
-
${JSON.stringify(data)}`
|
|
321
|
+
throw new MarcRecordSchemaValidationError(
|
|
322
|
+
validator.errors ?? [],
|
|
323
|
+
JSON.stringify(data)
|
|
320
324
|
);
|
|
321
|
-
throw new MarcRecordSchemaValidationError(validator.errors ?? []);
|
|
322
325
|
}
|
|
323
326
|
getControlFields() {
|
|
324
327
|
return this.fields.filter(isControlField);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biblioteksentralen/marc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "MARC record parser and serializer",
|
|
@@ -26,8 +26,7 @@
|
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"ajv": "^8.17.1",
|
|
29
|
-
"
|
|
30
|
-
"zod": "^3.23.8",
|
|
29
|
+
"zod": "^4.3.6",
|
|
31
30
|
"@biblioteksentralen/xml-utils": "^0.0.3"
|
|
32
31
|
},
|
|
33
32
|
"devDependencies": {
|