@natlibfi/marc-record-validators-melinda 12.0.0 → 12.0.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.
Files changed (229) hide show
  1. package/dist/disambiguateSeriesStatements.test.js +1 -7
  2. package/dist/disambiguateSeriesStatements.test.js.map +2 -2
  3. package/dist/drop-terms.js +122 -0
  4. package/dist/drop-terms.js.map +7 -0
  5. package/dist/drop-terms.test.js +56 -0
  6. package/dist/drop-terms.test.js.map +7 -0
  7. package/dist/field-008-18-34-character-groups.test.js +1 -7
  8. package/dist/field-008-18-34-character-groups.test.js.map +2 -2
  9. package/dist/field-505-separators.test.js +1 -7
  10. package/dist/field-505-separators.test.js.map +2 -2
  11. package/dist/field-521-fix.test.js +1 -7
  12. package/dist/field-521-fix.test.js.map +2 -2
  13. package/dist/fix-33X.test.js +1 -1
  14. package/dist/fix-33X.test.js.map +1 -1
  15. package/dist/fix-country-codes.test.js +1 -7
  16. package/dist/fix-country-codes.test.js.map +2 -2
  17. package/dist/fix-sami-041.js +87 -0
  18. package/dist/fix-sami-041.js.map +7 -0
  19. package/dist/fix-sami-041.test.js +40 -0
  20. package/dist/fix-sami-041.test.js.map +7 -0
  21. package/dist/fixRelatorTerms.test.js +1 -7
  22. package/dist/fixRelatorTerms.test.js.map +2 -2
  23. package/dist/index.js +7 -1
  24. package/dist/index.js.map +2 -2
  25. package/dist/indicator-fixes.js +10 -0
  26. package/dist/indicator-fixes.js.map +2 -2
  27. package/dist/indicator-fixes.test.js +1 -7
  28. package/dist/indicator-fixes.test.js.map +2 -2
  29. package/dist/merge-fields.test.js +2 -7
  30. package/dist/merge-fields.test.js.map +2 -2
  31. package/dist/mergeField500Lisapainokset.test.js +1 -7
  32. package/dist/mergeField500Lisapainokset.test.js.map +2 -2
  33. package/dist/mergeRelatorTermFields.test.js +1 -7
  34. package/dist/mergeRelatorTermFields.test.js.map +2 -2
  35. package/dist/multiple-subfield-0.test.js +1 -7
  36. package/dist/multiple-subfield-0.test.js.map +2 -2
  37. package/dist/normalize-dashes.test.js +1 -7
  38. package/dist/normalize-dashes.test.js.map +2 -2
  39. package/dist/normalize-identifiers.test.js +1 -7
  40. package/dist/normalize-identifiers.test.js.map +2 -2
  41. package/dist/normalize-qualifying-information.test.js +1 -7
  42. package/dist/normalize-qualifying-information.test.js.map +2 -2
  43. package/dist/normalize-utf8-diacritics.test.js +1 -7
  44. package/dist/normalize-utf8-diacritics.test.js.map +2 -2
  45. package/dist/punctuation.test.js +1 -7
  46. package/dist/punctuation.test.js.map +2 -2
  47. package/dist/punctuation2.test.js +1 -7
  48. package/dist/punctuation2.test.js.map +2 -2
  49. package/dist/reindexSubfield6OccurenceNumbers.test.js +1 -7
  50. package/dist/reindexSubfield6OccurenceNumbers.test.js.map +2 -2
  51. package/dist/remove-041-zxx.js +56 -0
  52. package/dist/remove-041-zxx.js.map +7 -0
  53. package/dist/remove-041-zxx.test.js +40 -0
  54. package/dist/remove-041-zxx.test.js.map +7 -0
  55. package/dist/removeDuplicateDataFields.test.js +1 -7
  56. package/dist/removeDuplicateDataFields.test.js.map +2 -2
  57. package/dist/removeInferiorDataFields.js +1 -9
  58. package/dist/removeInferiorDataFields.js.map +2 -2
  59. package/dist/removeInferiorDataFields.test.js +1 -7
  60. package/dist/removeInferiorDataFields.test.js.map +2 -2
  61. package/dist/resolveOrphanedSubfield6s.js +1 -3
  62. package/dist/resolveOrphanedSubfield6s.js.map +2 -2
  63. package/dist/resolveOrphanedSubfield6s.test.js +1 -7
  64. package/dist/resolveOrphanedSubfield6s.test.js.map +2 -2
  65. package/dist/sanitize-vocabulary-source-codes.test.js +1 -7
  66. package/dist/sanitize-vocabulary-source-codes.test.js.map +2 -2
  67. package/dist/sortFields.js +16 -1
  68. package/dist/sortFields.js.map +2 -2
  69. package/dist/sortFields.test.js +1 -7
  70. package/dist/sortFields.test.js.map +2 -2
  71. package/dist/sortRelatorTerms.test.js +1 -7
  72. package/dist/sortRelatorTerms.test.js.map +2 -2
  73. package/dist/sortSubfields.js +3 -1
  74. package/dist/sortSubfields.js.map +2 -2
  75. package/dist/sortSubfields.test.js +1 -7
  76. package/dist/sortSubfields.test.js.map +2 -2
  77. package/dist/stripPunctuation.js +7 -4
  78. package/dist/stripPunctuation.js.map +2 -2
  79. package/dist/stripPunctuation.test.js +1 -7
  80. package/dist/stripPunctuation.test.js.map +2 -2
  81. package/dist/subfield6Utils.js +1 -13
  82. package/dist/subfield6Utils.js.map +2 -2
  83. package/dist/subfieldValueNormalizations.test.js +1 -7
  84. package/dist/subfieldValueNormalizations.test.js.map +2 -2
  85. package/dist/sync-007-and-300.test.js +1 -7
  86. package/dist/sync-007-and-300.test.js.map +2 -2
  87. package/dist/sync-language.js +103 -0
  88. package/dist/sync-language.js.map +7 -0
  89. package/dist/sync-language.test.js +40 -0
  90. package/dist/sync-language.test.js.map +7 -0
  91. package/dist/translate-terms.js +121 -85
  92. package/dist/translate-terms.js.map +3 -3
  93. package/dist/translate-terms.test.js +5 -8
  94. package/dist/translate-terms.test.js.map +2 -2
  95. package/dist/update-field-540.test.js +1 -7
  96. package/dist/update-field-540.test.js.map +2 -2
  97. package/dist/urn.test.js +1 -7
  98. package/dist/urn.test.js.map +2 -2
  99. package/package.json +8 -6
  100. package/src/disambiguateSeriesStatements.test.js +3 -8
  101. package/src/drop-terms.js +162 -0
  102. package/src/drop-terms.test.js +81 -0
  103. package/src/field-008-18-34-character-groups.test.js +3 -8
  104. package/src/field-505-separators.test.js +3 -8
  105. package/src/field-521-fix.test.js +3 -8
  106. package/src/fix-33X.test.js +1 -1
  107. package/src/fix-country-codes.test.js +3 -8
  108. package/src/fix-sami-041.js +113 -0
  109. package/src/fix-sami-041.test.js +52 -0
  110. package/src/fixRelatorTerms.test.js +3 -8
  111. package/src/index.js +8 -1
  112. package/src/indicator-fixes.js +12 -0
  113. package/src/indicator-fixes.test.js +3 -8
  114. package/src/merge-fields.test.js +3 -8
  115. package/src/mergeField500Lisapainokset.test.js +3 -8
  116. package/src/mergeRelatorTermFields.test.js +3 -8
  117. package/src/multiple-subfield-0.test.js +3 -8
  118. package/src/normalize-dashes.test.js +3 -8
  119. package/src/normalize-identifiers.test.js +3 -8
  120. package/src/normalize-qualifying-information.test.js +3 -8
  121. package/src/normalize-utf8-diacritics.test.js +3 -8
  122. package/src/punctuation.test.js +3 -8
  123. package/src/punctuation2.test.js +3 -8
  124. package/src/reindexSubfield6OccurenceNumbers.test.js +3 -8
  125. package/src/remove-041-zxx.js +85 -0
  126. package/src/remove-041-zxx.test.js +52 -0
  127. package/src/removeDuplicateDataFields.test.js +3 -8
  128. package/src/removeInferiorDataFields.js +7 -7
  129. package/src/removeInferiorDataFields.test.js +3 -8
  130. package/src/resolveOrphanedSubfield6s.js +3 -3
  131. package/src/resolveOrphanedSubfield6s.test.js +3 -8
  132. package/src/sanitize-vocabulary-source-codes.test.js +3 -8
  133. package/src/sortFields.js +20 -1
  134. package/src/sortFields.test.js +3 -8
  135. package/src/sortRelatorTerms.test.js +3 -8
  136. package/src/sortSubfields.js +3 -1
  137. package/src/sortSubfields.test.js +3 -8
  138. package/src/stripPunctuation.js +9 -6
  139. package/src/stripPunctuation.test.js +3 -8
  140. package/src/subfield6Utils.js +13 -13
  141. package/src/subfieldValueNormalizations.test.js +3 -8
  142. package/src/sync-007-and-300.test.js +3 -8
  143. package/src/sync-language.js +148 -0
  144. package/src/sync-language.test.js +52 -0
  145. package/src/translate-terms.js +158 -103
  146. package/src/translate-terms.test.js +12 -16
  147. package/src/update-field-540.test.js +3 -8
  148. package/src/urn.test.js +3 -8
  149. package/test-fixtures/drop-terms/01/expectedResult.json +31 -0
  150. package/test-fixtures/drop-terms/01/metadata.json +6 -0
  151. package/test-fixtures/drop-terms/01/record.json +35 -0
  152. package/test-fixtures/drop-terms/02/expectedResult.json +7 -0
  153. package/test-fixtures/drop-terms/02/metadata.json +6 -0
  154. package/test-fixtures/drop-terms/02/record.json +40 -0
  155. package/test-fixtures/drop-terms/03/expectedResult.json +6 -0
  156. package/test-fixtures/drop-terms/03/metadata.json +18 -0
  157. package/test-fixtures/drop-terms/03/record.json +39 -0
  158. package/test-fixtures/drop-terms/04/expectedResult.json +6 -0
  159. package/test-fixtures/drop-terms/04/metadata.json +19 -0
  160. package/test-fixtures/drop-terms/04/record.json +24 -0
  161. package/test-fixtures/fix-language-codes/02/metadata.json +1 -1
  162. package/test-fixtures/fix-sami-041/01/expectedResult.json +6 -0
  163. package/test-fixtures/fix-sami-041/01/metadata.json +4 -0
  164. package/test-fixtures/fix-sami-041/01/record.json +13 -0
  165. package/test-fixtures/fix-sami-041/02/expectedResult.json +10 -0
  166. package/test-fixtures/fix-sami-041/02/metadata.json +4 -0
  167. package/test-fixtures/fix-sami-041/02/record.json +8 -0
  168. package/test-fixtures/fix-sami-041/03/expectedResult.json +5 -0
  169. package/test-fixtures/fix-sami-041/03/metadata.json +5 -0
  170. package/test-fixtures/fix-sami-041/03/record.json +8 -0
  171. package/test-fixtures/fix-sami-041/04/expectedResult.json +7 -0
  172. package/test-fixtures/fix-sami-041/04/metadata.json +4 -0
  173. package/test-fixtures/fix-sami-041/04/record.json +10 -0
  174. package/test-fixtures/fix-sami-041/05/expectedResult.json +10 -0
  175. package/test-fixtures/fix-sami-041/05/metadata.json +6 -0
  176. package/test-fixtures/fix-sami-041/05/record.json +8 -0
  177. package/test-fixtures/indicator-fixes/11/expectedResult.json +10 -0
  178. package/test-fixtures/indicator-fixes/11/metadata.json +4 -0
  179. package/test-fixtures/indicator-fixes/11/record.json +10 -0
  180. package/test-fixtures/merge-fields/f05/metadata.json +1 -1
  181. package/test-fixtures/remove-041-zxx/01/expectedResult.json +5 -0
  182. package/test-fixtures/remove-041-zxx/01/metadata.json +5 -0
  183. package/test-fixtures/remove-041-zxx/01/record.json +10 -0
  184. package/test-fixtures/remove-041-zxx/02/expectedResult.json +7 -0
  185. package/test-fixtures/remove-041-zxx/02/metadata.json +5 -0
  186. package/test-fixtures/remove-041-zxx/02/record.json +9 -0
  187. package/test-fixtures/remove-041-zxx/11/expectedResult.json +10 -0
  188. package/test-fixtures/remove-041-zxx/11/metadata.json +5 -0
  189. package/test-fixtures/remove-041-zxx/11/record.json +9 -0
  190. package/test-fixtures/remove-041-zxx/12/expectedResult.json +10 -0
  191. package/test-fixtures/remove-041-zxx/12/metadata.json +5 -0
  192. package/test-fixtures/remove-041-zxx/12/record.json +9 -0
  193. package/test-fixtures/sort-fields/15/input.json +9 -0
  194. package/test-fixtures/sort-fields/15/metadata.json +5 -0
  195. package/test-fixtures/sort-fields/15/result.json +10 -0
  196. package/test-fixtures/sync-language/01/expectedResult.json +5 -0
  197. package/test-fixtures/sync-language/01/metadata.json +5 -0
  198. package/test-fixtures/sync-language/01/record.json +7 -0
  199. package/test-fixtures/sync-language/02/expectedResult.json +6 -0
  200. package/test-fixtures/sync-language/02/metadata.json +5 -0
  201. package/test-fixtures/sync-language/02/record.json +10 -0
  202. package/test-fixtures/sync-language/03/expectedResult.json +6 -0
  203. package/test-fixtures/sync-language/03/metadata.json +5 -0
  204. package/test-fixtures/sync-language/03/record.json +6 -0
  205. package/test-fixtures/sync-language/10/expectedResult.json +10 -0
  206. package/test-fixtures/sync-language/10/metadata.json +5 -0
  207. package/test-fixtures/sync-language/10/record.json +8 -0
  208. package/test-fixtures/sync-language/11/expectedResult.json +10 -0
  209. package/test-fixtures/sync-language/11/metadata.json +5 -0
  210. package/test-fixtures/sync-language/11/record.json +7 -0
  211. package/test-fixtures/sync-language/12/expectedResult.json +9 -0
  212. package/test-fixtures/sync-language/12/metadata.json +6 -0
  213. package/test-fixtures/sync-language/12/record.json +6 -0
  214. package/test-fixtures/sync-language/13/expectedResult.json +10 -0
  215. package/test-fixtures/sync-language/13/metadata.json +5 -0
  216. package/test-fixtures/sync-language/13/record.json +8 -0
  217. package/test-fixtures/sync-language/14/expectedResult.json +9 -0
  218. package/test-fixtures/sync-language/14/metadata.json +5 -0
  219. package/test-fixtures/sync-language/14/record.json +7 -0
  220. package/test-fixtures/sync-language/15/expectedResult.json +9 -0
  221. package/test-fixtures/sync-language/15/metadata.json +5 -0
  222. package/test-fixtures/sync-language/15/record.json +7 -0
  223. package/test-fixtures/translate-terms/05/expectedResult.json +12 -0
  224. package/test-fixtures/translate-terms/05/metadata.json +7 -0
  225. package/test-fixtures/translate-terms/05/record.json +11 -0
  226. package/test-fixtures/translate-terms/06/expectedResult.json +12 -0
  227. package/test-fixtures/translate-terms/06/metadata.json +7 -0
  228. package/test-fixtures/translate-terms/06/record.json +11 -0
  229. package/test-fixtures/translate-terms-data.js +23 -0
