@natlibfi/marc-record-merge 6.0.0-beta.2 → 6.0.0-beta.4

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 (183) hide show
  1. package/.github/CODEOWNERS +9 -0
  2. package/.github/dependabot.yml +41 -0
  3. package/.github/workflows/melinda-node-tests.yml +60 -0
  4. package/LICENSE +21 -0
  5. package/README.md +92 -2
  6. package/dist/index.js +2 -37
  7. package/dist/index.js.map +1 -1
  8. package/dist/reducers/copy.js +246 -111
  9. package/dist/reducers/copy.js.map +1 -1
  10. package/dist/reducers/copy.spec.js +53 -99
  11. package/dist/reducers/copy.spec.js.map +1 -1
  12. package/dist/reducers/index.js +0 -27
  13. package/dist/reducers/index.js.map +1 -1
  14. package/dist/reducers/select.js +2 -48
  15. package/dist/reducers/select.js.map +1 -1
  16. package/dist/reducers/select.spec.js +49 -83
  17. package/dist/reducers/select.spec.js.map +1 -1
  18. package/package.json +27 -22
  19. package/src/index.js +1 -33
  20. package/src/reducers/copy.js +253 -119
  21. package/src/reducers/copy.spec.js +41 -65
  22. package/src/reducers/index.js +0 -27
  23. package/src/reducers/select.js +1 -47
  24. package/src/reducers/select.spec.js +37 -58
  25. package/test-fixtures/reducers/copy/{01/base.json → basic copy/01/base.json } +1 -1
  26. package/test-fixtures/reducers/copy/{01 → basic copy/01}/merged.json +0 -0
  27. package/test-fixtures/reducers/copy/basic copy/01/metadata.json +5 -0
  28. package/test-fixtures/reducers/copy/{01 → basic copy/01}/source.json +0 -0
  29. package/test-fixtures/reducers/copy/{02/source.json → basic copy/02/base.json} +0 -0
  30. package/test-fixtures/reducers/copy/{02 → basic copy/02}/merged.json +0 -0
  31. package/test-fixtures/reducers/copy/basic copy/02/metadata.json +5 -0
  32. package/test-fixtures/reducers/copy/{02/base.json → basic copy/02/source.json } +1 -1
  33. package/test-fixtures/reducers/copy/{03/base.json → basic copy/03/base.json } +1 -1
  34. package/test-fixtures/reducers/copy/{03 → basic copy/03}/merged.json +0 -0
  35. package/test-fixtures/reducers/copy/basic copy/03/metadata.json +5 -0
  36. package/test-fixtures/reducers/copy/{03 → basic copy/03}/source.json +0 -0
  37. package/test-fixtures/reducers/copy/{05/base.json → basic copy/04/base.json } +1 -1
  38. package/test-fixtures/reducers/copy/{04/merged.json → basic copy/04/merged.json } +1 -1
  39. package/test-fixtures/reducers/copy/basic copy/04/metadata.json +5 -0
  40. package/test-fixtures/reducers/copy/{04/source.json → basic copy/04/source.json } +1 -1
  41. package/test-fixtures/reducers/copy/{04/base.json → basic copy/05/base.json } +1 -1
  42. package/test-fixtures/reducers/copy/{05 → basic copy/05}/merged.json +0 -0
  43. package/test-fixtures/reducers/copy/basic copy/05/metadata.json +5 -0
  44. package/test-fixtures/reducers/copy/{05 → basic copy/05}/source.json +0 -0
  45. package/test-fixtures/reducers/copy/basic copy/06/base.json +24 -0
  46. package/test-fixtures/reducers/copy/basic copy/06/merged.json +39 -0
  47. package/test-fixtures/reducers/copy/basic copy/06/metadata.json +5 -0
  48. package/test-fixtures/reducers/copy/basic copy/06/source.json +24 -0
  49. package/test-fixtures/reducers/copy/{06 → compareTagsOnly/01}/base.json +0 -0
  50. package/test-fixtures/reducers/copy/{06 → compareTagsOnly/01}/merged.json +0 -0
  51. package/test-fixtures/reducers/copy/compareTagsOnly/01/metadata.json +6 -0
  52. package/test-fixtures/reducers/copy/{06 → compareTagsOnly/01}/source.json +0 -0
  53. package/test-fixtures/reducers/copy/{09 → compareTagsOnly/02}/base.json +0 -0
  54. package/test-fixtures/reducers/copy/{09 → compareTagsOnly/02}/merged.json +0 -0
  55. package/test-fixtures/reducers/copy/compareTagsOnly/02/metadata.json +6 -0
  56. package/test-fixtures/reducers/copy/{09 → compareTagsOnly/02}/source.json +0 -0
  57. package/test-fixtures/reducers/copy/compareWithoutIndicators/01/base.json +24 -0
  58. package/test-fixtures/reducers/copy/compareWithoutIndicators/01/merged.json +24 -0
  59. package/test-fixtures/reducers/copy/compareWithoutIndicators/01/metadata.json +6 -0
  60. package/test-fixtures/reducers/copy/compareWithoutIndicators/01/source.json +24 -0
  61. package/test-fixtures/reducers/copy/compareWithoutIndicators/02/base.json +24 -0
  62. package/test-fixtures/reducers/copy/compareWithoutIndicators/02/merged.json +39 -0
  63. package/test-fixtures/reducers/copy/compareWithoutIndicators/02/metadata.json +6 -0
  64. package/test-fixtures/reducers/copy/compareWithoutIndicators/02/source.json +24 -0
  65. package/test-fixtures/reducers/copy/copyUnless/01/base.json +9 -0
  66. package/test-fixtures/reducers/copy/copyUnless/01/merged.json +24 -0
  67. package/test-fixtures/reducers/copy/copyUnless/01/metadata.json +6 -0
  68. package/test-fixtures/reducers/copy/copyUnless/01/source.json +47 -0
  69. package/test-fixtures/reducers/copy/{08 → dropSubfields/01}/base.json +0 -0
  70. package/test-fixtures/reducers/copy/{08 → dropSubfields/01}/merged.json +8 -0
  71. package/test-fixtures/reducers/copy/dropSubfields/01/metadata.json +6 -0
  72. package/test-fixtures/reducers/copy/{08 → dropSubfields/01}/source.json +0 -0
  73. package/test-fixtures/reducers/copy/dropSubfields/02/base.json +43 -0
  74. package/test-fixtures/reducers/copy/dropSubfields/02/merged.json +58 -0
  75. package/test-fixtures/reducers/copy/dropSubfields/02/metadata.json +6 -0
  76. package/test-fixtures/reducers/copy/dropSubfields/02/source.json +32 -0
  77. package/test-fixtures/reducers/copy/dropSubfields/03/base.json +43 -0
  78. package/test-fixtures/reducers/copy/dropSubfields/03/merged.json +58 -0
  79. package/test-fixtures/reducers/copy/dropSubfields/03/metadata.json +6 -0
  80. package/test-fixtures/reducers/copy/dropSubfields/03/source.json +32 -0
  81. package/test-fixtures/reducers/copy/dropSubfields/04/base.json +43 -0
  82. package/test-fixtures/reducers/copy/dropSubfields/04/merged.json +43 -0
  83. package/test-fixtures/reducers/copy/dropSubfields/04/metadata.json +6 -0
  84. package/test-fixtures/reducers/copy/dropSubfields/04/source.json +32 -0
  85. package/test-fixtures/reducers/copy/{07 → excludeSubfields/01}/base.json +0 -0
  86. package/test-fixtures/reducers/copy/{07 → excludeSubfields/01}/merged.json +0 -0
  87. package/test-fixtures/reducers/copy/excludeSubfields/01/metadata.json +9 -0
  88. package/test-fixtures/reducers/copy/{07 → excludeSubfields/01}/source.json +0 -0
  89. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/01/base.json +24 -0
  90. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/01/merged.json +39 -0
  91. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/01/metadata.json +6 -0
  92. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/01/source.json +24 -0
  93. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/02/base.json +24 -0
  94. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/02/merged.json +24 -0
  95. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/02/metadata.json +6 -0
  96. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/02/source.json +24 -0
  97. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/03/base.json +28 -0
  98. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/03/merged.json +43 -0
  99. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/03/metadata.json +6 -0
  100. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/03/source.json +24 -0
  101. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/04/base.json +28 -0
  102. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/04/merged.json +28 -0
  103. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/04/metadata.json +6 -0
  104. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/04/source.json +24 -0
  105. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/05/base.json +20 -0
  106. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/05/merged.json +35 -0
  107. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/05/metadata.json +6 -0
  108. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/05/source.json +24 -0
  109. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/06/base.json +20 -0
  110. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/06/merged.json +35 -0
  111. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/06/metadata.json +6 -0
  112. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/06/source.json +24 -0
  113. package/test-fixtures/reducers/metadata.json +5 -0
  114. package/test-fixtures/reducers/select/01/metadata.json +5 -0
  115. package/test-fixtures/reducers/select/02/metadata.json +4 -0
  116. package/test-fixtures/reducers/select/03/metadata.json +4 -0
  117. package/test-fixtures/reducers/select/04/metadata.json +5 -0
  118. package/test-fixtures/reducers/select/05/metadata.json +4 -0
  119. package/test-fixtures/reducers/select/06/metadata.json +4 -0
  120. package/test-fixtures/reducers/select/07/metadata.json +5 -0
  121. package/test-fixtures/reducers/select/08/metadata.json +4 -0
  122. package/test-fixtures/reducers/select/09/metadata.json +4 -0
  123. package/test-fixtures/reducers/select/10/metadata.json +5 -0
  124. package/test-fixtures/reducers/select/11/metadata.json +4 -0
  125. package/test-fixtures/reducers/select/12/metadata.json +5 -0
  126. package/test-fixtures/reducers/select/13/metadata.json +4 -0
  127. package/test-fixtures/reducers/select/14/metadata.json +4 -0
  128. package/.nyc_output/72717fff-b4ac-4aef-a145-37cae88e07f8.json +0 -1
  129. package/.nyc_output/processinfo/72717fff-b4ac-4aef-a145-37cae88e07f8.json +0 -1
  130. package/.nyc_output/processinfo/index.json +0 -1
  131. package/LICENSE.txt +0 -165
  132. package/coverage/base.css +0 -224
  133. package/coverage/block-navigation.js +0 -79
  134. package/coverage/copy.js.html +0 -500
  135. package/coverage/favicon.png +0 -0
  136. package/coverage/index.html +0 -126
  137. package/coverage/lcov-report/base.css +0 -224
  138. package/coverage/lcov-report/block-navigation.js +0 -79
  139. package/coverage/lcov-report/copy.js.html +0 -500
  140. package/coverage/lcov-report/favicon.png +0 -0
  141. package/coverage/lcov-report/index.html +0 -126
  142. package/coverage/lcov-report/prettify.css +0 -1
  143. package/coverage/lcov-report/prettify.js +0 -2
  144. package/coverage/lcov-report/select.js.html +0 -539
  145. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  146. package/coverage/lcov-report/sorter.js +0 -170
  147. package/coverage/lcov.info +0 -274
  148. package/coverage/prettify.css +0 -1
  149. package/coverage/prettify.js +0 -2
  150. package/coverage/select.js.html +0 -539
  151. package/coverage/sort-arrow-sprite.png +0 -0
  152. package/coverage/sorter.js +0 -170
  153. package/test-fixtures/reducers/copy/01/tagPattern.txt +0 -1
  154. package/test-fixtures/reducers/copy/02/tagPattern.txt +0 -1
  155. package/test-fixtures/reducers/copy/03/tagPattern.txt +0 -1
  156. package/test-fixtures/reducers/copy/04/tagPattern.txt +0 -1
  157. package/test-fixtures/reducers/copy/05/tagPattern.txt +0 -1
  158. package/test-fixtures/reducers/copy/06/compareTagsOnly.txt +0 -1
  159. package/test-fixtures/reducers/copy/06/tagPattern.txt +0 -1
  160. package/test-fixtures/reducers/copy/07/excludeSubfields.json +0 -1
  161. package/test-fixtures/reducers/copy/07/tagPattern.txt +0 -1
  162. package/test-fixtures/reducers/copy/08/dropSubfields.json +0 -1
  163. package/test-fixtures/reducers/copy/08/tagPattern.txt +0 -1
  164. package/test-fixtures/reducers/copy/09/compareTagsOnly.txt +0 -1
  165. package/test-fixtures/reducers/copy/09/tagPattern.txt +0 -1
  166. package/test-fixtures/reducers/select/01/expected-error.txt +0 -1
  167. package/test-fixtures/reducers/select/01/pattern.txt +0 -1
  168. package/test-fixtures/reducers/select/02/pattern.txt +0 -1
  169. package/test-fixtures/reducers/select/03/pattern.txt +0 -1
  170. package/test-fixtures/reducers/select/04/pattern.txt +0 -1
  171. package/test-fixtures/reducers/select/05/pattern.txt +0 -1
  172. package/test-fixtures/reducers/select/06/pattern.txt +0 -1
  173. package/test-fixtures/reducers/select/07/equalityFunction.txt +0 -1
  174. package/test-fixtures/reducers/select/07/pattern.txt +0 -1
  175. package/test-fixtures/reducers/select/08/pattern.txt +0 -1
  176. package/test-fixtures/reducers/select/09/pattern.txt +0 -1
  177. package/test-fixtures/reducers/select/10/equalityFunction.txt +0 -1
  178. package/test-fixtures/reducers/select/10/pattern.txt +0 -1
  179. package/test-fixtures/reducers/select/11/pattern.txt +0 -1
  180. package/test-fixtures/reducers/select/12/equalityFunction.txt +0 -1
  181. package/test-fixtures/reducers/select/12/pattern.txt +0 -1
  182. package/test-fixtures/reducers/select/13/pattern.txt +0 -1
  183. package/test-fixtures/reducers/select/14/pattern.txt +0 -1
