@natlibfi/marc-record-validators-melinda 10.10.0 → 10.11.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/sync-007-and-300.js +124 -0
- package/dist/sync-007-and-300.js.map +1 -0
- package/dist/sync-007-and-300.spec.js +51 -0
- package/dist/sync-007-and-300.spec.js.map +1 -0
- package/package.json +1 -1
- package/src/sync-007-and-300.js +127 -0
- package/src/sync-007-and-300.spec.js +52 -0
- package/test-fixtures/sync-007-and-300/f01/expectedResult.json +16 -0
- package/test-fixtures/sync-007-and-300/f01/metadata.json +7 -0
- package/test-fixtures/sync-007-and-300/f01/record.json +15 -0
- package/test-fixtures/sync-007-and-300/v01/expectedResult.json +6 -0
- package/test-fixtures/sync-007-and-300/v01/metadata.json +6 -0
- package/test-fixtures/sync-007-and-300/v01/record.json +15 -0
- package/test-fixtures/sync-007-and-300/v02/expectedResult.json +5 -0
- package/test-fixtures/sync-007-and-300/v02/metadata.json +6 -0
- package/test-fixtures/sync-007-and-300/v02/record.json +14 -0
- package/test-fixtures/sync-007-and-300/v03/expectedResult.json +6 -0
- package/test-fixtures/sync-007-and-300/v03/metadata.json +6 -0
- package/test-fixtures/sync-007-and-300/v03/record.json +15 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = _default;
|
|
7
|
+
var _utils = require("./utils");
|
|
8
|
+
var _clone = _interopRequireDefault(require("clone"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
//import createDebugLogger from 'debug';
|
|
11
|
+
|
|
12
|
+
//const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/sanitize-vocabulary-source-codes);
|
|
13
|
+
// Author(s): Nicholas Volk, Joni Ollila
|
|
14
|
+
function _default() {
|
|
15
|
+
return {
|
|
16
|
+
description: 'Validator for updating mismatching f007 based on f300 (DVD/Bluray) (MRA-613)',
|
|
17
|
+
validate,
|
|
18
|
+
fix
|
|
19
|
+
};
|
|
20
|
+
function fix(record) {
|
|
21
|
+
const res = {
|
|
22
|
+
message: [],
|
|
23
|
+
fix: [],
|
|
24
|
+
valid: true
|
|
25
|
+
};
|
|
26
|
+
record.fields.forEach(f => fixField(f, record));
|
|
27
|
+
return res;
|
|
28
|
+
}
|
|
29
|
+
function validate(record) {
|
|
30
|
+
const res = {
|
|
31
|
+
message: []
|
|
32
|
+
};
|
|
33
|
+
record.fields.forEach(field => {
|
|
34
|
+
validateField(field, res, record);
|
|
35
|
+
});
|
|
36
|
+
res.valid = !(res.message.length >= 1); // eslint-disable-line functional/immutable-data
|
|
37
|
+
return res;
|
|
38
|
+
}
|
|
39
|
+
function validateField(field, res, record) {
|
|
40
|
+
if (field.tag !== '007') {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const orig = (0, _utils.fieldToString)(field);
|
|
44
|
+
const normalizedField = fixField((0, _clone.default)(field), record);
|
|
45
|
+
const mod = (0, _utils.fieldToString)(normalizedField);
|
|
46
|
+
if (orig !== mod) {
|
|
47
|
+
// Fail as the input is "broken"/"crap"/sumthing
|
|
48
|
+
res.message.push(`FIXABLE: '${orig}' => '${mod}'`); // eslint-disable-line functional/immutable-data
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/*
|
|
53
|
+
if (fieldHasUnfixableStuff(field)) {
|
|
54
|
+
res.message.push(`CAN'T BE FIXED AUTOMATICALLY: '${orig}'`); // eslint-disable-line functional/immutable-data
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
*/
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
function fieldIsBluray007(field) {
|
|
61
|
+
if (field.tag !== '007') {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return field.value.match(/^v...s/u);
|
|
65
|
+
}
|
|
66
|
+
function fieldIsDvd007(field) {
|
|
67
|
+
if (field.tag !== '007') {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return field.value.match(/^v...v/u);
|
|
71
|
+
}
|
|
72
|
+
function fieldIsBluray300(field) {
|
|
73
|
+
if (field.tag !== '300') {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return field.subfields.some(subfield => subfield.value.match(/(?:Blu.?ray|BD)/ui));
|
|
77
|
+
}
|
|
78
|
+
function fieldIsDvd300(field) {
|
|
79
|
+
if (field.tag !== '300') {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return field.subfields.some(subfield => subfield.value.match(/DVD/ui));
|
|
83
|
+
}
|
|
84
|
+
function recordHasBluray300(record) {
|
|
85
|
+
return record.fields.some(f => fieldIsBluray300(f));
|
|
86
|
+
}
|
|
87
|
+
function recordHasDvd300(record) {
|
|
88
|
+
return record.fields.some(f => fieldIsDvd300(f));
|
|
89
|
+
}
|
|
90
|
+
function convert007BlurayToDvd(field) {
|
|
91
|
+
if (!fieldIsBluray007(field)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
//field.value = field.value.substring(0, 4) + 's' + field.value.substring(5); // eslint-disable-line functional/immutable-data
|
|
95
|
+
//field.value = field.value.replace(/^(?:v...)s/u, `${1}v`); // eslint-disable-line functional/immutable-data, no-template-curly-in-string
|
|
96
|
+
field.value = `${field.value.substring(0, 4)}v${field.value.substring(5)}`; // eslint-disable-line functional/immutable-data
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function convert007DvdToBluray(field) {
|
|
100
|
+
if (!fieldIsDvd007(field)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
//field.value = field.value.replace(/^(?:v...)v/u, `${1}s`); // eslint-disable-line functional/immutable-data
|
|
104
|
+
field.value = `${field.value.substring(0, 4)}s${field.value.substring(5)}`; // eslint-disable-line functional/immutable-data
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function fixField(field, record) {
|
|
108
|
+
if (field.tag !== '007') {
|
|
109
|
+
return field;
|
|
110
|
+
}
|
|
111
|
+
if (fieldIsDvd007(field) && recordHasBluray300(record) && !recordHasDvd300(record)) {
|
|
112
|
+
// FIX 007: DVD -> Blu-ray
|
|
113
|
+
convert007DvdToBluray(field);
|
|
114
|
+
return field;
|
|
115
|
+
}
|
|
116
|
+
if (fieldIsBluray007(field) && recordHasDvd300(record) && !recordHasBluray300(record)) {
|
|
117
|
+
// FIX 007: Blu-Ray -> DVD
|
|
118
|
+
convert007BlurayToDvd(field);
|
|
119
|
+
return field;
|
|
120
|
+
}
|
|
121
|
+
return field;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=sync-007-and-300.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-007-and-300.js","names":["_utils","require","_clone","_interopRequireDefault","obj","__esModule","default","_default","description","validate","fix","record","res","message","valid","fields","forEach","f","fixField","field","validateField","length","tag","orig","fieldToString","normalizedField","clone","mod","push","fieldIsBluray007","value","match","fieldIsDvd007","fieldIsBluray300","subfields","some","subfield","fieldIsDvd300","recordHasBluray300","recordHasDvd300","convert007BlurayToDvd","substring","convert007DvdToBluray"],"sources":["../src/sync-007-and-300.js"],"sourcesContent":["//import createDebugLogger from 'debug';\nimport {fieldToString} from './utils';\nimport clone from 'clone';\n\n//const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/sanitize-vocabulary-source-codes);\n\n// Author(s): Nicholas Volk, Joni Ollila\nexport default function () {\n\n return {\n description: 'Validator for updating mismatching f007 based on f300 (DVD/Bluray) (MRA-613)',\n validate, fix\n };\n\n function fix(record) {\n const res = {message: [], fix: [], valid: true};\n record.fields.forEach(f => fixField(f, record));\n return res;\n }\n\n function validate(record) {\n const res = {message: []};\n\n record.fields.forEach(field => {\n validateField(field, res, record);\n });\n\n res.valid = !(res.message.length >= 1); // eslint-disable-line functional/immutable-data\n return res;\n }\n\n function validateField(field, res, record) {\n if (field.tag !== '007') {\n return;\n }\n const orig = fieldToString(field);\n\n const normalizedField = fixField(clone(field), record);\n const mod = fieldToString(normalizedField);\n if (orig !== mod) { // Fail as the input is \"broken\"/\"crap\"/sumthing\n res.message.push(`FIXABLE: '${orig}' => '${mod}'`); // eslint-disable-line functional/immutable-data\n return;\n }\n\n /*\n if (fieldHasUnfixableStuff(field)) {\n res.message.push(`CAN'T BE FIXED AUTOMATICALLY: '${orig}'`); // eslint-disable-line functional/immutable-data\n return;\n }\n */\n return;\n }\n\n function fieldIsBluray007(field) {\n if (field.tag !== '007') {\n return false;\n }\n return field.value.match(/^v...s/u);\n }\n\n function fieldIsDvd007(field) {\n if (field.tag !== '007') {\n return false;\n }\n return field.value.match(/^v...v/u);\n }\n\n function fieldIsBluray300(field) {\n if (field.tag !== '300') {\n return false;\n }\n return field.subfields.some(subfield => subfield.value.match(/(?:Blu.?ray|BD)/ui));\n }\n\n function fieldIsDvd300(field) {\n if (field.tag !== '300') {\n return false;\n }\n return field.subfields.some(subfield => subfield.value.match(/DVD/ui));\n }\n\n function recordHasBluray300(record) {\n return record.fields.some(f => fieldIsBluray300(f));\n }\n\n function recordHasDvd300(record) {\n return record.fields.some(f => fieldIsDvd300(f));\n }\n\n function convert007BlurayToDvd(field) {\n if (!fieldIsBluray007(field)) {\n return;\n }\n //field.value = field.value.substring(0, 4) + 's' + field.value.substring(5); // eslint-disable-line functional/immutable-data\n //field.value = field.value.replace(/^(?:v...)s/u, `${1}v`); // eslint-disable-line functional/immutable-data, no-template-curly-in-string\n field.value = `${field.value.substring(0, 4)}v${field.value.substring(5)}`; // eslint-disable-line functional/immutable-data\n }\n\n function convert007DvdToBluray(field) {\n if (!fieldIsDvd007(field)) {\n return;\n }\n //field.value = field.value.replace(/^(?:v...)v/u, `${1}s`); // eslint-disable-line functional/immutable-data\n field.value = `${field.value.substring(0, 4)}s${field.value.substring(5)}`; // eslint-disable-line functional/immutable-data\n }\n\n function fixField(field, record) {\n if (field.tag !== '007') {\n return field;\n }\n\n if (fieldIsDvd007(field) && recordHasBluray300(record) && !recordHasDvd300(record)) {\n // FIX 007: DVD -> Blu-ray\n convert007DvdToBluray(field);\n return field;\n }\n\n if (fieldIsBluray007(field) && recordHasDvd300(record) && !recordHasBluray300(record)) {\n // FIX 007: Blu-Ray -> DVD\n convert007BlurayToDvd(field);\n return field;\n }\n\n return field;\n }\n\n}\n"],"mappings":";;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAC,sBAAA,CAAAF,OAAA;AAA0B,SAAAE,uBAAAC,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAF1B;;AAIA;AAEA;AACe,SAAAG,SAAA,EAAY;EAEzB,OAAO;IACLC,WAAW,EAAE,8EAA8E;IAC3FC,QAAQ;IAAEC;EACZ,CAAC;EAED,SAASA,GAAGA,CAACC,MAAM,EAAE;IACnB,MAAMC,GAAG,GAAG;MAACC,OAAO,EAAE,EAAE;MAAEH,GAAG,EAAE,EAAE;MAAEI,KAAK,EAAE;IAAI,CAAC;IAC/CH,MAAM,CAACI,MAAM,CAACC,OAAO,CAACC,CAAC,IAAIC,QAAQ,CAACD,CAAC,EAAEN,MAAM,CAAC,CAAC;IAC/C,OAAOC,GAAG;EACZ;EAEA,SAASH,QAAQA,CAACE,MAAM,EAAE;IACxB,MAAMC,GAAG,GAAG;MAACC,OAAO,EAAE;IAAE,CAAC;IAEzBF,MAAM,CAACI,MAAM,CAACC,OAAO,CAACG,KAAK,IAAI;MAC7BC,aAAa,CAACD,KAAK,EAAEP,GAAG,EAAED,MAAM,CAAC;IACnC,CAAC,CAAC;IAEFC,GAAG,CAACE,KAAK,GAAG,EAAEF,GAAG,CAACC,OAAO,CAACQ,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC;IACxC,OAAOT,GAAG;EACZ;E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AAC,EAAE;MAClF;MACA+B,qBAAqB,CAACvB,KAAK,CAAC;MAC5B,OAAOA,KAAK;IACd;IAEA,IAAIU,gBAAgB,CAACV,KAAK,CAAC,IAAIoB,eAAe,CAAC5B,MAAM,CAAC,IAAI,CAAC2B,kBAAkB,CAAC3B,MAAM,CAAC,EAAE;MACrF;MACA6B,qBAAqB,CAACrB,KAAK,CAAC;MAC5B,OAAOA,KAAK;IACd;IAEA,OAAOA,KAAK;EACd;AAEF"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _chai = require("chai");
|
|
4
|
+
var _marcRecord = require("@natlibfi/marc-record");
|
|
5
|
+
var _sync007And = _interopRequireDefault(require("./sync-007-and-300"));
|
|
6
|
+
var _fixura = require("@natlibfi/fixura");
|
|
7
|
+
var _fixugen = _interopRequireDefault(require("@natlibfi/fixugen"));
|
|
8
|
+
var _debug = _interopRequireDefault(require("debug"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
(0, _fixugen.default)({
|
|
11
|
+
callback,
|
|
12
|
+
path: [__dirname, '..', 'test-fixtures', 'sync-007-and-300'],
|
|
13
|
+
useMetadataFile: true,
|
|
14
|
+
recurse: false,
|
|
15
|
+
fixura: {
|
|
16
|
+
reader: _fixura.READERS.JSON
|
|
17
|
+
},
|
|
18
|
+
mocha: {
|
|
19
|
+
before: () => testValidatorFactory()
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const debug = (0, _debug.default)('@natlibfi/marc-record-validators-melinda/sync-007-and-300:test');
|
|
23
|
+
async function testValidatorFactory() {
|
|
24
|
+
const validator = await (0, _sync007And.default)();
|
|
25
|
+
(0, _chai.expect)(validator).to.be.an('object').that.has.any.keys('description', 'validate');
|
|
26
|
+
(0, _chai.expect)(validator.description).to.be.a('string');
|
|
27
|
+
(0, _chai.expect)(validator.validate).to.be.a('function');
|
|
28
|
+
}
|
|
29
|
+
async function callback({
|
|
30
|
+
getFixture,
|
|
31
|
+
enabled = true,
|
|
32
|
+
fix = false
|
|
33
|
+
}) {
|
|
34
|
+
if (enabled === false) {
|
|
35
|
+
debug('TEST SKIPPED!');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const validator = await (0, _sync007And.default)();
|
|
39
|
+
const record = new _marcRecord.MarcRecord(getFixture('record.json'));
|
|
40
|
+
const expectedResult = getFixture('expectedResult.json');
|
|
41
|
+
// console.log(expectedResult); // eslint-disable-line
|
|
42
|
+
|
|
43
|
+
if (!fix) {
|
|
44
|
+
const result = await validator.validate(record);
|
|
45
|
+
(0, _chai.expect)(result).to.eql(expectedResult);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
await validator.fix(record);
|
|
49
|
+
(0, _chai.expect)(record).to.eql(expectedResult);
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=sync-007-and-300.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-007-and-300.spec.js","names":["_chai","require","_marcRecord","_sync007And","_interopRequireDefault","_fixura","_fixugen","_debug","obj","__esModule","default","generateTests","callback","path","__dirname","useMetadataFile","recurse","fixura","reader","READERS","JSON","mocha","before","testValidatorFactory","debug","createDebugLogger","validator","validatorFactory","expect","to","be","an","that","has","any","keys","description","a","validate","getFixture","enabled","fix","record","MarcRecord","expectedResult","result","eql"],"sources":["../src/sync-007-and-300.spec.js"],"sourcesContent":["import {expect} from 'chai';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport validatorFactory from './sync-007-and-300';\nimport {READERS} from '@natlibfi/fixura';\nimport generateTests from '@natlibfi/fixugen';\nimport createDebugLogger from 'debug';\n\ngenerateTests({\n callback,\n path: [__dirname, '..', 'test-fixtures', 'sync-007-and-300'],\n useMetadataFile: true,\n recurse: false,\n fixura: {\n reader: READERS.JSON\n },\n mocha: {\n before: () => testValidatorFactory()\n }\n});\nconst debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/sync-007-and-300:test');\n\nasync function testValidatorFactory() {\n const validator = await validatorFactory();\n\n expect(validator)\n .to.be.an('object')\n .that.has.any.keys('description', 'validate');\n\n expect(validator.description).to.be.a('string');\n expect(validator.validate).to.be.a('function');\n}\n\nasync function callback({getFixture, enabled = true, fix = false}) {\n if (enabled === false) {\n debug('TEST SKIPPED!');\n return;\n }\n\n const validator = await validatorFactory();\n const record = new MarcRecord(getFixture('record.json'));\n const expectedResult = getFixture('expectedResult.json');\n // console.log(expectedResult); // eslint-disable-line\n\n if (!fix) {\n const result = await validator.validate(record);\n expect(result).to.eql(expectedResult);\n return;\n }\n\n await validator.fix(record);\n expect(record).to.eql(expectedResult);\n}\n"],"mappings":";;AAAA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAC,sBAAA,CAAAH,OAAA;AACA,IAAAI,OAAA,GAAAJ,OAAA;AACA,IAAAK,QAAA,GAAAF,sBAAA,CAAAH,OAAA;AACA,IAAAM,MAAA,GAAAH,sBAAA,CAAAH,OAAA;AAAsC,SAAAG,uBAAAI,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAEtC,IAAAG,gBAAa,EAAC;EACZC,QAAQ;EACRC,IAAI,EAAE,CAACC,SAAS,EAAE,IAAI,EAAE,eAAe,EAAE,kBAAkB,CAAC;EAC5DC,eAAe,EAAE,IAAI;EACrBC,OAAO,EAAE,KAAK;EACdC,MAAM,EAAE;IACNC,MAAM,EAAEC,eAAO,CAACC;EAClB,CAAC;EACDC,KAAK,EAAE;IACLC,MAAM,EAAEA,CAAA,KAAMC,oBAAoB,CAAC;EACrC;AACF,CAAC,CAAC;AACF,MAAMC,KAAK,GAAG,IAAAC,cAAiB,EAAC,gEAAgE,CAAC;AAEjG,eAAeF,oBAAoBA,CAAA,EAAG;EACpC,MAAMG,SAAS,G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ieldToString} from './utils';
|
|
3
|
+
import clone from 'clone';
|
|
4
|
+
|
|
5
|
+
//const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/sanitize-vocabulary-source-codes);
|
|
6
|
+
|
|
7
|
+
// Author(s): Nicholas Volk, Joni Ollila
|
|
8
|
+
export default function () {
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
description: 'Validator for updating mismatching f007 based on f300 (DVD/Bluray) (MRA-613)',
|
|
12
|
+
validate, fix
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function fix(record) {
|
|
16
|
+
const res = {message: [], fix: [], valid: true};
|
|
17
|
+
record.fields.forEach(f => fixField(f, record));
|
|
18
|
+
return res;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function validate(record) {
|
|
22
|
+
const res = {message: []};
|
|
23
|
+
|
|
24
|
+
record.fields.forEach(field => {
|
|
25
|
+
validateField(field, res, record);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
res.valid = !(res.message.length >= 1); // eslint-disable-line functional/immutable-data
|
|
29
|
+
return res;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function validateField(field, res, record) {
|
|
33
|
+
if (field.tag !== '007') {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const orig = fieldToString(field);
|
|
37
|
+
|
|
38
|
+
const normalizedField = fixField(clone(field), record);
|
|
39
|
+
const mod = fieldToString(normalizedField);
|
|
40
|
+
if (orig !== mod) { // Fail as the input is "broken"/"crap"/sumthing
|
|
41
|
+
res.message.push(`FIXABLE: '${orig}' => '${mod}'`); // eslint-disable-line functional/immutable-data
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/*
|
|
46
|
+
if (fieldHasUnfixableStuff(field)) {
|
|
47
|
+
res.message.push(`CAN'T BE FIXED AUTOMATICALLY: '${orig}'`); // eslint-disable-line functional/immutable-data
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
*/
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function fieldIsBluray007(field) {
|
|
55
|
+
if (field.tag !== '007') {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return field.value.match(/^v...s/u);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function fieldIsDvd007(field) {
|
|
62
|
+
if (field.tag !== '007') {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
return field.value.match(/^v...v/u);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function fieldIsBluray300(field) {
|
|
69
|
+
if (field.tag !== '300') {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return field.subfields.some(subfield => subfield.value.match(/(?:Blu.?ray|BD)/ui));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function fieldIsDvd300(field) {
|
|
76
|
+
if (field.tag !== '300') {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return field.subfields.some(subfield => subfield.value.match(/DVD/ui));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function recordHasBluray300(record) {
|
|
83
|
+
return record.fields.some(f => fieldIsBluray300(f));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function recordHasDvd300(record) {
|
|
87
|
+
return record.fields.some(f => fieldIsDvd300(f));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function convert007BlurayToDvd(field) {
|
|
91
|
+
if (!fieldIsBluray007(field)) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
//field.value = field.value.substring(0, 4) + 's' + field.value.substring(5); // eslint-disable-line functional/immutable-data
|
|
95
|
+
//field.value = field.value.replace(/^(?:v...)s/u, `${1}v`); // eslint-disable-line functional/immutable-data, no-template-curly-in-string
|
|
96
|
+
field.value = `${field.value.substring(0, 4)}v${field.value.substring(5)}`; // eslint-disable-line functional/immutable-data
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function convert007DvdToBluray(field) {
|
|
100
|
+
if (!fieldIsDvd007(field)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
//field.value = field.value.replace(/^(?:v...)v/u, `${1}s`); // eslint-disable-line functional/immutable-data
|
|
104
|
+
field.value = `${field.value.substring(0, 4)}s${field.value.substring(5)}`; // eslint-disable-line functional/immutable-data
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function fixField(field, record) {
|
|
108
|
+
if (field.tag !== '007') {
|
|
109
|
+
return field;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (fieldIsDvd007(field) && recordHasBluray300(record) && !recordHasDvd300(record)) {
|
|
113
|
+
// FIX 007: DVD -> Blu-ray
|
|
114
|
+
convert007DvdToBluray(field);
|
|
115
|
+
return field;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (fieldIsBluray007(field) && recordHasDvd300(record) && !recordHasBluray300(record)) {
|
|
119
|
+
// FIX 007: Blu-Ray -> DVD
|
|
120
|
+
convert007BlurayToDvd(field);
|
|
121
|
+
return field;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return field;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {expect} from 'chai';
|
|
2
|
+
import {MarcRecord} from '@natlibfi/marc-record';
|
|
3
|
+
import validatorFactory from './sync-007-and-300';
|
|
4
|
+
import {READERS} from '@natlibfi/fixura';
|
|
5
|
+
import generateTests from '@natlibfi/fixugen';
|
|
6
|
+
import createDebugLogger from 'debug';
|
|
7
|
+
|
|
8
|
+
generateTests({
|
|
9
|
+
callback,
|
|
10
|
+
path: [__dirname, '..', 'test-fixtures', 'sync-007-and-300'],
|
|
11
|
+
useMetadataFile: true,
|
|
12
|
+
recurse: false,
|
|
13
|
+
fixura: {
|
|
14
|
+
reader: READERS.JSON
|
|
15
|
+
},
|
|
16
|
+
mocha: {
|
|
17
|
+
before: () => testValidatorFactory()
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/sync-007-and-300:test');
|
|
21
|
+
|
|
22
|
+
async function testValidatorFactory() {
|
|
23
|
+
const validator = await validatorFactory();
|
|
24
|
+
|
|
25
|
+
expect(validator)
|
|
26
|
+
.to.be.an('object')
|
|
27
|
+
.that.has.any.keys('description', 'validate');
|
|
28
|
+
|
|
29
|
+
expect(validator.description).to.be.a('string');
|
|
30
|
+
expect(validator.validate).to.be.a('function');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function callback({getFixture, enabled = true, fix = false}) {
|
|
34
|
+
if (enabled === false) {
|
|
35
|
+
debug('TEST SKIPPED!');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const validator = await validatorFactory();
|
|
40
|
+
const record = new MarcRecord(getFixture('record.json'));
|
|
41
|
+
const expectedResult = getFixture('expectedResult.json');
|
|
42
|
+
// console.log(expectedResult); // eslint-disable-line
|
|
43
|
+
|
|
44
|
+
if (!fix) {
|
|
45
|
+
const result = await validator.validate(record);
|
|
46
|
+
expect(result).to.eql(expectedResult);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await validator.fix(record);
|
|
51
|
+
expect(record).to.eql(expectedResult);
|
|
52
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_validationOptions": {},
|
|
3
|
+
"fields": [
|
|
4
|
+
{ "tag": "001", "value": "f01" },
|
|
5
|
+
{ "tag": "007", "value": "vd|csaizq" },
|
|
6
|
+
{ "tag": "007", "value": "vd|cs||||" },
|
|
7
|
+
|
|
8
|
+
{ "tag": "300", "ind1": " ", "ind2": " ", "subfields": [
|
|
9
|
+
{ "code": "a", "value": "1 Blu-ray-videolevy (2 h 15 min) :"},
|
|
10
|
+
{ "code": "b", "value": "värillinen, ääni ;"},
|
|
11
|
+
{ "code": "c", "value": "12 cm"}
|
|
12
|
+
]}
|
|
13
|
+
|
|
14
|
+
],
|
|
15
|
+
"leader": ""
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"fields": [
|
|
3
|
+
{ "tag": "001", "value": "f01" },
|
|
4
|
+
{ "tag": "007", "value": "vd|csaizq" },
|
|
5
|
+
{ "tag": "007", "value": "vd|cv||||" },
|
|
6
|
+
|
|
7
|
+
{ "tag": "300", "ind1": " ", "ind2": " ", "subfields": [
|
|
8
|
+
{ "code": "a", "value": "1 Blu-ray-videolevy (2 h 15 min) :"},
|
|
9
|
+
{ "code": "b", "value": "värillinen, ääni ;"},
|
|
10
|
+
{ "code": "c", "value": "12 cm"}
|
|
11
|
+
]}
|
|
12
|
+
|
|
13
|
+
],
|
|
14
|
+
"leader": ""
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"fields": [
|
|
3
|
+
{ "tag": "001", "value": "f01" },
|
|
4
|
+
{ "tag": "007", "value": "vd|csaizq" },
|
|
5
|
+
{ "tag": "007", "value": "vd|cv||||" },
|
|
6
|
+
|
|
7
|
+
{ "tag": "300", "ind1": " ", "ind2": " ", "subfields": [
|
|
8
|
+
{ "code": "a", "value": "1 Blu-ray-videolevy (2 h 15 min) :"},
|
|
9
|
+
{ "code": "b", "value": "värillinen, ääni ;"},
|
|
10
|
+
{ "code": "c", "value": "12 cm"}
|
|
11
|
+
]}
|
|
12
|
+
|
|
13
|
+
],
|
|
14
|
+
"leader": ""
|
|
15
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"fields": [
|
|
3
|
+
{ "tag": "001", "value": "f01" },
|
|
4
|
+
{ "tag": "007", "value": "vd|csaizq" },
|
|
5
|
+
|
|
6
|
+
{ "tag": "300", "ind1": " ", "ind2": " ", "subfields": [
|
|
7
|
+
{ "code": "a", "value": "1 Blu-ray-videolevy (2 h 15 min) :"},
|
|
8
|
+
{ "code": "b", "value": "värillinen, ääni ;"},
|
|
9
|
+
{ "code": "c", "value": "12 cm"}
|
|
10
|
+
]}
|
|
11
|
+
|
|
12
|
+
],
|
|
13
|
+
"leader": ""
|
|
14
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"fields": [
|
|
3
|
+
{ "tag": "001", "value": "f01" },
|
|
4
|
+
{ "tag": "007", "value": "vd|csaizq" },
|
|
5
|
+
{ "tag": "007", "value": "vd|cv||||" },
|
|
6
|
+
|
|
7
|
+
{ "tag": "300", "ind1": " ", "ind2": " ", "subfields": [
|
|
8
|
+
{ "code": "a", "value": "DVD (2 h 15 min) :"},
|
|
9
|
+
{ "code": "b", "value": "värillinen, ääni ;"},
|
|
10
|
+
{ "code": "c", "value": "12 cm"}
|
|
11
|
+
]}
|
|
12
|
+
|
|
13
|
+
],
|
|
14
|
+
"leader": ""
|
|
15
|
+
}
|