@@ -0,0 +1,148 @@
1
+ // Author(s): Nicholas Volk
2
+
3
+ //import createDebugLogger from 'debug';
4
+ import clone from 'clone';
5
+
6
+ import {fieldToString} from './utils.js';
7
+
8
+ // const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda:sync-language');
9
+
10
+ export default function () {
11
+
12
+ return {
13
+ description: 'Sync 008/35-37 and 041$a/$d',
14
+ validate, fix
15
+ };
16
+
17
+ function getSubfieldCodeFor041(record) {
18
+ const c = record?.leader[6];
19
+ if (c === 'i' || c === 'j') {
20
+ return 'd';
21
+ }
22
+ return 'a';
23
+ }
24
+
25
+ function transferableValue(val) {
26
+ if (val.match(/^[a-z]{3}$/u)) {
27
+ // 'zxx' is an exception to the otherwise relatively transitive 008/35-37 <=> 041$a relationship...
28
+ // https://wiki.helsinki.fi/xwiki/bin/view/rdasovellusohje/RDA-kuvailu%20MARC%2021%20-formaatilla/RDA-sovellusohje/008%20Kontrollikentt%C3%A4/#Hmp.35-37Kieli
29
+ // "008-kielikoodi on sama kuin ensimmäinen 041-kentän a- (tai d-) osakenttä. Poikkeuksen muodostaa koodi zxx:
30
+ // jos tietueen 008ssa on kielikoodi zxx (ei kielellistä sisältöä, esim. soitinmusiikki), tietueessa ei voi olla 041 a- eikä d-osakenttää."
31
+ // So 'zxx' ain't transferable (done here), and it should be removed from 041 ($a and $d at least, not done here nor elsewhere at the moment...)
32
+ // Note that 'mul' and 'und' are treated (in this validator) the same as normal values.
33
+ if (val === 'zxx') {
34
+ return false;
35
+ }
36
+ return true;
37
+ }
38
+ return false;
39
+ }
40
+
41
+
42
+ function zxxRemoval041(record, field, validateMode) {
43
+ const fields041 = record.fields.filter(f => f.tag === '041');
44
+
45
+ return handleZxx(fields041);
46
+
47
+ function handleZxx(fields, result = []) {
48
+ const [currField, ...remainingFields] = fields;
49
+ if ( !currField) {
50
+ return result;
51
+ }
52
+ // Theoretically 'zxx' might mean something in ISO-639-3 or some other language code list:
53
+ if (!field.subfields || field.subfields.some(sf => sf.code === '2')) {
54
+ return handleZxx(remainingFields, result);
55
+ }
56
+ // Presumable 'zxx' is bad in any data subfield, not just 'a' and 'd':
57
+ const otherSubfields = field.subfields.some(sf => !sf.code.match(/^[a-z]$/u || sf.value !== 'zxx'));
58
+ if (otherSubfields.length === field.subfields.length) {
59
+ return handleZxx(remainingFields, result);
60
+ }
61
+ const originalString = fieldToString(field);
62
+ if (otherSubfields.length === 0) {
63
+ const message = `Remove '${originalString}'`;
64
+ if (!validateMode) {
65
+ record.removeField(field);
66
+ }
67
+ return handleZxx(remainingFields, [...result, message]);
68
+ }
69
+ // Some subfields are removed:
70
+ if (validateMode) {
71
+ // NB! In validation field is not really deleted, and the non-deleted field might trigger other fixes (than won't be done to a deleted field) later on
72
+ const clonedField = clone(field);
73
+ clonedField.subfields = otherSubfields;
74
+ const modifiedString = fieldToString(clonedField);
75
+ const message = `Modify '${originalString}' => '${modifiedString}`;
76
+ return handleZxx(remainingFields, [...result, message]);
77
+ }
78
+ field.subfields = otherSubfields;
79
+ const modifiedString = fieldToString(field);
80
+ const message = `Modify '${originalString}' => '${modifiedString}`;
81
+ return handleZxx(remainingFields, [...result, message]);
82
+ }
83
+
84
+
85
+ }
86
+
87
+ function sync008And041(record, validateMode) {
88
+ const f008 = record.fields.find(f => f.tag === '008');
89
+ const f041 = record.fields.find(f => f.tag === '041');
90
+
91
+ if (!f008 || f008.value.length !== 40) { // Some sanity checks
92
+ return [];
93
+ }
94
+
95
+ const subfieldCode = getSubfieldCodeFor041(record);
96
+ const lang008 = f008.value.substring(35, 38);
97
+
98
+ if (!f041) {
99
+ // Insert missing 041
100
+ if (transferableValue(lang008)) {
101
+ const newField = {'tag': '041', 'ind1': ' ', 'ind2': ' ', 'subfields': [ {'code': subfieldCode, 'value': lang008}]};
102
+ if (!validateMode) {
103
+ record.insertField(newField);
104
+ }
105
+ return [ `Add '${fieldToString(newField)}'` ];
106
+ }
107
+ // Can't do anything, and we only report this we can fix...
108
+ return [];
109
+ }
110
+
111
+ const firstRelevantSubfield = f041.subfields.find(sf => sf.code === subfieldCode);
112
+
113
+ // NB! If $2 is set, *never* copy it's value to 008/35-37... Otherwise we might a loop in Aleph.
114
+ // Note that if $2 is used, 008/35-37 should actually be '|||'. Now we just aggressively leave it alone.
115
+ if (!firstRelevantSubfield || !transferableValue(firstRelevantSubfield.value) || f041.subfields.some(sf => sf.code === '2') || lang008 === firstRelevantSubfield.value) {
116
+ return []
117
+ }
118
+
119
+ // Update 008/35-37:
120
+ const cloned008 = clone(f008);
121
+ cloned008.value = `${f008.value.substring(0, 35)}${firstRelevantSubfield.value}${f008.value.substring(38)}`;
122
+
123
+
124
+ if (!validateMode) {
125
+ f008.value = cloned008.value;
126
+ }
127
+ return [ `Modify '${f008.value}' to '${cloned008.value}'` ];
128
+ }
129
+
130
+ function fix(record, validateMode = false) {
131
+ const zxxMessages = zxxRemoval041(record, validateMode);
132
+
133
+ const syncMessages = sync008And041(record, validateMode);
134
+
135
+ const messages = [...zxxMessages, ...syncMessages];
136
+
137
+ if (validateMode) {
138
+ return {message: messages, valid: messages.length === 0};
139
+ }
140
+ return {message: [], fix: messages, valid: true};
141
+ }
142
+
143
+
144
+ function validate(record) {
145
+ return fix(record, true);
146
+ }
147
+ }
148
+
@@ -0,0 +1,52 @@
1
+ import assert from 'node:assert/strict';
2
+ //import createDebugLogger from 'debug';
3
+
4
+ import validatorFactory from './sync-language.js';
5
+
6
+ import {MarcRecord} from '@natlibfi/marc-record';
7
+ import {READERS} from '@natlibfi/fixura';
8
+ import generateTests from '@natlibfi/fixugen';
9
+
10
+ generateTests({
11
+ callback,
12
+ path: [import.meta.dirname, '..', 'test-fixtures', 'sync-language'],
13
+ useMetadataFile: true,
14
+ recurse: false,
15
+ fixura: {
16
+ reader: READERS.JSON
17
+ },
18
+ hooks: {
19
+ before: async () => {
20
+ testValidatorFactory();
21
+ }
22
+ }
23
+ });
24
+
25
+ //const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/drop-terms:test');
26
+
27
+ async function testValidatorFactory() {
28
+ const validator = await validatorFactory();
29
+
30
+ assert.equal(typeof validator, 'object');
31
+ assert.equal(typeof validator.description, 'string');
32
+ assert.equal(typeof validator.validate, 'function');
33
+ }
34
+
35
+ async function callback({getFixture, fix = false}) {
36
+ const validator = await validatorFactory();
37
+ const record = new MarcRecord(getFixture('record.json'));
38
+ const expectedResult = getFixture('expectedResult.json');
39
+ //const expectedResult = new MarcRecord(getFixture('expectedResult.json'));
40
+ // console.log(expectedResult); // eslint-disable-line
41
+
42
+ if (!fix) {
43
+ const result = await validator.validate(record);
44
+ assert.deepEqual(result, expectedResult);
45
+ const originalRecord = new MarcRecord(getFixture('record.json'));
46
+ assert.deepEqual(record, originalRecord); // The record should now change in validation-only
47
+ return;
48
+ }
49
+
50
+ await validator.fix(record);
51
+ assert.deepEqual(record, new MarcRecord(expectedResult));
52
+ }
@@ -60,8 +60,8 @@ export default function () {
60
60
  return [];
61
61
  }