@@ -5,158 +5,293 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
+ var _marcRecord = require("@natlibfi/marc-record");
9
+
8
10
  var _debug = _interopRequireDefault(require("debug"));
9
11
 
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
13
 
12
- /**
13
- *
14
- * @licstart The following is the entire license notice for the JavaScript code in this file.
15
- *
16
- * Merge MARC records
17
- *
18
- * Copyright (C) 2015-2019 University Of Helsinki (The National Library Of Finland)
19
- *
20
- * This file is part of marc-record-merge-js
21
-
22
- * marc-record-merge-js program is free software: you can redistribute it and/or modify
23
- * it under the terms of the GNU Lesser General Public License as
24
- * published by the Free Software Foundation, either version 3 of the
25
- * License, or (at your option) any later version.
26
- *
27
- * marc-record-merge-js is distributed in the hope that it will be useful,
28
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
- * GNU Lesser General Public License for more details.
31
- *
32
- * You should have received a copy of the GNU Lesser General Public License
33
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
34
- *
35
- * @licend The above is the entire license notice
36
- * for the JavaScript code in this file.
37
- *
38
- */
39
-
40
- /**
41
- * Test 01: If base does not contain the field at all, it is copied from source to base
42
- * Test 02: Identical control fields are not copied
43
- * Test 03: Add missing control field to base
44
- * Test 04: Identical data fields in base and source, not copied
45
- * Test 05: Different data fields are copied from source to base (multiple fields)
46
- * Test 06: compareTagsOnly: Field is copied from source only if it is missing in base, a different instance is not copied
47
- * Test 07: excludeSubfields: Ignore excluded subfields in comparing identicalness
48
- * Test 08: dropSubfields: Drop subfields from source before copying
49
- * Test 09: compareTagsOnly for repeatable fields, 2x each 260/264
50
- * */
14
+ /* eslint-disable no-unused-vars */
51
15
  var _default = ({
52
16
  tagPattern,
53
17
  compareTagsOnly = false,
18
+ compareWithoutIndicators = false,
19
+ subfieldsMustBeIdentical = true,
54
20
  excludeSubfields = [],
55
- dropSubfields = []
21
+ dropSubfields = [],
22
+ copyUnless = [],
23
+ baseValidators = {
24
+ subfieldValues: false
25
+ },
26
+ sourceValidators = {
27
+ subfieldValues: false
28
+ }
56
29
  }) => (base, source) => {
30
+ const baseRecord = new _marcRecord.MarcRecord(base, baseValidators);
31
+ const sourceRecord = new _marcRecord.MarcRecord(source, sourceValidators);
57
32
  const debug = (0, _debug.default)('@natlibfi/marc-record-merge');
58
- const baseFields = base.get(tagPattern); // Check whether there are subfields to drop from source before copying
33
+ const debugOptions = (0, _debug.default)('@natlibfi/marc-record-merge:compare-options');
34
+ const debugCompare = (0, _debug.default)('@natlibfi/marc-record-merge:compare');
35
+ debugOptions(`Tag Pattern: ${tagPattern}`);
36
+ debugOptions(`Compare tags only: ${compareTagsOnly}`);
37
+ debugOptions(`Compare without indicators ${compareWithoutIndicators}`);
38
+ debugOptions(`Copy if identical: ${subfieldsMustBeIdentical}`);
39
+ debugOptions(`Exclude subfields: [${excludeSubfields}]`);
40
+ debugOptions(`Drop subfields [${dropSubfields}]`);
41
+ debugOptions(`Copy unless contains subfields: ${JSON.stringify(copyUnless)}`);
42
+ const baseFields = baseRecord.get(tagPattern);
43
+ const sourceFields = sourceRecord.get(tagPattern);
44
+ debug(`Base fields: `, baseFields);
45
+ debug(`Source fields: `, sourceFields);
46
+ const compareResultFields = compareFields(sourceFields, baseFields);
47
+ const droppedUnwantedSubfield = checkDropSubfields(compareResultFields);
48
+ const droppedUnwantedFields = checkCopyUnlessFields(droppedUnwantedSubfield);
49
+ debug('Fields to be copied');
50
+ debug(JSON.stringify(droppedUnwantedFields)); // Add fields to base;
51
+
52
+ droppedUnwantedFields.forEach(field => baseRecord.insertField(field));
53
+ return baseRecord.toObject(); //return copyFields(baseFields, sourceFields);
59
54
 
60
- const sourceFields = checkDropSubfields(source.get(tagPattern));
61
- return copyFields();
55
+ function compareFields(sourceFields, baseFields, uniqFields = []) {
56
+ const [sourceField, ...rest] = sourceFields;
62
57
 
63
- function copyFields() {
64
- const sourceTags = sourceFields.map(field => field.tag);
65
- sourceTags.forEach(tag => debug(`Comparing field ${tag}`)); // If compareTagsOnly = true, only this part is run
66
- // The field is copied from source only if it is missing completely from base
58
+ if (sourceField === undefined) {
59
+ return uniqFields;
60
+ }
67
61
 
68
62
  if (baseFields.length === 0) {
69
- sourceTags.forEach(tag => debug(`Missing field ${tag} copied from source to base`));
70
- sourceFields.forEach(f => base.insertField(f));
71
- return base;
72
- } // If compareTagsOnly = false (default)
73
- // Source and base are also compared for identicalness
63
+ return compareFields(rest, baseFields, [...uniqFields, sourceField]);
64
+ } // Source and base are also compared for identicalness
74
65
  // Non-identical fields are copied from source to base as duplicates
75
66
 
76
67
 
77
- if (!compareTagsOnly) {
78
- const filterMissing = function (sourceField) {
79
- if ('value' in sourceField) {
80
- debug(`Checking control field ${sourceField.tag} for identicalness`);
81
- return baseFields.some(isIdenticalControlField) === false;
82
- }
83
-
84
- if ('subfields' in sourceField) {
85
- debug(`Checking data field ${sourceField.tag} for identicalness`);
86
- return baseFields.some(isIdenticalDataField) === false;
87
- }
68
+ const sourceComapareField = createCompareField(sourceField);
69
+ const baseCompareFields = baseFields.map(baseField => createCompareField(baseField));
70
+ const unique = checkCompareFields(baseCompareFields, sourceComapareField);
71
+ debugCompare(`${JSON.stringify(sourceField)} ${unique ? 'is UNIQUE' : 'not UNIQUE'}`);
88
72
 
89
- function normalizeControlField(field) {
90
- return field.value.toLowerCase().replace(/\s+/u, '');
91
- }
73
+ if (unique) {
74
+ return compareFields(rest, baseFields, [...uniqFields, sourceField]);
75
+ }
92
76
 
93
- function isIdenticalControlField(baseField) {
94
- const normalizedBaseField = normalizeControlField(baseField);
95
- const normalizedSourceField = normalizeControlField(sourceField);
96
- return normalizedSourceField === normalizedBaseField;
97
- }
77
+ return compareFields(rest, baseFields, uniqFields);
98
78
 
99
- function isIdenticalDataField(baseField) {
100
- // If excluded subfields have been defined for this field, they must be ignored first
101
- // (i.e. source and base fields are considered identical if all non-excluded subfields are identical)
102
- if (excludeSubfields.length > 0 && sourceField.tag === baseField.tag && sourceField.ind1 === baseField.ind1 && sourceField.ind2 === baseField.ind2) {
103
- excludeSubfields.forEach(sub => debug(`Subfield ${sub} excluded from identicalness comparison`)); // Compare only those subfields that are not excluded
79
+ function checkCompareFields(baseCompareFields, sourceComapareField) {
80
+ const [baseCompareField, ...rest] = baseCompareFields;
104
81
 
105
- const baseSubsToCompare = baseField.subfields.filter(subfield => excludeSubfields.indexOf(subfield.code) === -1);
106
- return baseSubsToCompare.every(isIdenticalSubfield);
107
- } // If there are no excluded subfields (default case)
82
+ if (baseCompareField === undefined) {
83
+ return true;
84
+ }
108
85
 
86
+ if (sourceComapareField.value !== baseCompareField.value) {
87
+ debugCompare(`Value is different ${sourceComapareField.value} !== ${baseCompareField.value}`);
88
+ return true;
89
+ }
109
90
 
110
- if (sourceField.tag === baseField.tag && sourceField.ind1 === baseField.ind1 && sourceField.ind2 === baseField.ind2 && sourceField.subfields.length === baseField.subfields.length) {
111
- return baseField.subfields.every(isIdenticalSubfield);
112
- }
91
+ if (sourceComapareField.ind1 !== baseCompareField.ind1) {
92
+ debugCompare(`Ind1 is different ${sourceComapareField.ind1} !== ${baseCompareField.ind1}`);
93
+ return true;
94
+ }
113
95
 
114
- function normalizeSubfield(subfield) {
115
- return subfield.value.toLowerCase().replace(/\s+/u, '');
116
- }
96
+ if (sourceComapareField.ind2 !== baseCompareField.ind2) {
97
+ debugCompare(`Ind2 is different ${sourceComapareField.ind2} !== ${baseCompareField.ind2}`);
98
+ return true;
99
+ }
117
100
 
118
- function isIdenticalSubfield(baseSub) {
119
- const normBaseSub = normalizeSubfield(baseSub);
120
- return sourceField.subfields.some(sourceSub => {
121
- const normSourceSub = normalizeSubfield(sourceSub);
122
- return normSourceSub === normBaseSub;
123
- });
124
- }
125
- }
126
- }; // Search for fields missing from base
101
+ if ('subfields' in sourceComapareField) {
102
+ const allFound = checkSubfields(sourceComapareField.subfields, baseCompareField.subfields);
103
+ debugCompare(`Subfields are different ${!allFound}`);
104
+ return allFound ? false : checkCompareFields(rest, sourceComapareField);
105
+ }
127
106
 
107
+ return false;
108
+ }
128
109
 
129
- const missingFields = sourceFields.filter(filterMissing);
130
- missingFields.forEach(f => base.insertField(f));
110
+ function checkSubfields(sourceSubfields, baseSubfields) {
111
+ const foundSubs = sourceSubfields.filter(sSub => baseSubfields.some(bSub => sSub.code === bSub.code && sSub.value === bSub.value));
131
112
 
132
- if (missingFields.length > 0) {
133
- const missingTags = missingFields.map(field => field.tag);
134
- missingTags.forEach(tag => debug(`Field ${tag} copied from source to base`));
135
- return base;
113
+ if (subfieldsMustBeIdentical) {
114
+ return foundSubs.length === sourceSubfields.length && foundSubs.length === baseSubfields.length;
136
115
  }
137
116
 
138
- if (missingFields.length === 0) {
139
- debug(`No missing fields found`);
140
- return base;
141
- }
117
+ return foundSubs.length === sourceSubfields.length;
118
+ }
119
+ }
120
+
121
+ function createCompareField(field) {
122
+ if (compareTagsOnly) {
123
+ return {
124
+ tag: field.tag
125
+ };
126
+ }
127
+
128
+ if ('value' in field) {
129
+ return {
130
+ tag: field.tag,
131
+ value: field.value
132
+ };
142
133
  }
143
134
 
144
- debug(`No missing fields found`);
145
- return base;
135
+ const [filteredField] = checkDropSubfields([field]);
136
+ const params = [{
137
+ name: 'tag',
138
+ value: field.tag
139
+ }, {
140
+ name: 'ind1',
141
+ value: compareWithoutIndicators ? undefined : field.ind1
142
+ }, {
143
+ name: 'ind2',
144
+ value: compareWithoutIndicators ? undefined : field.ind2
145
+ }, {
146
+ name: 'subfields',
147
+ value: createCompareSubfields(filteredField.subfields)
148
+ }].map(param => [param.name, param.value]);
149
+ return Object.fromEntries(params);
150
+
151
+ function createCompareSubfields(subfields) {
152
+ const nonExcludedSubfields = subfields.filter(sub => !excludeSubfields.some(code => code === sub.code));
153
+ const normalizedSubfields = nonExcludedSubfields.map(sub => ({
154
+ code: sub.code,
155
+ value: normalizeSubfieldValue(sub.value)
156
+ }));
157
+ return normalizedSubfields;
158
+
159
+ function normalizeSubfieldValue(value) {
160
+ return value.toLowerCase().replace(/\s+/ug, '');
161
+ }
162
+ }
146
163
  }
147
164
 
148
165
  function checkDropSubfields(fields) {
149
166
  if (dropSubfields.length > 0) {
150
- dropSubfields.forEach(sub => debug(`Subfield ${sub} dropped from source field before copying`));
151
167
  return fields.map(field => ({ ...field,
152
- subfields: field.subfields.filter(subfield => dropSubfields.indexOf(subfield.code) === -1)
153
- }));
168
+ subfields: dropSubfieldsFunc(field.subfields)
169
+ })).filter(field => field.subfields.length > 0);
170
+ }
171
+
172
+ return fields;
173
+
174
+ function dropSubfieldsFunc(subfields) {
175
+ return subfields.filter(sub => {
176
+ // eslint-disable-line
177
+ return !dropSubfields.some(({
178
+ code,
179
+ value = false,
180
+ condition = false
181
+ }) => {
182
+ if (code !== sub.code) {
183
+ return false;
184
+ }
185
+
186
+ if (!condition && value) {
187
+ return value === sub.value;
188
+ }
189
+
190
+ if (condition === 'unless' && value) {
191
+ return !new RegExp(value, 'u').test(sub.value);
192
+ }
193
+
194
+ return true;
195
+ });
196
+ });
197
+ }
198
+ }
199
+
200
+ function checkCopyUnlessFields(fields) {
201
+ if (copyUnless.length > 0) {
202
+ return fields.filter(({
203
+ subfields
204
+ }) => copyUnless.some(filter => !subfields.some(sub => sub.code === filter.code && new RegExp(filter.value, 'u').test(sub.value))));
154
205
  }
155
206
 
156
- debug(`No subfields to drop`);
157
207
  return fields;
158
208
  }
159
- };
209
+ }; // function copyFields() { //eslint-disable-line no-unused-vars
210
+ // const sourceTags = sourceFields.map(field => field.tag);
211
+ // sourceTags.forEach(tag => debug(`Comparing field ${tag}`));
212
+ // /*
213
+ // if (combine.length > 0) {
214
+ // debug(`*** NOW Copy options: ${tagPattern}, ${compareTagsOnly}, ${compareWithoutIndicators}, ${subfieldsMustBeIdentical}, [${combine}], [${excludeSubfields}], [${dropSubfields}]`);
215
+ // combine.forEach(row => debug(` ### combine ${row} <- `));
216
+ // return [];
217
+ // }
218
+ // */
219
+ // // If compareTagsOnly = true, only this part is run
220
+ // // The field is copied from source only if it is missing completely from base
221
+ // if (compareTagsOnly && baseFields.length === 0) {
222
+ // sourceTags.forEach(tag => debug(`Missing field ${tag} copied from source to base`));
223
+ // sourceFields.forEach(f => base.insertField(f));
224
+ // return true;
225
+ // }
226
+ // // If compareTagsOnly = false (default)
227
+ // // Source and base are also compared for identicalness
228
+ // // Non-identical fields are copied from source to base as duplicates
229
+ // if (!compareTagsOnly) {
230
+ // const filterMissing = function (sourceField) {
231
+ // if ('value' in sourceField) {
232
+ // debug(`Checking control field ${sourceField.tag} for identicalness`);
233
+ // return baseFields.some(isIdenticalControlField) === false;
234
+ // }
235
+ // if ('subfields' in sourceField) {
236
+ // debug(`Checking data field ${sourceField.tag} for identicalness`);
237
+ // return baseFields.some(isIdenticalDataField) === false;
238
+ // }
239
+ // function normalizeControlField(field) {
240
+ // return field.value.toLowerCase().replace(/\s+/u, '');
241
+ // }
242
+ // function isIdenticalControlField(baseField) {
243
+ // const normalizedBaseField = normalizeControlField(baseField);
244
+ // const normalizedSourceField = normalizeControlField(sourceField);
245
+ // return normalizedSourceField === normalizedBaseField;
246
+ // }
247
+ // function isIdenticalDataField(baseField) {
248
+ // // If excluded subfields have been defined for this field, they must be ignored first
249
+ // // (i.e. source and base fields are considered identical if all non-excluded subfields are identical)
250
+ // if (excludeSubfields.length > 0 &&
251
+ // sourceField.tag === baseField.tag &&
252
+ // sourceField.ind1 === baseField.ind1 &&
253
+ // sourceField.ind2 === baseField.ind2) {
254
+ // excludeSubfields.forEach(sub => debug(`Subfield ${sub} excluded from identicalness comparison`));
255
+ // // Compare only those subfields that are not excluded
256
+ // const baseSubsToCompare = baseField.subfields.filter(subfield => excludeSubfields.indexOf(subfield.code) === -1);
257
+ // return baseSubsToCompare.every(isIdenticalSubfield);
258
+ // }
259
+ // // If there are no excluded subfields (default case)
260
+ // if (sourceField.tag === baseField.tag &&
261
+ // sourceField.ind1 === baseField.ind1 &&
262
+ // sourceField.ind2 === baseField.ind2 &&
263
+ // sourceField.subfields.length === baseField.subfields.length) {
264
+ // return baseField.subfields.every(isIdenticalSubfield);
265
+ // }
266
+ // function normalizeSubfield(subfield) {
267
+ // return subfield.value.toLowerCase().replace(/\s+/u, '');
268
+ // }
269
+ // function isIdenticalSubfield(baseSub) {
270
+ // const normBaseSub = normalizeSubfield(baseSub);
271
+ // return sourceField.subfields.some(sourceSub => {
272
+ // const normSourceSub = normalizeSubfield(sourceSub);
273
+ // return normSourceSub === normBaseSub;
274
+ // });
275
+ // }
276
+ // }
277
+ // };
278
+ // // Search for fields missing from base
279
+ // const missingFields = sourceFields.filter(filterMissing);
280
+ // missingFields.forEach(f => base.insertField(f));
281
+ // if (missingFields.length > 0) {
282
+ // const missingTags = missingFields.map(field => field.tag);
283
+ // missingTags.forEach(tag => debug(`Field ${tag} copied from source to base`));
284
+ // return base;
285
+ // }
286
+ // if (missingFields.length === 0) {
287
+ // debug(`No missing fields found`);
288
+ // return base;
289
+ // }
290
+ // }
291
+ // debug(`No missing fields found`);
292
+ // return base;
293
+ // }
294
+
160
295
 