62
62
  const fields = record.get(tag);
63
- const finnishFields = fields.filter(f => isRelevantField(f, 'fin'));
64
- const swedishFields = fields.filter(f => isRelevantField(f, 'swe'));
63
+ const finnishFields = fields.filter(f => isTranslatable(f, 'fin'));
64
+ const swedishFields = fields.filter(f => isTranslatable(f, 'swe'));
65
65
  const finnishOnly = getMisses(finnishFields, swedishFields);
66
66
  const swedishOnly = getMisses(swedishFields, finnishFields);
67
67
 
@@ -79,6 +79,7 @@ export default function () {
79
79
  const pairlessFields = getPairlessFinnishAndSwedishFields(record, tag);
80
80
 
81
81
  // Dunno how to handle loop+promise combo in our normal coding style. Spent half a day trying... (I reckon it takes like 2 minuts to do this properly...)
82
+ // Did a proper implementation in drop-terms.js...
82
83
  let prefLabels = [];
83
84
  for (let i=0; i < pairlessFields.length; i += 1) {
84
85
  prefLabels[i] = await getPrefLabel(pairlessFields[i]);
@@ -105,119 +106,48 @@ export default function () {
105
106
  return newField;
106
107
  }
107
108
 
108
- function getLexiconAndLanguage(field) {
109
- const subfield2 = field.subfields.find(sf => sf.code === '2');
110
- if (subfield2.value === 'slm/fin') {
111
- return {'lex': 'slm', 'lang': 'fin'};
112
- }
113
- if (subfield2.value === 'slm/swe') {
114
- return {'lex': 'slm', 'lang': 'swe'};
115
- }
116
- if (subfield2.value === 'yso/fin') {
117
- return {'lex': 'yso', 'lang': 'fin'};
118
- }
119
- if (subfield2.value === 'yso/swe') {
120
- return {'lex': 'yso', 'lang': 'swe'};
121
- }
122
- return {};
123
- }
109
+
124
110
 
125
111
  async function getPrefLabel(field) {
126
- // Tag vs $2 correlation has already been checked!
112
+ // Pre-requisite: tag vs $2 correlation has already been checked!
127
113
  const uri = fieldToUri(field);
128
114
  if (!uri) { // $0 is invalid or sumthing
129
115
  return undefined;
130
116
  }
131
- const prefLabels = await getTermData(uri);
132
- if (!prefLabels) { // Sanity check. Miss caused by illegal id etc.
117
+
118
+ const data = await getTermData(uri);
119
+
120
+ if (!data) { // Sanity check. Miss caused by illegal id etc.
133
121
  nvdebug(`No labels found for ${uri}`, debug);
134
122
  return undefined;
135
123
  }
136
- const lexData = getLexiconAndLanguage(field); // $2 data
137
- const lang = changeAbbr(lexData.lang);
138
124
 
125
+ const prefLabels = data.prefLabel;
139
126
 
127
+ const lexData = getLexiconAndLanguage(field); // $2 data
128
+ const lang = changeAbbr(lexData.lang); // fi <=> fin (finto use 2 chars, we use 3-letters)
140
129
  const subfieldA = field.subfields.find(sf => sf.code === 'a');
141
130
 
142
- const prefLabel = prefLabels.find(pl => pl.lang === lang);
143
- //console.info(`Compare prefLabel '${prefLabel.value}' AND $a '${subfieldA.value}'`); // eslint-disable-line no-console
144
- if (prefLabel.value === subfieldA.value) {
145
- nvdebug(`'${fieldToString(field)}' requires translating`, debug);
131
+ if (isLabel(prefLabels, subfieldA.value, lang)) {
146
132
  return prefLabels;
147
133
  }
148
134
  return undefined;
149
135
  }
150
136
 
151
- function swapLanguageCodeBetweenLanguages(code) {
152
- if (swapLanguageCode[code]) {
153
- return swapLanguageCode[code];
154
- }
155
- return code;
156
- }
157
-
158
- function changeAbbr(abbr) {
159
- if (changeAbbrHash[abbr]) {
160
- return changeAbbrHash[abbr];
161
- }
162
- return abbr;
163
- }
164
-
165
- function swaggerQuery(uri) {
166
- // This would work for only yso, not yso-paikat etc `https://api.finto.fi/rest/v1/yso/data?format=application%2Fjson&uri=${uri}`;
167
- return `https://api.finto.fi/rest/v1/data?uri=${uri}&format=application%2Fjson`; // This is simpler, but contains more irrelevant data
168
- }
169
-
170
- async function getTermData(uri) {
171
- //console.log(`getTermData(${uri})`); // eslint-disable-line no-console
172
- if (termCache[uri]) { // Don't think current implementation uses the cache any more.
173
- //console.log(`CACHED ${uri}`); // eslint-disable-line no-console
174
- return termCache[uri];
175
- }
176
- const tmp = await getTermDataFromFinto(uri);
177
- termCache[uri] = tmp;
178
- return tmp;
179
- }
180
-
181
- async function getTermDataFromFinto(uri) {
182
- const headers = {'Accept': 'application/json'};
183
- const uri2 = swaggerQuery(uri);
184
-
185
- const response = await fetch(uri2, {method: 'GET', headers});
186
- if (!response.ok) {
187
- return undefined;
188
- }
189
- const json = await response.json();
190
-
191
- if (!json.graph) {
192
- return undefined;
193
- }
194
- const arr = json.graph;
195
- const [hit] = arr.filter(row => row.uri === uri);
196
- //console.log(`NEW JSON: ${JSON.stringify(hit.prefLabel)}`); // eslint-disable-line no-console
197
- return hit.prefLabel;
198
- }
199
-
200
137
 
201
138
  function fieldToUri(field) {
202
139
  const lex = mapTagToLex(field.tag);
203
140
 
204
141
  const subfield0 = field.subfields.find(sf => sf.code === '0');
205
142
  const id = subfield0.value.replace(/^[^0-9]+/u, '');
206
- if (lex === 'yso') {
207
- //return `http%3A%2F%2Fwww.yso.fi%2Fonto%2Fyso%2Fp${id}`;
208
- return `http://www.yso.fi/onto/yso/p${id}`;
209
- }
210
- if (lex === 'slm') {
211
- return `http://urn.fi/URN:NBN:fi:au:slm:s${id}`;
212
- }
213
- return undefined;
143
+ return buildUri(lex, id);
214
144
  }
215
145
 
216
- function isRelevantField(field, lang) {
146
+ function isTranslatable(field, lang) {
217
147
  const fieldAsString = fieldToString(field);
218
148
 
219
149
  // We should probably allow an optional $8 as the first subfield.
220
- if (!fieldAsString.match(/^... #7 ‡a [^‡]+ ‡2 [^‡]+ ‡0 [^‡]+(?: ‡9 [A-Z]+<(?:KEEP|DROP)>)*$/u)) {
150
+ if (!fieldAsString.match(/^... #7 (?: 8 [^‡]+ )*‡a [^‡]+ ‡2 [^‡]+ ‡0 [^‡]+(?: ‡9 [A-Z]+<(?:KEEP|DROP)>)*$/u)) {
221
151
  return false;
222
152
  }
223
153
  const lex = mapTagToLex(field.tag);
@@ -228,17 +158,6 @@ export default function () {
228
158
  return fieldHasValidSubfield0(field);
229
159
  }
230
160
 
231
- function fieldHasValidSubfield0(field) {
232
- const lex = mapTagToLex(field.tag);
233
- const subfield0 = field.subfields.find(sf => sf.code === '0');
234
- if (lex === 'yso' && subfield0.value.match(/^http:\/\/www\.yso\.fi\/onto\/yso\/p[0-9]+$/u)) {
235
- return true;
236
- }
237
- if (lex === 'slm' && subfield0.value.match(/^http:\/\/urn\.fi\/URN:NBN:fi:au:slm:s[0-9]+$/u)) {
238
- return true;
239
- }
240
- return false;
241
- }
242
161
 
243
162
  function getMisses(fieldList1, fieldList2) {
244
163
  return fieldList1.filter(f => !hasSubfield0Match(f, fieldList2));
@@ -250,12 +169,7 @@ export default function () {
250
169
  }
251
170
 
252
171
 
253
- function mapTagToLex(tag) {
254
- if (tag === '655') {
255
- return 'slm';
256
- }
257
- return 'yso';
258
- }
172
+
259
173
 
260
174
 
261
175
  /*
@@ -269,3 +183,144 @@ export default function () {
269
183
 
270
184
  }
271
185
 
186
+ export function fieldHasValidSubfield0(field, defaultLex = undefined) {
187
+ const lex = defaultLex || mapTagToLex(field.tag);
188
+ return field.subfields.some(sf => isValidSubfield0(sf, lex));
189
+ }
190
+
191
+ export function isValidSubfield0(subfield, lex = '???') {
192
+ if (subfield.code !== '0') {
193
+ return false;
194
+ }
195
+ // 2025-12-10: supports both http and https as well. Note that translation will copy the original $0 idenfifier even though it might be non-standard.
196
+ // Note that currently 'http' is teh standard!!!
197
+ if (['yso', 'yso/fin', 'yso/swe'].includes(lex) && subfield.value.match(/^https?:\/\/www\.yso\.fi\/onto\/yso\/p[0-9]+$/u)) {
198
+ return true;
199
+ }
200
+ if (['slm', 'slm/fin', 'slm/swe'].includes(lex) && subfield.value.match(/^https?:\/\/urn\.fi\/URN:NBN:fi:au:slm:s[0-9]+$/u)) {
201
+ return true;
202
+ }
203
+ return false;
204
+ }
205
+
206
+
207
+ export function buildUri(lex, id) {
208
+ if (['yso', 'yso/fin', 'yso/swe'].includes(lex)) {
209
+ //return `http%3A%2F%2Fwww.yso.fi%2Fonto%2Fyso%2Fp${id}`;
210
+ return `http://www.yso.fi/onto/yso/p${id}`;
211
+ }
212
+ if (['slm', 'slm/fin', 'slm/swe'].includes(lex)) {
213
+ return `http://urn.fi/URN:NBN:fi:au:slm:s${id}`;
214
+ }
215
+ return undefined;
216
+ }
217
+
218
+ function mapTagToLex(tag) {
219
+ if (tag === '655') {
220
+ return 'slm';
221
+ }
222
+ return 'yso';
223
+ }
224
+
225
+ export async function getTermData(uri) {
226
+ nvdebug(`getTermData(${uri})`);
227
+ if (termCache[uri]) { // Don't think current implementation uses the cache any more.
228
+ //console.log(`CACHED ${uri}`); // eslint-disable-line no-console
229
+ return termCache[uri];
230
+ }
231
+ const tmp = await getTermDataFromFinto(uri);
232
+ termCache[uri] = tmp;
233
+ return tmp;
234
+ }
235
+
236
+ async function getTermDataFromFinto(uri) {
237
+ const headers = {'Accept': 'application/json'};
238
+ const uri2 = swaggerQuery(uri);
239
+
240
+ const response = await fetch(uri2, {method: 'GET', headers});
241
+ if (!response.ok) {
242
+ return undefined;
243
+ }
244
+ const json = await response.json();
245
+
246
+ if (!json.graph) {
247
+ return undefined;
248
+ }
249
+ const arr = json.graph;
250
+ const [hit] = arr.filter(row => row.uri === uri);
251
+ const subset = {
252
+ prefLabel: processLabel(hit?.prefLabel || undefined),
253
+ altLabel: processLabel(hit?.altLabel || undefined)
254
+ };
255
+ //console.log(`NEW JSON: ${JSON.stringify(hit)}`); // eslint-disable-line no-console
256
+
257
+ return subset;
258
+
259
+ function swaggerQuery(uri) {
260
+ // This would work for only yso, not yso-paikat etc `https://api.finto.fi/rest/v1/yso/data?format=application%2Fjson&uri=${uri}`;
261
+ return `https://api.finto.fi/rest/v1/data?uri=${uri}&format=application%2Fjson`; // This is simpler, but contains more irrelevant data
262
+ }
263
+
264
+ function processLabel(label) {
265
+ if (typeof label === 'object') {
266
+ if (Array.isArray(label)) {
267
+ return label;
268
+ }
269
+ return [label];
270
+ }
271
+ return [];
272
+ }
273
+ }
274
+
275
+ export function getLexiconAndLanguage(field) {
276
+ // Assumes that field has exactly one $2
277
+ const subfield2 = field.subfields.find(sf => sf.code === '2');
278
+ if (subfield2) {
279
+ if (subfield2.value === 'slm/fin') {
280
+ return {'lex': 'slm', 'lang': 'fin'};
281
+ }
282
+ if (subfield2.value === 'slm/swe') {
283
+ return {'lex': 'slm', 'lang': 'swe'};
284
+ }
285
+ if (subfield2.value === 'yso/fin') {
286
+ return {'lex': 'yso', 'lang': 'fin'};
287
+ }
288
+ if (subfield2.value === 'yso/swe') {
289
+ return {'lex': 'yso', 'lang': 'swe'};
290
+ }
291
+ }
292
+ return {};
293
+ }
294
+
295
+
296
+
297
+ export function isLabel(labels, term, lang = undefined) {
298
+ const twoLetterLanguageCode = lang && lang.length === 3 ? changeAbbr(lang) : lang;
299
+ return labels.some(l => isMatch(l));
300
+
301
+ function isMatch(label) {
302
+ if (label.value !== term) {
303
+ return false;
304
+ }
305
+ if (!twoLetterLanguageCode) { // If language code is not defined, any hit will do
306
+ return true;
307
+ }
308
+ return label.lang === twoLetterLanguageCode;
309
+ }
310
+ }
311
+
312
+
313
+ function changeAbbr(abbr) {
314
+ if (changeAbbrHash[abbr]) {
315
+ return changeAbbrHash[abbr];
316
+ }
317
+ return abbr;
318
+ }
319
+
320
+
321
+ function swapLanguageCodeBetweenLanguages(code) {
322
+ if (swapLanguageCode[code]) {
323
+ return swapLanguageCode[code];
324
+ }
325
+ return code;
326
+ }
@@ -1,5 +1,5 @@
1
1
  import assert from 'node:assert';
2
- import createDebugLogger from 'debug';
2
+ //import createDebugLogger from 'debug';
3
3
  import fetchMock from 'fetch-mock';
4
4
 
5
5
  import validatorFactory from './translate-terms.js';
@@ -17,7 +17,7 @@ const uris = [
17
17
  'http://urn.fi/URN:NBN:fi:au:slm:s161'
18
18
  ];
19
19
 
20
-
20
+ const useMock = false;
21
21
 
22
22
  generateTests({
23
23
  callback,
@@ -30,20 +30,21 @@ generateTests({
30
30
  hooks: {
31
31
  before: async () => {
32
32
 
33
- fetchMock.mockGlobal()
34
- .get(`https://api.finto.fi/rest/v1/data?uri=${uris[0]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData})
35
- .get(`https://api.finto.fi/rest/v1/data?uri=${uris[1]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData})
36
- .get(`https://api.finto.fi/rest/v1/data?uri=${uris[2]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData})
37
- .get(`https://api.finto.fi/rest/v1/data?uri=${uris[3]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData})
38
- .get(`https://api.finto.fi/rest/v1/data?uri=${uris[4]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData});
39
-
33
+ if (useMock){
34
+ fetchMock.mockGlobal()
35
+ .get(`https://api.finto.fi/rest/v1/data?uri=${uris[0]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData})
36
+ .get(`https://api.finto.fi/rest/v1/data?uri=${uris[1]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData})
37
+ .get(`https://api.finto.fi/rest/v1/data?uri=${uris[2]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData})
38
+ .get(`https://api.finto.fi/rest/v1/data?uri=${uris[3]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData})
39
+ .get(`https://api.finto.fi/rest/v1/data?uri=${uris[4]}&format=application%2Fjson`, {status: 200, headers: {}, body: fakeData});
40
+ }
40
41
 
41
42
  testValidatorFactory();
42
43
  }
43
44
  }
44
45
  });
45
46
 
46
- const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/translate-terms:test');
47
+ //const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/translate-terms:test');
47
48
 
48
49
  async function testValidatorFactory() {
49
50
  const validator = await validatorFactory();
@@ -53,12 +54,7 @@ async function testValidatorFactory() {
53
54
  assert.equal(typeof validator.validate, 'function');
54
55
  }
55
56
 
56
- async function callback({getFixture, enabled = true, fix = false}) {
57
- if (enabled === false) {
58
- debug('TEST SKIPPED!');
59
- return;
60
- }
61
-
57
+ async function callback({getFixture, fix = false}) {
62
58
  const validator = await validatorFactory();
63
59
  const record = new MarcRecord(getFixture('record.json'));
64
60
  const expectedResult = getFixture('expectedResult.json');
@@ -3,7 +3,7 @@ import {MarcRecord} from '@natlibfi/marc-record';
3
3
  import validatorFactory from './update-field-540.js';
4
4
  import {READERS} from '@natlibfi/fixura';
5
5
  import generateTests from '@natlibfi/fixugen';
6
- import createDebugLogger from 'debug';
6
+ //import createDebugLogger from 'debug';
7
7
 
8
8
  generateTests({
9
9
  callback,
@@ -20,7 +20,7 @@ generateTests({
20
20
  }
21
21
  });
22
22
 
23
- const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/update-field-540:test');
23
+ //const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/update-field-540:test');
24
24
 
25
25
  async function testValidatorFactory() {
26
26
  const validator = await validatorFactory();
@@ -33,12 +33,7 @@ async function testValidatorFactory() {
33
33
  assert(validator.validate).to.be.a('function');
34
34
  }
35
35
 
36
- async function callback({getFixture, enabled = true, fix = false}) {
37
- if (enabled === false) {
38
- debug('TEST SKIPPED!');
39
- return;
40
- }
41
-
36
+ async function callback({getFixture, fix = false}) {
42
37
  const validator = await validatorFactory();
43
38
  const record = new MarcRecord(getFixture('record.json'));
44
39
  const expectedResult = getFixture('expectedResult.json');
package/src/urn.test.js CHANGED
@@ -3,7 +3,7 @@ import {MarcRecord} from '@natlibfi/marc-record';
3
3
  import validatorFactory from './urn.js';
4
4
  import {READERS} from '@natlibfi/fixura';
5
5
  import generateTests from '@natlibfi/fixugen';
6
- import createDebugLogger from 'debug';
6
+ //import createDebugLogger from 'debug';
7
7
 
8
8
  generateTests({
9
9
  callback,
@@ -19,7 +19,7 @@ generateTests({
19
19
  }
20
20
  }
21
21
  });
22
- const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/urn:test');
22
+ //const debug = createDebugLogger('@natlibfi/marc-record-validators-melinda/urn:test');
23
23
 
24
24
  async function testValidatorFactory() {
25
25
  const validator = await validatorFactory();
@@ -29,12 +29,7 @@ async function testValidatorFactory() {
29
29
  assert.equal(typeof validator.validate, 'function');
30
30
  }
31
31
 
32
- async function callback({getFixture, enabled = true, fix = true, isLegalDeposit = false}) {
33
- if (enabled === false) {
34
- debug('TEST SKIPPED!');
35
- return;
36
- }
37
-
32
+ async function callback({getFixture, fix = true, isLegalDeposit = false}) {
38
33
  const validator = await validatorFactory(isLegalDeposit);
39
34
  const record = new MarcRecord(getFixture('input.json'));
40
35
  const expectedResult = getFixture('result.json');
@@ -0,0 +1,31 @@
1
+ {
2
+ "_validationOptions": {},
3
+ "fields": [
4
+ { "tag": "001", "value": "test1"},
5
+ { "tag": "650", "ind1": " ", "ind2": "7", "subfields": [
6
+ { "code": "a", "value": "laiturit" },
7
+ { "code": "2", "value": "yso/fin" },
8
+ { "code": "0", "value": "http://www.yso.fi/onto/yso/p13299" }
9
+ ]
10
+ },
11
+
12
+ { "tag": "650", "ind1": " ", "ind2": "7", "subfields": [
13
+ { "code": "a", "value": "marxismi" },
14
+ { "code": "2", "value": "yso/fin" },
15
+ { "code": "0", "value": "http://www.yso.fi/onto/yso/p444" }
16
+ ]},
17
+ { "tag": "650", "ind1": " ", "ind2": "7", "subfields": [
18
+ { "code": "a", "value": "marxism" },
19
+ { "code": "2", "value": "yso/swe" },
20
+ { "code": "0", "value": "http://www.yso.fi/onto/yso/p444" }
21
+ ]},
22
+ { "tag": "651", "ind1": " ", "ind2": "7", "subfields": [
23
+ { "code": "a", "value": "Ivalo (Enare)" },
24
+ { "code": "2", "value": "yso/swe" },
25
+ { "code": "0", "value": "http://www.yso.fi/onto/yso/p111739" }
26
+ ]}
27
+
28
+
29
+ ],
30
+ "leader": ""
31
+ }