161
296
  exports.default = _default;
162
297
  //# sourceMappingURL=copy.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/reducers/copy.js"],"names":["tagPattern","compareTagsOnly","excludeSubfields","dropSubfields","base","source","debug","baseFields","get","sourceFields","checkDropSubfields","copyFields","sourceTags","map","field","tag","forEach","length","f","insertField","filterMissing","sourceField","some","isIdenticalControlField","isIdenticalDataField","normalizeControlField","value","toLowerCase","replace","baseField","normalizedBaseField","normalizedSourceField","ind1","ind2","sub","baseSubsToCompare","subfields","filter","subfield","indexOf","code","every","isIdenticalSubfield","normalizeSubfield","baseSub","normBaseSub","sourceSub","normSourceSub","missingFields","missingTags","fields"],"mappings":";;;;;;;AAuCA;;;;AAvCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;eAGe,CAAC;AAACA,EAAAA,UAAD;AAAaC,EAAAA,eAAe,GAAG,KAA/B;AAAsCC,EAAAA,gBAAgB,GAAG,EAAzD;AAA6DC,EAAAA,aAAa,GAAG;AAA7E,CAAD,KAAsF,CAACC,IAAD,EAAOC,MAAP,KAAkB;AACrH,QAAMC,KAAK,GAAG,oBAAkB,6BAAlB,CAAd;AACA,QAAMC,UAAU,GAAGH,IAAI,CAACI,GAAL,CAASR,UAAT,CAAnB,CAFqH,CAGrH;;AACA,QAAMS,YAAY,GAAGC,kBAAkB,CAACL,MAAM,CAACG,GAAP,CAAWR,UAAX,CAAD,CAAvC;AACA,SAAOW,UAAU,EAAjB;;AAEA,WAASA,UAAT,GAAsB;AACpB,UAAMC,UAAU,GAAGH,YAAY,CAACI,GAAb,CAAiBC,KAAK,IAAIA,KAAK,CAACC,GAAhC,CAAnB;AACAH,IAAAA,UAAU,CAACI,OAAX,CAAmBD,GAAG,IAAIT,KAAK,CAAE,mBAAkBS,GAAI,EAAxB,CAA/B,EAFoB,CAIpB;AACA;;AACA,QAAIR,UAAU,CAACU,MAAX,KAAsB,CAA1B,EAA6B;AAC3BL,MAAAA,UAAU,CAACI,OAAX,CAAmBD,GAAG,IAAIT,KAAK,CAAE,iBAAgBS,GAAI,6BAAtB,CAA/B;AAC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I,CAACe,WAAL,CAAiBD,CAAjB,CAA3B;;AACA,UAAI8B,aAAa,CAAC/B,MAAd,GAAuB,CAA3B,EAA8B;AAC5B,cAAMgC,WAAW,GAAGD,aAAa,CAACnC,GAAd,CAAkBC,KAAK,IAAIA,KAAK,CAACC,GAAjC,CAApB;AACAkC,QAAAA,WAAW,CAACjC,OAAZ,CAAoBD,GAAG,IAAIT,KAAK,CAAE,SAAQS,GAAI,6BAAd,CAAhC;AACA,eAAOX,IAAP;AACD;;AACD,UAAI4C,aAAa,CAAC/B,MAAd,KAAyB,CAA7B,EAAgC;AAC9BX,QAAAA,KAAK,CAAE,yBAAF,CAAL;AACA,eAAOF,IAAP;AACD;AACF;;AACDE,IAAAA,KAAK,CAAE,yBAAF,CAAL;AACA,WAAOF,IAAP;AACD;;AAED,WAASM,kBAAT,CAA4BwC,MAA5B,EAAoC;AAClC,QAAI/C,aAAa,CAACc,MAAd,GAAuB,CAA3B,EAA8B;AAC5Bd,MAAAA,aAAa,CAACa,OAAd,CAAsBkB,GAAG,IAAI5B,KAAK,CAAE,YAAW4B,GAAI,2CAAjB,CAAlC;AACA,aAAOgB,MAAM,CAACrC,GAAP,CAAYC,KAAD,KAAY,EAAC,GAAGA,KAAJ;AAAWsB,QAAAA,SAAS,EAAEtB,KAAK,CAACsB,SAAN,CAAgBC,MAAhB,CAAwBC,QAAD,IAAcnC,aAAa,CAACoC,OAAd,CAAsBD,QAAQ,CAACE,IAA/B,MAAyC,CAAC,CAA/E;AAAtB,OAAZ,CAAX,CAAP;AACD;;AACDlC,IAAAA,KAAK,CAAE,sBAAF,CAAL;AACA,WAAO4C,MAAP;AACD;AACF,C","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Merge MARC records\n*\n* Copyright (C) 2015-2019 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of marc-record-merge-js\n\n* marc-record-merge-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* marc-record-merge-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Lesser General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\n/**\n * Test 01: If base does not contain the field at all, it is copied from source to base\n * Test 02: Identical control fields are not copied\n * Test 03: Add missing control field to base\n * Test 04: Identical data fields in base and source, not copied\n * Test 05: Different data fields are copied from source to base (multiple fields)\n * Test 06: compareTagsOnly: Field is copied from source only if it is missing in base, a different instance is not copied\n * Test 07: excludeSubfields: Ignore excluded subfields in comparing identicalness\n * Test 08: dropSubfields: Drop subfields from source before copying\n * Test 09: compareTagsOnly for repeatable fields, 2x each 260/264\n * */\nimport createDebugLogger from 'debug';\n\nexport default ({tagPattern, compareTagsOnly = false, excludeSubfields = [], dropSubfields = []}) => (base, source) => {\n const debug = createDebugLogger('@natlibfi/marc-record-merge');\n const baseFields = base.get(tagPattern);\n // Check whether there are subfields to drop from source before copying\n const sourceFields = checkDropSubfields(source.get(tagPattern));\n return copyFields();\n\n function copyFields() {\n const sourceTags = sourceFields.map(field => field.tag);\n sourceTags.forEach(tag => debug(`Comparing field ${tag}`));\n\n // If compareTagsOnly = true, only this part is run\n // The field is copied from source only if it is missing completely from base\n if (baseFields.length === 0) {\n sourceTags.forEach(tag => debug(`Missing field ${tag} copied from source to base`));\n sourceFields.forEach(f => base.insertField(f));\n return base;\n }\n\n // If compareTagsOnly = false (default)\n // Source and base are also compared for identicalness\n // Non-identical fields are copied from source to base as duplicates\n if (!compareTagsOnly) {\n const filterMissing = function(sourceField) {\n if ('value' in sourceField) {\n debug(`Checking control field ${sourceField.tag} for identicalness`);\n return baseFields.some(isIdenticalControlField) === false;\n }\n if ('subfields' in sourceField) {\n debug(`Checking data field ${sourceField.tag} for identicalness`);\n return baseFields.some(isIdenticalDataField) === false;\n }\n\n function normalizeControlField(field) {\n return field.value.toLowerCase().replace(/\\s+/u, '');\n }\n\n function isIdenticalControlField(baseField) {\n const normalizedBaseField = normalizeControlField(baseField);\n const normalizedSourceField = normalizeControlField(sourceField);\n return normalizedSourceField === normalizedBaseField;\n }\n function isIdenticalDataField(baseField) {\n // If excluded subfields have been defined for this field, they must be ignored first\n // (i.e. source and base fields are considered identical if all non-excluded subfields are identical)\n if (excludeSubfields.length > 0 &&\n sourceField.tag === baseField.tag &&\n sourceField.ind1 === baseField.ind1 &&\n sourceField.ind2 === baseField.ind2) {\n excludeSubfields.forEach(sub => debug(`Subfield ${sub} excluded from identicalness comparison`));\n // Compare only those subfields that are not excluded\n const baseSubsToCompare = baseField.subfields.filter(subfield => excludeSubfields.indexOf(subfield.code) === -1);\n return baseSubsToCompare.every(isIdenticalSubfield);\n }\n // If there are no excluded subfields (default case)\n if (sourceField.tag === baseField.tag &&\n sourceField.ind1 === baseField.ind1 &&\n sourceField.ind2 === baseField.ind2 &&\n sourceField.subfields.length === baseField.subfields.length) {\n return baseField.subfields.every(isIdenticalSubfield);\n }\n function normalizeSubfield(subfield) {\n return subfield.value.toLowerCase().replace(/\\s+/u, '');\n }\n function isIdenticalSubfield(baseSub) {\n const normBaseSub = normalizeSubfield(baseSub);\n return sourceField.subfields.some(sourceSub => {\n const normSourceSub = normalizeSubfield(sourceSub);\n return normSourceSub === normBaseSub;\n });\n }\n }\n };\n // Search for fields missing from base\n const missingFields = sourceFields.filter(filterMissing);\n missingFields.forEach(f => base.insertField(f));\n if (missingFields.length > 0) {\n const missingTags = missingFields.map(field => field.tag);\n missingTags.forEach(tag => debug(`Field ${tag} copied from source to base`));\n return base;\n }\n if (missingFields.length === 0) {\n debug(`No missing fields found`);\n return base;\n }\n }\n debug(`No missing fields found`);\n return base;\n }\n\n function checkDropSubfields(fields) {\n if (dropSubfields.length > 0) {\n dropSubfields.forEach(sub => debug(`Subfield ${sub} dropped from source field before copying`));\n return fields.map((field) => ({...field, subfields: field.subfields.filter((subfield) => dropSubfields.indexOf(subfield.code) === -1)}));\n }\n debug(`No subfields to drop`);\n return fields;\n }\n};\n"],"file":"copy.js"}
1
+ {"version":3,"file":"copy.js","names":["tagPattern","compareTagsOnly","compareWithoutIndicators","subfieldsMustBeIdentical","excludeSubfields","dropSubfields","copyUnless","baseValidators","subfieldValues","sourceValidators","base","source","baseRecord","MarcRecord","sourceRecord","debug","createDebugLogger","debugOptions","debugCompare","JSON","stringify","baseFields","get","sourceFields","compareResultFields","compareFields","droppedUnwantedSubfield","checkDropSubfields","droppedUnwantedFields","checkCopyUnlessFields","forEach","field","insertField","toObject","uniqFields","sourceField","rest","undefined","length","sourceComapareField","createCompareField","baseCompareFields","map","baseField","unique","checkCompareFields","baseCompareField","value","ind1","ind2","allFound","checkSubfields","subfields","sourceSubfields","baseSubfields","foundSubs","filter","sSub","some","bSub","code","tag","filteredField","params","name","createCompareSubfields","param","Object","fromEntries","nonExcludedSubfields","sub","normalizedSubfields","normalizeSubfieldValue","toLowerCase","replace","fields","dropSubfieldsFunc","condition","RegExp","test"],"sources":["../../src/reducers/copy.js"],"sourcesContent":["/* eslint-disable no-unused-vars */\n\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport createDebugLogger from 'debug';\n\nexport default ({\n tagPattern,\n compareTagsOnly = false,\n compareWithoutIndicators = false,\n subfieldsMustBeIdentical = true,\n excludeSubfields = [],\n dropSubfields = [],\n copyUnless = [],\n baseValidators = {subfieldValues: false},\n sourceValidators = {subfieldValues: false}\n}) => (base, source) => {\n const baseRecord = new MarcRecord(base, baseValidators);\n const sourceRecord = new MarcRecord(source, sourceValidators);\n\n const debug = createDebugLogger('@natlibfi/marc-record-merge');\n const debugOptions = createDebugLogger('@natlibfi/marc-record-merge:compare-options');\n const debugCompare = createDebugLogger('@natlibfi/marc-record-merge:compare');\n debugOptions(`Tag Pattern: ${tagPattern}`);\n debugOptions(`Compare tags only: ${compareTagsOnly}`);\n debugOptions(`Compare without indicators ${compareWithoutIndicators}`);\n debugOptions(`Copy if identical: ${subfieldsMustBeIdentical}`);\n debugOptions(`Exclude subfields: [${excludeSubfields}]`);\n debugOptions(`Drop subfields [${dropSubfields}]`);\n debugOptions(`Copy unless contains subfields: ${JSON.stringify(copyUnless)}`);\n\n const baseFields = baseRecord.get(tagPattern);\n const sourceFields = sourceRecord.get(tagPattern);\n\n debug(`Base fields: `, baseFields);\n debug(`Source fields: `, sourceFields);\n\n const compareResultFields = compareFields(sourceFields, baseFields);\n const droppedUnwantedSubfield = checkDropSubfields(compareResultFields);\n const droppedUnwantedFields = checkCopyUnlessFields(droppedUnwantedSubfield);\n debug('Fields to be copied');\n debug(JSON.stringify(droppedUnwantedFields));\n\n // Add fields to base;\n droppedUnwantedFields.forEach(field => baseRecord.insertField(field));\n return baseRecord.toObject();\n //return copyFields(baseFields, sourceFields);\n\n function compareFields(sourceFields, baseFields, uniqFields = []) {\n const [sourceField, ...rest] = sourceFields;\n if (sourceField === undefined) {\n return uniqFields;\n }\n\n if (baseFields.length === 0) {\n return compareFields(rest, baseFields, [...uniqFields, sourceField]);\n }\n\n // Source and base are also compared for identicalness\n // Non-identical fields are copied from source to base as duplicates\n const sourceComapareField = createCompareField(sourceField);\n const baseCompareFields = baseFields.map(baseField => createCompareField(baseField));\n\n const unique = checkCompareFields(baseCompareFields, sourceComapareField);\n\n debugCompare(`${JSON.stringify(sourceField)} ${unique ? 'is UNIQUE' : 'not UNIQUE'}`);\n\n if (unique) {\n return compareFields(rest, baseFields, [...uniqFields, sourceField]);\n }\n\n return compareFields(rest, baseFields, uniqFields);\n\n function checkCompareFields(baseCompareFields, sourceComapareField) {\n const [baseCompareField, ...rest] = baseCompareFields;\n if (baseCompareField === undefined) {\n return true;\n }\n\n if (sourceComapareField.value !== baseCompareField.value) {\n debugCompare(`Value is different ${sourceComapareField.value} !== ${baseCompareField.value}`);\n return true;\n }\n\n if (sourceComapareField.ind1 !== baseCompareField.ind1) {\n debugCompare(`Ind1 is different ${sourceComapareField.ind1} !== ${baseCompareField.ind1}`);\n return true;\n }\n\n if (sourceComapareField.ind2 !== baseCompareField.ind2) {\n debugCompare(`Ind2 is different ${sourceComapareField.ind2} !== ${baseCompareField.ind2}`);\n return true;\n }\n\n if ('subfields' in sourceComapareField) {\n const allFound = checkSubfields(sourceComapareField.subfields, baseCompareField.subfields);\n debugCompare(`Subfields are different ${!allFound}`);\n return allFound ? false : checkCompareFields(rest, sourceComapareField);\n }\n\n return false;\n }\n\n function checkSubfields(sourceSubfields, baseSubfields) {\n const foundSubs = sourceSubfields.filter(sSub => baseSubfields.some(bSub => sSub.code === bSub.code && sSub.value === bSub.value));\n\n if (subfieldsMustBeIdentical) {\n return foundSubs.length === sourceSubfields.length && foundSubs.length === baseSubfields.length;\n }\n\n return foundSubs.length === sourceSubfields.length;\n }\n }\n\n function createCompareField(field) {\n if (compareTagsOnly) {\n return {tag: field.tag};\n }\n\n if ('value' in field) {\n return {tag: field.tag, value: field.value};\n }\n\n const [filteredField] = checkDropSubfields([field]);\n\n const params = [\n {name: 'tag', value: field.tag},\n {name: 'ind1', value: compareWithoutIndicators ? undefined : field.ind1},\n {name: 'ind2', value: compareWithoutIndicators ? undefined : field.ind2},\n {name: 'subfields', value: createCompareSubfields(filteredField.subfields)}\n ].map(param => [param.name, param.value]);\n\n return Object.fromEntries(params);\n\n function createCompareSubfields(subfields) {\n const nonExcludedSubfields = subfields.filter(sub => !excludeSubfields.some(code => code === sub.code));\n const normalizedSubfields = nonExcludedSubfields.map(sub => ({code: sub.code, value: normalizeSubfieldValue(sub.value)}));\n\n return normalizedSubfields;\n\n function normalizeSubfieldValue(value) {\n return value.toLowerCase().replace(/\\s+/ug, '');\n }\n }\n }\n\n function checkDropSubfields(fields) {\n if (dropSubfields.length > 0) {\n return fields.map(field => ({...field, subfields: dropSubfieldsFunc(field.subfields)}))\n .filter(field => field.subfields.length > 0);\n }\n\n return fields;\n\n function dropSubfieldsFunc(subfields) {\n return subfields.filter(sub => { // eslint-disable-line\n return !dropSubfields.some(({code, value = false, condition = false}) => {\n if (code !== sub.code) {\n return false;\n }\n\n if (!condition && value) {\n return value === sub.value;\n }\n\n if (condition === 'unless' && value) {\n return !new RegExp(value, 'u').test(sub.value);\n }\n\n return true;\n });\n });\n }\n }\n\n function checkCopyUnlessFields(fields) {\n if (copyUnless.length > 0) {\n return fields.filter(({subfields}) => copyUnless.some(filter => !subfields.some(sub => sub.code === filter.code && new RegExp(filter.value, 'u').test(sub.value))));\n }\n\n return fields;\n }\n};\n\n// function copyFields() { //eslint-disable-line no-unused-vars\n// const sourceTags = sourceFields.map(field => field.tag);\n// sourceTags.forEach(tag => debug(`Comparing field ${tag}`));\n\n// /*\n// if (combine.length > 0) {\n// debug(`*** NOW Copy options: ${tagPattern}, ${compareTagsOnly}, ${compareWithoutIndicators}, ${subfieldsMustBeIdentical}, [${combine}], [${excludeSubfields}], [${dropSubfields}]`);\n// combine.forEach(row => debug(` ### combine ${row} <- `));\n// return [];\n// }\n// */\n\n// // If compareTagsOnly = true, only this part is run\n// // The field is copied from source only if it is missing completely from base\n// if (compareTagsOnly && baseFields.length === 0) {\n// sourceTags.forEach(tag => debug(`Missing field ${tag} copied from source to base`));\n// sourceFields.forEach(f => base.insertField(f));\n// return true;\n// }\n\n// // If compareTagsOnly = false (default)\n// // Source and base are also compared for identicalness\n// // Non-identical fields are copied from source to base as duplicates\n// if (!compareTagsOnly) {\n// const filterMissing = function (sourceField) {\n// if ('value' in sourceField) {\n// debug(`Checking control field ${sourceField.tag} for identicalness`);\n// return baseFields.some(isIdenticalControlField) === false;\n// }\n// if ('subfields' in sourceField) {\n// debug(`Checking data field ${sourceField.tag} for identicalness`);\n// return baseFields.some(isIdenticalDataField) === false;\n// }\n\n// function normalizeControlField(field) {\n// return field.value.toLowerCase().replace(/\\s+/u, '');\n// }\n\n// function isIdenticalControlField(baseField) {\n// const normalizedBaseField = normalizeControlField(baseField);\n// const normalizedSourceField = normalizeControlField(sourceField);\n// return normalizedSourceField === normalizedBaseField;\n// }\n\n// function isIdenticalDataField(baseField) {\n// // If excluded subfields have been defined for this field, they must be ignored first\n// // (i.e. source and base fields are considered identical if all non-excluded subfields are identical)\n// if (excludeSubfields.length > 0 &&\n// sourceField.tag === baseField.tag &&\n// sourceField.ind1 === baseField.ind1 &&\n// sourceField.ind2 === baseField.ind2) {\n// excludeSubfields.forEach(sub => debug(`Subfield ${sub} excluded from identicalness comparison`));\n// // Compare only those subfields that are not excluded\n// const baseSubsToCompare = baseField.subfields.filter(subfield => excludeSubfields.indexOf(subfield.code) === -1);\n// return baseSubsToCompare.every(isIdenticalSubfield);\n// }\n// // If there are no excluded subfields (default case)\n// if (sourceField.tag === baseField.tag &&\n// sourceField.ind1 === baseField.ind1 &&\n// sourceField.ind2 === baseField.ind2 &&\n// sourceField.subfields.length === baseField.subfields.length) {\n// return baseField.subfields.every(isIdenticalSubfield);\n// }\n// function normalizeSubfield(subfield) {\n// return subfield.value.toLowerCase().replace(/\\s+/u, '');\n// }\n// function isIdenticalSubfield(baseSub) {\n// const normBaseSub = normalizeSubfield(baseSub);\n// return sourceField.subfields.some(sourceSub => {\n// const normSourceSub = normalizeSubfield(sourceSub);\n// return normSourceSub === normBaseSub;\n// });\n// }\n// }\n// };\n// // Search for fields missing from base\n// const missingFields = sourceFields.filter(filterMissing);\n// missingFields.forEach(f => base.insertField(f));\n// if (missingFields.length > 0) {\n// const missingTags = missingFields.map(field => field.tag);\n// missingTags.forEach(tag => debug(`Field ${tag} copied from source to base`));\n// return base;\n// }\n// if (missingFields.length === 0) {\n// debug(`No missing fields found`);\n// return base;\n// }\n// }\n// debug(`No missing fields found`);\n// return base;\n// }\n"],"mappings":";;;;;;;AAEA;;AACA;;;;AAHA;eAKe,CAAC;EACdA,UADc;EAEdC,eAAe,GAAG,KAFJ;EAGdC,wBAAwB,GAAG,KAHb;EAIdC,wBAAwB,GAA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A;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA"}
@@ -1,114 +1,68 @@
1
1
  "use strict";
2
2
 
3
- var _chai = _interopRequireDefault(require("chai"));
3
+ var _chai = require("chai");
4
4
 
5
- var _fs = _interopRequireDefault(require("fs"));
6
-
7
- var _path = _interopRequireDefault(require("path"));
5
+ var _fixura = require("@natlibfi/fixura");
8
6
 
9
7
  var _marcRecord = require("@natlibfi/marc-record");
10
8
 
11
9
  var _copy = _interopRequireDefault(require("./copy"));
12
10
 
13
- var _fixura = _interopRequireWildcard(require("@natlibfi/fixura"));
14
-
15
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
16
-
17
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
+ var _fixugen = _interopRequireDefault(require("@natlibfi/fixugen"));
18
12
 
19
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
14
 
21
- /**
22
- *
23
- * @licstart The following is the entire license notice for the JavaScript code in this file.
24
- *
25
- * Merge MARC records
26
- *
27
- * Copyright (C) 2015-2019 University Of Helsinki (The National Library Of Finland)
28
- *
29
- * This file is part of marc-record-merge-js
30
-
31
- * marc-record-merge-js program is free software: you can redistribute it and/or modify
32
- * it under the terms of the GNU Lesser General Public License as
33
- * published by the Free Software Foundation, either version 3 of the
34
- * License, or (at your option) any later version.
35
- *
36
- * marc-record-merge-js is distributed in the hope that it will be useful,
37
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39
- * GNU Lesser General Public License for more details.
40
- *
41
- * You should have received a copy of the GNU Lesser General Public License
42
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
43
- *
44
- * @licend The above is the entire license notice
45
- * for the JavaScript code in this file.
46
- *
47
- */
48
- _marcRecord.MarcRecord.setValidationOptions({
49
- subfieldValues: false
15
+ // import createDebugLogger from 'debug'; // <---
16
+ // const debug = createDebugLogger('@natlibfi/marc-record-merge/copy.spec.js'); // <---
17
+ (0, _fixugen.default)({
18
+ callback,
19
+ path: [__dirname, '..', '..', 'test-fixtures', 'reducers', 'copy'],
20
+ useMetadataFile: true,
21
+ recurse: true,
22
+ fixura: {
23
+ reader: _fixura.READERS.JSON,
24
+ failWhenNotFound: false
25
+ }
50
26
  });
51
27
 
52
- describe('reducers/copy', () => {
53
- const {
54
- expect
55
- } = _chai.default;
56
-
57
- const fixturesPath = _path.default.join(__dirname, '..', '..', 'test-fixtures', 'reducers', 'copy');
58
-
59
- _fs.default.readdirSync(fixturesPath).forEach(subDir => {
60
- const {
61
- getFixture
62
- } = (0, _fixura.default)({
63
- root: [fixturesPath, subDir],
64
- reader: _fixura.READERS.JSON,
65
- failWhenNotFound: false
66
- });
67
- it(subDir, () => {
68
- const base = new _marcRecord.MarcRecord(getFixture('base.json'));
69
- const source = new _marcRecord.MarcRecord(getFixture('source.json'));
70
- const tagPattern = new RegExp(getFixture({
71
- components: ['tagPattern.txt'],
72
- reader: _fixura.READERS.TEXT
73
- }), 'u');
74
- const compareTagsOnly = getCompareTagsOnly();
75
- const excludeSubfields = getExcludeSubfields();
76
- const dropSubfields = getDropSubfields();
77
- const expectedRecord = getFixture('merged.json');
78
- const mergedRecord = (0, _copy.default)({
79
- tagPattern,
80
- compareTagsOnly,
81
- excludeSubfields,
82
- dropSubfields
83
- })(base, source);
84
- expect(mergedRecord.toObject()).to.eql(expectedRecord); // Non-repeatable MARC fields are copied from source only if they are missing from base
85
-
86
- function getCompareTagsOnly() {
87
- const functionName = getFixture({
88
- components: ['compareTagsOnly.txt'],
89
- reader: _fixura.READERS.TEXT
90
- });
91
- return functionName === 'true' ? 'true' : undefined;
92
- } // Check whether excludeSubfields.json exists and if it does, return its contents. If not, do nothing.
93
-
94
-
95
- function getExcludeSubfields() {
96
- const subfieldsToExclude = getFixture({
97
- components: ['excludeSubfields.json'],
98
- reader: _fixura.READERS.JSON
99
- });
100
- return subfieldsToExclude ? subfieldsToExclude : undefined;
101
- } // Check whether dropSubfields.json exists and if it does, return its contents. If not, do nothing.
102
-
103
-
104
- function getDropSubfields() {
105
- const subfieldsToDrop = getFixture({
106
- components: ['dropSubfields.json'],
107
- reader: _fixura.READERS.JSON
108
- });
109
- return subfieldsToDrop ? subfieldsToDrop : undefined;
110
- }
111
- });
28
+ function callback({
29
+ getFixture,
30
+ tagPatternRegExp,
31
+ compareTagsOnly = false,
32
+ compareWithoutIndicators = false,
33
+ subfieldsMustBeIdentical = false,
34
+ copyUnless = undefined,
35
+ excludeSubfields = undefined,
36
+ dropSubfields = undefined,
37
+ disabled = false
38
+ }) {
39
+ if (disabled) {
40
+ console.log('TEST DISABLED!'); // eslint-disable-line no-console
41
+
42
+ return;
43
+ }
44
+
45
+ const base = new _marcRecord.MarcRecord(getFixture('base.json'), {
46
+ subfieldValues: false
112
47
  });
113
- });
48
+ const source = new _marcRecord.MarcRecord(getFixture('source.json'), {
49
+ subfieldValues: false
50
+ });
51
+ const tagPattern = new RegExp(tagPatternRegExp, 'u');
52
+ const expectedRecord = getFixture('merged.json');
53
+ const mergedRecord = (0, _copy.default)({
54
+ tagPattern,
55
+ compareTagsOnly,
56
+ compareWithoutIndicators,
57
+ copyUnless,
58
+ subfieldsMustBeIdentical,
59
+ excludeSubfields,
60
+ dropSubfields
61
+ })(base, source); //debug(`*** mergedRecord: `, mergedRecord); //<--
62
+ //debug(`*** mergedRecord,Strfy: `, JSON.stringify(mergedRecord.toObject())); //<--
63
+ //debug(`*** expectedRecord: `, expectedRecord); //<--
64
+ //debug(`*** expectedRecord,Strfy: `, JSON.stringify(expectedRecord)); //<--
65
+
66
+ (0, _chai.expect)(mergedRecord).to.eql(expectedRecord);
67
+ }
114
68
  //# sourceMappingURL=copy.spec.js.map