@natlibfi/marc-record-merge 6.0.0-beta.1 → 6.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) 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 +10 -41
  7. package/dist/index.js.map +1 -1
  8. package/dist/reducers/copy.js +280 -73
  9. package/dist/reducers/copy.js.map +1 -1
  10. package/dist/reducers/copy.spec.js +51 -62
  11. package/dist/reducers/copy.spec.js.map +1 -1
  12. package/dist/reducers/index.js +15 -20
  13. package/dist/reducers/index.js.map +1 -1
  14. package/dist/reducers/select.js +28 -34
  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 +33 -32
  19. package/src/index.js +2 -34
  20. package/src/reducers/copy.js +274 -79
  21. package/src/reducers/copy.spec.js +43 -47
  22. package/src/reducers/index.js +3 -30
  23. package/src/reducers/select.js +22 -32
  24. package/src/reducers/select.spec.js +37 -56
  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 +4 -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 +4 -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 +4 -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 +4 -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/basic copy/05/merged.json +77 -0
  43. package/test-fixtures/reducers/copy/basic copy/05/metadata.json +4 -0
  44. package/test-fixtures/reducers/copy/basic copy/05/source.json +62 -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 +4 -0
  48. package/test-fixtures/reducers/copy/basic copy/06/source.json +24 -0
  49. package/test-fixtures/reducers/copy/basic copy/07/base.json +39 -0
  50. package/test-fixtures/reducers/copy/basic copy/07/merged.json +39 -0
  51. package/test-fixtures/reducers/copy/basic copy/07/metadata.json +5 -0
  52. package/test-fixtures/reducers/copy/basic copy/07/source.json +24 -0
  53. package/test-fixtures/reducers/copy/basic copy/08/base.json +28 -0
  54. package/test-fixtures/reducers/copy/basic copy/08/merged.json +28 -0
  55. package/test-fixtures/reducers/copy/basic copy/08/metadata.json +4 -0
  56. package/test-fixtures/reducers/copy/basic copy/08/source.json +24 -0
  57. package/test-fixtures/reducers/copy/compareTagsOnly/01/base.json +24 -0
  58. package/test-fixtures/reducers/copy/compareTagsOnly/01/merged.json +24 -0
  59. package/test-fixtures/reducers/copy/compareTagsOnly/01/metadata.json +5 -0
  60. package/test-fixtures/reducers/copy/compareTagsOnly/01/source.json +24 -0
  61. package/test-fixtures/reducers/copy/compareTagsOnly/02/base.json +24 -0
  62. package/test-fixtures/reducers/copy/compareTagsOnly/02/merged.json +100 -0
  63. package/test-fixtures/reducers/copy/compareTagsOnly/02/metadata.json +5 -0
  64. package/test-fixtures/reducers/copy/compareTagsOnly/02/source.json +100 -0
  65. package/test-fixtures/reducers/copy/compareWithoutIndicators/01/base.json +24 -0
  66. package/test-fixtures/reducers/copy/compareWithoutIndicators/01/merged.json +24 -0
  67. package/test-fixtures/reducers/copy/compareWithoutIndicators/01/metadata.json +5 -0
  68. package/test-fixtures/reducers/copy/compareWithoutIndicators/01/source.json +24 -0
  69. package/test-fixtures/reducers/copy/compareWithoutIndicators/02/base.json +24 -0
  70. package/test-fixtures/reducers/copy/compareWithoutIndicators/02/merged.json +39 -0
  71. package/test-fixtures/reducers/copy/compareWithoutIndicators/02/metadata.json +5 -0
  72. package/test-fixtures/reducers/copy/compareWithoutIndicators/02/source.json +24 -0
  73. package/test-fixtures/reducers/copy/copyUnless/01/base.json +9 -0
  74. package/test-fixtures/reducers/copy/copyUnless/01/merged.json +24 -0
  75. package/test-fixtures/reducers/copy/copyUnless/01/metadata.json +5 -0
  76. package/test-fixtures/reducers/copy/copyUnless/01/source.json +47 -0
  77. package/test-fixtures/reducers/copy/dropSubfields/01/base.json +35 -0
  78. package/test-fixtures/reducers/copy/dropSubfields/01/merged.json +54 -0
  79. package/test-fixtures/reducers/copy/dropSubfields/01/metadata.json +5 -0
  80. package/test-fixtures/reducers/copy/dropSubfields/01/source.json +43 -0
  81. package/test-fixtures/reducers/copy/dropSubfields/02/base.json +43 -0
  82. package/test-fixtures/reducers/copy/dropSubfields/02/merged.json +58 -0
  83. package/test-fixtures/reducers/copy/dropSubfields/02/metadata.json +5 -0
  84. package/test-fixtures/reducers/copy/dropSubfields/02/source.json +32 -0
  85. package/test-fixtures/reducers/copy/dropSubfields/03/base.json +43 -0
  86. package/test-fixtures/reducers/copy/dropSubfields/03/merged.json +58 -0
  87. package/test-fixtures/reducers/copy/dropSubfields/03/metadata.json +5 -0
  88. package/test-fixtures/reducers/copy/dropSubfields/03/source.json +32 -0
  89. package/test-fixtures/reducers/copy/dropSubfields/04/base.json +43 -0
  90. package/test-fixtures/reducers/copy/dropSubfields/04/merged.json +43 -0
  91. package/test-fixtures/reducers/copy/dropSubfields/04/metadata.json +5 -0
  92. package/test-fixtures/reducers/copy/dropSubfields/04/source.json +32 -0
  93. package/test-fixtures/reducers/copy/excludeSubfields/01/base.json +39 -0
  94. package/test-fixtures/reducers/copy/excludeSubfields/01/merged.json +39 -0
  95. package/test-fixtures/reducers/copy/excludeSubfields/01/metadata.json +8 -0
  96. package/test-fixtures/reducers/copy/{05 → excludeSubfields/01}/source.json +1 -1
  97. package/test-fixtures/reducers/copy/excludeSubfields/02/base.json +39 -0
  98. package/test-fixtures/reducers/copy/{05 → excludeSubfields/02}/merged.json +5 -5
  99. package/test-fixtures/reducers/copy/excludeSubfields/02/metadata.json +5 -0
  100. package/test-fixtures/reducers/copy/excludeSubfields/02/source.json +43 -0
  101. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/01/base.json +24 -0
  102. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/01/merged.json +39 -0
  103. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/01/metadata.json +5 -0
  104. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/01/source.json +24 -0
  105. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/02/base.json +24 -0
  106. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/02/merged.json +24 -0
  107. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/02/metadata.json +5 -0
  108. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/02/source.json +24 -0
  109. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/03/base.json +28 -0
  110. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/03/merged.json +43 -0
  111. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/03/metadata.json +5 -0
  112. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/03/source.json +24 -0
  113. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/04/base.json +28 -0
  114. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/04/merged.json +28 -0
  115. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/04/metadata.json +5 -0
  116. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/04/source.json +24 -0
  117. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/05/base.json +20 -0
  118. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/05/merged.json +35 -0
  119. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/05/metadata.json +5 -0
  120. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/05/source.json +24 -0
  121. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/06/base.json +20 -0
  122. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/06/merged.json +35 -0
  123. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/06/metadata.json +5 -0
  124. package/test-fixtures/reducers/copy/subfieldsMustBeIdentical/06/source.json +24 -0
  125. package/test-fixtures/reducers/metadata.json +4 -0
  126. package/test-fixtures/reducers/select/01/metadata.json +5 -0
  127. package/test-fixtures/reducers/select/02/metadata.json +4 -0
  128. package/test-fixtures/reducers/select/03/metadata.json +4 -0
  129. package/test-fixtures/reducers/select/04/metadata.json +5 -0
  130. package/test-fixtures/reducers/select/05/metadata.json +4 -0
  131. package/test-fixtures/reducers/select/06/base.json +4 -0
  132. package/test-fixtures/reducers/select/06/merged.json +4 -0
  133. package/test-fixtures/reducers/select/06/metadata.json +4 -0
  134. package/test-fixtures/reducers/select/06/source.json +4 -0
  135. package/test-fixtures/reducers/select/07/metadata.json +5 -0
  136. package/test-fixtures/reducers/select/08/metadata.json +4 -0
  137. package/test-fixtures/reducers/select/09/metadata.json +4 -0
  138. package/test-fixtures/reducers/select/10/metadata.json +5 -0
  139. package/test-fixtures/reducers/select/11/metadata.json +4 -0
  140. package/test-fixtures/reducers/select/12/metadata.json +5 -0
  141. package/test-fixtures/reducers/select/13/metadata.json +4 -0
  142. package/test-fixtures/reducers/select/14/base.json +24 -0
  143. package/test-fixtures/reducers/select/14/merged.json +24 -0
  144. package/test-fixtures/reducers/select/14/metadata.json +4 -0
  145. package/test-fixtures/reducers/select/14/source.json +24 -0
  146. package/.drone.yml +0 -92
  147. package/.nyc_output/7ecda3cd-9f84-4416-a466-fa1e5889c6a3.json +0 -1
  148. package/.nyc_output/processinfo/7ecda3cd-9f84-4416-a466-fa1e5889c6a3.json +0 -1
  149. package/.nyc_output/processinfo/index.json +0 -1
  150. package/LICENSE.txt +0 -165
  151. package/coverage/base.css +0 -224
  152. package/coverage/block-navigation.js +0 -79
  153. package/coverage/copy.js.html +0 -344
  154. package/coverage/favicon.png +0 -0
  155. package/coverage/index.html +0 -126
  156. package/coverage/lcov-report/base.css +0 -224
  157. package/coverage/lcov-report/block-navigation.js +0 -79
  158. package/coverage/lcov-report/copy.js.html +0 -344
  159. package/coverage/lcov-report/favicon.png +0 -0
  160. package/coverage/lcov-report/index.html +0 -126
  161. package/coverage/lcov-report/prettify.css +0 -1
  162. package/coverage/lcov-report/prettify.js +0 -2
  163. package/coverage/lcov-report/select.js.html +0 -431
  164. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  165. package/coverage/lcov-report/sorter.js +0 -170
  166. package/coverage/lcov.info +0 -195
  167. package/coverage/prettify.css +0 -1
  168. package/coverage/prettify.js +0 -2
  169. package/coverage/select.js.html +0 -431
  170. package/coverage/sort-arrow-sprite.png +0 -0
  171. package/coverage/sorter.js +0 -170
  172. package/test-fixtures/reducers/copy/01/pattern.txt +0 -1
  173. package/test-fixtures/reducers/copy/02/pattern.txt +0 -1
  174. package/test-fixtures/reducers/copy/03/pattern.txt +0 -1
  175. package/test-fixtures/reducers/copy/04/pattern.txt +0 -1
  176. package/test-fixtures/reducers/copy/05/pattern.txt +0 -1
  177. package/test-fixtures/reducers/select/01/expected-error.txt +0 -1
  178. package/test-fixtures/reducers/select/01/pattern.txt +0 -1
  179. package/test-fixtures/reducers/select/02/pattern.txt +0 -1
  180. package/test-fixtures/reducers/select/03/pattern.txt +0 -1
  181. package/test-fixtures/reducers/select/04/pattern.txt +0 -1
  182. package/test-fixtures/reducers/select/05/pattern.txt +0 -1
  183. package/test-fixtures/reducers/select/06/pattern.txt +0 -1
  184. package/test-fixtures/reducers/select/07/equalityFunction.txt +0 -1
  185. package/test-fixtures/reducers/select/07/pattern.txt +0 -1
  186. package/test-fixtures/reducers/select/08/pattern.txt +0 -1
  187. package/test-fixtures/reducers/select/09/pattern.txt +0 -1
  188. package/test-fixtures/reducers/select/10/equalityFunction.txt +0 -1
  189. package/test-fixtures/reducers/select/10/pattern.txt +0 -1
  190. package/test-fixtures/reducers/select/11/pattern.txt +0 -1
  191. package/test-fixtures/reducers/select/12/equalityFunction.txt +0 -1
  192. package/test-fixtures/reducers/select/12/pattern.txt +0 -1
  193. package/test-fixtures/reducers/select/13/pattern.txt +0 -1
@@ -1,88 +1,283 @@
1
- /**
2
- *
3
- * @licstart The following is the entire license notice for the JavaScript code in this file.
4
- *
5
- * Merge MARC records
6
- *
7
- * Copyright (C) 2015-2019 University Of Helsinki (The National Library Of Finland)
8
- *
9
- * This file is part of marc-record-merge-js
10
-
11
- * marc-record-merge-js program is free software: you can redistribute it and/or modify
12
- * it under the terms of the GNU Lesser General Public License as
13
- * published by the Free Software Foundation, either version 3 of the
14
- * License, or (at your option) any later version.
15
- *
16
- * marc-record-merge-js is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU Lesser General Public License for more details.
20
- *
21
- * You should have received a copy of the GNU Lesser General Public License
22
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
- *
24
- * @licend The above is the entire license notice
25
- * for the JavaScript code in this file.
26
- *
27
- */
28
- // Import createDebugLogger from 'debug';
29
-
30
- export default (pattern) => (base, source) => {
31
- // Const debug = createDebugLogger('@natlibfi/marc-record-merge');
32
- const baseFields = base.get(pattern);
33
- const sourceFields = source.get(pattern);
34
- return copyFields();
35
-
36
- function copyFields() {
37
- // Test 01: If base does not contain the field at all, it is copied from source to base
38
- if (baseFields.length === 0) {
39
- sourceFields.forEach(f => base.insertField(f));
40
- return base;
1
+ /* eslint-disable max-statements */
2
+ /* eslint-disable no-unused-vars */
3
+
4
+ import {MarcRecord} from '@natlibfi/marc-record';
5
+ import createDebugLogger from 'debug';
6
+
7
+ export default ({
8
+ tagPattern,
9
+ compareTagsOnly = false,
10
+ compareWithoutIndicators = false,
11
+ subfieldsMustBeIdentical = true,
12
+ excludeSubfields = [],
13
+ dropSubfields = [],
14
+ copyUnless = [],
15
+ baseValidators = {subfieldValues: false},
16
+ sourceValidators = {subfieldValues: false}
17
+ }) => (base, source) => {
18
+ const baseRecord = new MarcRecord(base, baseValidators);
19
+ const sourceRecord = new MarcRecord(source, sourceValidators);
20
+
21
+ const debug = createDebugLogger('@natlibfi/marc-record-merge');
22
+ const debugOptions = createDebugLogger('@natlibfi/marc-record-merge:compare-options');
23
+ const debugCompare = createDebugLogger('@natlibfi/marc-record-merge:compare');
24
+ debugOptions(`Tag Pattern: ${tagPattern}`);
25
+ debugOptions(`Compare tags only: ${compareTagsOnly}`);
26
+ debugOptions(`Compare without indicators ${compareWithoutIndicators}`);
27
+ debugOptions(`Copy if identical: ${subfieldsMustBeIdentical}`);
28
+ debugOptions(`Exclude subfields: [${excludeSubfields}]`);
29
+ debugOptions(`Drop subfields [${dropSubfields}]`);
30
+ debugOptions(`Copy unless contains subfields: ${JSON.stringify(copyUnless)}`);
31
+
32
+ const baseFields = baseRecord.get(tagPattern);
33
+ const sourceFields = sourceRecord.get(tagPattern);
34
+
35
+ debug(`Base fields: `, baseFields);
36
+ debug(`Source fields: `, sourceFields);
37
+
38
+ const baseCompareFields = baseFields.map(baseField => createCompareField(baseField));
39
+ const compareResultFields = compareFields(sourceFields, baseCompareFields);
40
+ const droppedUnwantedSubfield = checkDropSubfields(compareResultFields);
41
+ const droppedUnwantedFields = checkCopyUnlessFields(droppedUnwantedSubfield);
42
+ debug('Fields to be copied');
43
+ debug(JSON.stringify(droppedUnwantedFields));
44
+
45
+ // Add fields to base;
46
+ droppedUnwantedFields.forEach(field => baseRecord.insertField(field));
47
+ return baseRecord.toObject();
48
+ //return copyFields(baseFields, sourceFields);
49
+
50
+ function compareFields(sourceFields, baseCompareFields, uniqFields = []) {
51
+ const [sourceField, ...rest] = sourceFields;
52
+ if (sourceField === undefined) {
53
+ return uniqFields;
41
54
  }
42
- const filterMissing = function(sourceField) {
43
- // Test 02: Identical control fields are not copied
44
- if ('value' in sourceField) {
45
- return baseFields.some(isIdenticalControlField) === false;
46
- }
47
- // Test 04: Identical data fields in base and source, not copied
48
- // Test 05: Different data fields are copied from source to base
49
- if ('subfields' in sourceField) {
50
- return baseFields.some(isIdenticalDataField) === false;
51
- }
52
55
 
53
- function normalizeControlField(field) {
54
- return field.value.toLowerCase().replace(/\s+/u, '');
55
- }
56
+ if (baseCompareFields.length === 0) {
57
+ return compareFields(rest, baseCompareFields, [...uniqFields, sourceField]);
58
+ }
56
59
 
57
- function isIdenticalControlField(baseField) {
58
- const normalizedBaseField = normalizeControlField(baseField);
59
- const normalizedSourceField = normalizeControlField(sourceField);
60
- return normalizedSourceField === normalizedBaseField;
61
- }
62
- function isIdenticalDataField(baseField) {
63
- if (sourceField.tag === baseField.tag &&
64
- sourceField.ind1 === baseField.ind1 &&
65
- sourceField.ind2 === baseField.ind2 &&
66
- sourceField.subfields.length === baseField.subfields.length) {
67
- return baseField.subfields.every(isIdenticalSubfield);
60
+ // Source and base are also compared for identicalness
61
+ // Non-identical fields are copied from source to base as duplicates
62
+ const sourceCompareField = createCompareField(sourceField);
63
+ const unique = checkCompareFields(baseCompareFields, sourceCompareField);
64
+
65
+ debugCompare(`${JSON.stringify(sourceField)} ${unique ? 'is UNIQUE' : 'not UNIQUE'}`);
66
+
67
+ if (unique) {
68
+ return compareFields(rest, baseCompareFields, [...uniqFields, sourceField]);
69
+ }
70
+
71
+ return compareFields(rest, baseCompareFields, uniqFields);
72
+
73
+ function checkCompareFields(baseCompareFields, sourceCompareField) {
74
+ let unique = true; // eslint-disable-line functional/no-let
75
+
76
+ baseCompareFields.forEach(baseCompareField => {
77
+ debugCompare(`Comparing ${JSON.stringify(sourceCompareField)} to ${JSON.stringify(baseCompareField)}}`);
78
+
79
+ if (sourceCompareField.value !== baseCompareField.value) {
80
+ debugCompare(`Value is different ${sourceCompareField.value} !== ${baseCompareField.value}`);
81
+ return;
82
+ }
83
+
84
+ if (sourceCompareField.ind1 !== baseCompareField.ind1) {
85
+ debugCompare(`Ind1 is different ${sourceCompareField.ind1} !== ${baseCompareField.ind1}`);
86
+ return;
68
87
  }
69
- function normalizeSubfield(subfield) {
70
- return subfield.value.toLowerCase().replace(/\s+/u, '');
88
+
89
+ if (sourceCompareField.ind2 !== baseCompareField.ind2) {
90
+ debugCompare(`Ind2 is different ${sourceCompareField.ind2} !== ${baseCompareField.ind2}`);
91
+ return;
71
92
  }
72
- function isIdenticalSubfield(baseSub) {
73
- const normBaseSub = normalizeSubfield(baseSub);
74
- return sourceField.subfields.some(sourceSub => {
75
- const normSourceSub = normalizeSubfield(sourceSub);
76
- return normSourceSub === normBaseSub;
77
- });
93
+
94
+ if ('subfields' in sourceCompareField) {
95
+ const allFound = checkSubfields(sourceCompareField.subfields, baseCompareField.subfields);
96
+ debugCompare(`Subfields are different ${!allFound}`);
97
+ if (!allFound) {
98
+ return;
99
+ }
100
+
101
+ unique = false;
102
+ return;
78
103
  }
104
+
105
+ unique = false;
106
+ return;
107
+ });
108
+
109
+ return unique;
110
+ }
111
+
112
+ function checkSubfields(sourceSubfields, baseSubfields) {
113
+ const foundSubs = sourceSubfields.filter(sSub => baseSubfields.some(bSub => sSub.code === bSub.code && sSub.value === bSub.value));
114
+
115
+ if (subfieldsMustBeIdentical) {
116
+ return foundSubs.length === sourceSubfields.length && foundSubs.length === baseSubfields.length;
79
117
  }
80
- };
81
- // Search for fields missing from base
82
- const missingFields = sourceFields.filter(filterMissing);
83
- // Test 03: Add missing control field to base
84
- // Test 05: Add missing data field to base
85
- missingFields.forEach(f => base.insertField(f));
86
- return base; // This is returned by copyFields
118
+
119
+ return foundSubs.length === sourceSubfields.length;
120
+ }
121
+ }
122
+
123
+ function createCompareField(field) {
124
+ if (compareTagsOnly) {
125
+ return {tag: field.tag};
126
+ }
127
+
128
+ if ('value' in field) {
129
+ return {tag: field.tag, value: field.value};
130
+ }
131
+
132
+ const [filteredField] = checkDropSubfields([field]);
133
+
134
+ const params = [
135
+ {name: 'tag', value: field.tag},
136
+ {name: 'ind1', value: compareWithoutIndicators ? undefined : field.ind1},
137
+ {name: 'ind2', value: compareWithoutIndicators ? undefined : field.ind2},
138
+ {name: 'subfields', value: createCompareSubfields(filteredField.subfields)}
139
+ ].map(param => [param.name, param.value]);
140
+
141
+ return Object.fromEntries(params);
142
+
143
+ function createCompareSubfields(subfields) {
144
+ const nonExcludedSubfields = subfields.filter(sub => !excludeSubfields.some(code => code === sub.code));
145
+ const normalizedSubfields = nonExcludedSubfields.map(sub => ({code: sub.code, value: normalizeSubfieldValue(sub.value)}));
146
+
147
+ return normalizedSubfields;
148
+
149
+ function normalizeSubfieldValue(value) {
150
+ return value.toLowerCase().replace(/\s+/ug, '');
151
+ }
152
+ }
153
+ }
154
+
155
+ function checkDropSubfields(fields) {
156
+ if (dropSubfields.length > 0) {
157
+ return fields.map(field => ({...field, subfields: dropSubfieldsFunc(field.subfields)}))
158
+ .filter(field => field.subfields.length > 0);
159
+ }
160
+
161
+ return fields;
162
+
163
+ function dropSubfieldsFunc(subfields) {
164
+ return subfields.filter(sub => { // eslint-disable-line
165
+ return !dropSubfields.some(({code, value = false, condition = false}) => {
166
+ if (code !== sub.code) {
167
+ return false;
168
+ }
169
+
170
+ if (!condition && value) {
171
+ return value === sub.value;
172
+ }
173
+
174
+ if (condition === 'unless' && value) {
175
+ return !new RegExp(value, 'u').test(sub.value);
176
+ }
177
+
178
+ return true;
179
+ });
180
+ });
181
+ }
182
+ }
183
+
184
+ function checkCopyUnlessFields(fields) {
185
+ if (copyUnless.length > 0) {
186
+ return fields.filter(({subfields}) => copyUnless.some(filter => !subfields.some(sub => sub.code === filter.code && new RegExp(filter.value, 'u').test(sub.value))));
187
+ }
188
+
189
+ return fields;
87
190
  }
88
191
  };
192
+
193
+ // function copyFields() { //eslint-disable-line no-unused-vars
194
+ // const sourceTags = sourceFields.map(field => field.tag);
195
+ // sourceTags.forEach(tag => debug(`Comparing field ${tag}`));
196
+
197
+ // /*
198
+ // if (combine.length > 0) {
199
+ // debug(`*** NOW Copy options: ${tagPattern}, ${compareTagsOnly}, ${compareWithoutIndicators}, ${subfieldsMustBeIdentical}, [${combine}], [${excludeSubfields}], [${dropSubfields}]`);
200
+ // combine.forEach(row => debug(` ### combine ${row} <- `));
201
+ // return [];
202
+ // }
203
+ // */
204
+
205
+ // // If compareTagsOnly = true, only this part is run
206
+ // // The field is copied from source only if it is missing completely from base
207
+ // if (compareTagsOnly && baseFields.length === 0) {
208
+ // sourceTags.forEach(tag => debug(`Missing field ${tag} copied from source to base`));
209
+ // sourceFields.forEach(f => base.insertField(f));
210
+ // return true;
211
+ // }
212
+
213
+ // // If compareTagsOnly = false (default)
214
+ // // Source and base are also compared for identicalness
215
+ // // Non-identical fields are copied from source to base as duplicates
216
+ // if (!compareTagsOnly) {
217
+ // const filterMissing = function (sourceField) {
218
+ // if ('value' in sourceField) {
219
+ // debug(`Checking control field ${sourceField.tag} for identicalness`);
220
+ // return baseFields.some(isIdenticalControlField) === false;
221
+ // }
222
+ // if ('subfields' in sourceField) {
223
+ // debug(`Checking data field ${sourceField.tag} for identicalness`);
224
+ // return baseFields.some(isIdenticalDataField) === false;
225
+ // }
226
+
227
+ // function normalizeControlField(field) {
228
+ // return field.value.toLowerCase().replace(/\s+/u, '');
229
+ // }
230
+
231
+ // function isIdenticalControlField(baseField) {
232
+ // const normalizedBaseField = normalizeControlField(baseField);
233
+ // const normalizedSourceField = normalizeControlField(sourceField);
234
+ // return normalizedSourceField === normalizedBaseField;
235
+ // }
236
+
237
+ // function isIdenticalDataField(baseField) {
238
+ // // If excluded subfields have been defined for this field, they must be ignored first
239
+ // // (i.e. source and base fields are considered identical if all non-excluded subfields are identical)
240
+ // if (excludeSubfields.length > 0 &&
241
+ // sourceField.tag === baseField.tag &&
242
+ // sourceField.ind1 === baseField.ind1 &&
243
+ // sourceField.ind2 === baseField.ind2) {
244
+ // excludeSubfields.forEach(sub => debug(`Subfield ${sub} excluded from identicalness comparison`));
245
+ // // Compare only those subfields that are not excluded
246
+ // const baseSubsToCompare = baseField.subfields.filter(subfield => excludeSubfields.indexOf(subfield.code) === -1);
247
+ // return baseSubsToCompare.every(isIdenticalSubfield);
248
+ // }
249
+ // // If there are no excluded subfields (default case)
250
+ // if (sourceField.tag === baseField.tag &&
251
+ // sourceField.ind1 === baseField.ind1 &&
252
+ // sourceField.ind2 === baseField.ind2 &&
253
+ // sourceField.subfields.length === baseField.subfields.length) {
254
+ // return baseField.subfields.every(isIdenticalSubfield);
255
+ // }
256
+ // function normalizeSubfield(subfield) {
257
+ // return subfield.value.toLowerCase().replace(/\s+/u, '');
258
+ // }
259
+ // function isIdenticalSubfield(baseSub) {
260
+ // const normBaseSub = normalizeSubfield(baseSub);
261
+ // return sourceField.subfields.some(sourceSub => {
262
+ // const normSourceSub = normalizeSubfield(sourceSub);
263
+ // return normSourceSub === normBaseSub;
264
+ // });
265
+ // }
266
+ // }
267
+ // };
268
+ // // Search for fields missing from base
269
+ // const missingFields = sourceFields.filter(filterMissing);
270
+ // missingFields.forEach(f => base.insertField(f));
271
+ // if (missingFields.length > 0) {
272
+ // const missingTags = missingFields.map(field => field.tag);
273
+ // missingTags.forEach(tag => debug(`Field ${tag} copied from source to base`));
274
+ // return base;
275
+ // }
276
+ // if (missingFields.length === 0) {
277
+ // debug(`No missing fields found`);
278
+ // return base;
279
+ // }
280
+ // }
281
+ // debug(`No missing fields found`);
282
+ // return base;
283
+ // }
@@ -1,52 +1,48 @@
1
- /**
2
- *
3
- * @licstart The following is the entire license notice for the JavaScript code in this file.
4
- *
5
- * Merge MARC records
6
- *
7
- * Copyright (C) 2015-2019 University Of Helsinki (The National Library Of Finland)
8
- *
9
- * This file is part of marc-record-merge-js
10
-
11
- * marc-record-merge-js program is free software: you can redistribute it and/or modify
12
- * it under the terms of the GNU Lesser General Public License as
13
- * published by the Free Software Foundation, either version 3 of the
14
- * License, or (at your option) any later version.
15
- *
16
- * marc-record-merge-js is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU Lesser General Public License for more details.
20
- *
21
- * You should have received a copy of the GNU Lesser General Public License
22
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
- *
24
- * @licend The above is the entire license notice
25
- * for the JavaScript code in this file.
26
- *
27
- */
28
- import chai from 'chai';
29
- import fs from 'fs';
30
- import path from 'path';
1
+ import {expect} from 'chai';
2
+ import {READERS} from '@natlibfi/fixura';
31
3
  import {MarcRecord} from '@natlibfi/marc-record';
32
4
  import createReducer from './copy';
33
- import fixturesFactory, {READERS} from '@natlibfi/fixura';
34
-
35
- MarcRecord.setValidationOptions({subfieldValues: false});
5
+ import generateTests from '@natlibfi/fixugen';
36
6
 
37
- describe('reducers/copy', () => {
38
- const {expect} = chai;
39
- const fixturesPath = path.join(__dirname, '..', '..', 'test-fixtures', 'reducers', 'copy');
7
+ //import createDebugLogger from 'debug'; // <---
8
+ //const debug = createDebugLogger('@natlibfi/marc-record-merge/copy.spec.js'); // <---
40
9
 
41
- fs.readdirSync(fixturesPath).forEach(subDir => {
42
- const {getFixture} = fixturesFactory({root: [fixturesPath, subDir], reader: READERS.JSON});
43
- it(subDir, () => {
44
- const baseTest = new MarcRecord(getFixture('base.json'));
45
- const sourceTest = new MarcRecord(getFixture('source.json'));
46
- const patternTest = new RegExp(getFixture({components: ['pattern.txt'], reader: READERS.TEXT}), 'u');
47
- const expectedRecord = getFixture('merged.json');
48
- const mergedRecord = createReducer(patternTest)(baseTest, sourceTest);
49
- expect(mergedRecord.toObject()).to.eql(expectedRecord);
50
- });
51
- });
10
+ generateTests({
11
+ callback,
12
+ path: [__dirname, '..', '..', 'test-fixtures', 'reducers', 'copy'],
13
+ useMetadataFile: true,
14
+ recurse: true,
15
+ fixura: {
16
+ reader: READERS.JSON,
17
+ failWhenNotFound: false
18
+ }
52
19
  });
20
+
21
+ function callback({
22
+ getFixture,
23
+ tagPatternRegExp,
24
+ compareTagsOnly = false,
25
+ compareWithoutIndicators = false,
26
+ subfieldsMustBeIdentical = false,
27
+ copyUnless = undefined,
28
+ excludeSubfields = undefined,
29
+ dropSubfields = undefined,
30
+ disabled = false
31
+ }) {
32
+ if (disabled) {
33
+ console.log('TEST DISABLED!'); // eslint-disable-line no-console
34
+ return;
35
+ }
36
+
37
+ const base = new MarcRecord(getFixture('base.json'), {subfieldValues: false});
38
+ const source = new MarcRecord(getFixture('source.json'), {subfieldValues: false});
39
+ const tagPattern = new RegExp(tagPatternRegExp, 'u');
40
+ const expectedRecord = getFixture('merged.json');
41
+
42
+ const mergedRecord = createReducer({tagPattern, compareTagsOnly, compareWithoutIndicators, copyUnless, subfieldsMustBeIdentical, excludeSubfields, dropSubfields})(base, source);
43
+ //debug(`*** mergedRecord: `, mergedRecord); //<--
44
+ //debug(`*** mergedRecord,Strfy: `, JSON.stringify(mergedRecord)); //<--
45
+ //debug(`*** expectedRecord: `, expectedRecord); //<--
46
+ //debug(`*** expectedRecord,Strfy: `, JSON.stringify(expectedRecord)); //<--
47
+ expect(mergedRecord).to.eql(expectedRecord);
48
+ }
@@ -1,31 +1,4 @@
1
- /**
2
- *
3
- * @licstart The following is the entire license notice for the JavaScript code in this file.
4
- *
5
- * Merge MARC records
6
- *
7
- * Copyright (C) 2015-2019 University Of Helsinki (The National Library Of Finland)
8
- *
9
- * This file is part of marc-record-merge-js
10
-
11
- * marc-record-merge-js program is free software: you can redistribute it and/or modify
12
- * it under the terms of the GNU Lesser General Public License as
13
- * published by the Free Software Foundation, either version 3 of the
14
- * License, or (at your option) any later version.
15
- *
16
- * marc-record-merge-js is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU Lesser General Public License for more details.
20
- *
21
- * You should have received a copy of the GNU Lesser General Public License
22
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
- *
24
- * @licend The above is the entire license notice
25
- * for the JavaScript code in this file.
26
- *
27
- */
28
-
29
- export * from './copy';
30
- export * from './select';
1
+ import copy from './copy';
2
+ import select, {strictEquality, subsetEquality} from './select';
31
3
 
4
+ export default {copy, select, strictEquality, subsetEquality};
@@ -1,31 +1,5 @@
1
- /**
2
- *
3
- * @licstart The following is the entire license notice for the JavaScript code in this file.
4
- *
5
- * Merge MARC records
6
- *
7
- * Copyright (C) 2015-2019 University Of Helsinki (The National Library Of Finland)
8
- *
9
- * This file is part of marc-record-merge-js
10
-
11
- * marc-record-merge-js program is free software: you can redistribute it and/or modify
12
- * it under the terms of the GNU Lesser General Public License as
13
- * published by the Free Software Foundation, either version 3 of the
14
- * License, or (at your option) any later version.
15
- *
16
- * marc-record-merge-js is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- * GNU Lesser General Public License for more details.
20
- *
21
- * You should have received a copy of the GNU Lesser General Public License
22
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
- *
24
- * @licend The above is the entire license notice
25
- * for the JavaScript code in this file.
26
- *
27
- */
28
1
  import {normalizeSync} from 'normalize-diacritics';
2
+ import createDebugLogger from 'debug';
29
3
 
30
4
  export function strictEquality(subfieldA, subfieldB) {
31
5
  return subfieldA.code === subfieldB.code &&
@@ -36,21 +10,28 @@ export function subsetEquality(subfieldA, subfieldB) {
36
10
  return subfieldA.code === subfieldB.code &&
37
11
  (subfieldA.value.indexOf(subfieldB.value) !== -1 || subfieldB.value.indexOf(subfieldA.value) !== -1);
38
12
  }
39
-
40
- export default ({pattern, equalityFunction = strictEquality}) => (base, source) => {
41
- const baseFields = base.get(pattern);
42
- const sourceFields = source.get(pattern);
13
+ // EqualityFunction can be either strictEquality or subsetEquality
14
+ export default ({tagPattern, equalityFunction = strictEquality}) => (base, source) => {
15
+ const debug = createDebugLogger('@natlibfi/marc-record-merge:select');
16
+ const baseFields = base.get(tagPattern);
17
+ const sourceFields = source.get(tagPattern);
18
+ const fieldTag = sourceFields.map(field => field.tag);
19
+ debug(`Comparing field ${fieldTag}`);
43
20
 
44
21
  checkFieldType(baseFields);
45
22
  checkFieldType(sourceFields);
46
23
 
47
24
  if (baseFields.length > 1 || sourceFields.length > 1) {
25
+ debug(`Multiple fields in base or source`);
26
+ debug(`No changes to base`);
48
27
  return base;
49
28
  }
50
29
  const [baseField] = baseFields;
51
30
  const [sourceField] = sourceFields;
52
31
 
53
32
  if (baseField.tag === sourceField.tag === false) {
33
+ debug(`Base tag ${baseField.tag} is not equal to source tag ${sourceField.tag}`);
34
+ debug(`No changes to base`);
54
35
  return base;
55
36
  }
56
37
  const baseSubs = baseField.subfields;
@@ -62,19 +43,26 @@ export default ({pattern, equalityFunction = strictEquality}) => (base, source)
62
43
  const sourceSubsNormalized = sourceSubs
63
44
  .map(({code, value}) => ({code, value: normalizeSubfieldValue(value)}));
64
45
 
46
+ // Returns the base subfields for which a matching source subfield is found
65
47
  const equalSubfieldsBase = baseSubsNormalized
66
48
  .filter(baseSubfield => sourceSubsNormalized
67
49
  .some(sourceSubfield => equalityFunction(baseSubfield, sourceSubfield)));
50
+ debug(`equalSubfieldsBase: ${JSON.stringify(equalSubfieldsBase, undefined, 2)}`);
68
51
 
52
+ // Returns the source subfields for which a matching base subfield is found
69
53
  const equalSubfieldsSource = sourceSubsNormalized
70
54
  .filter(sourceSubfield => baseSubsNormalized
71
55
  .some(baseSubfield => equalityFunction(sourceSubfield, baseSubfield)));
56
+ debug(`equalSubfieldsSource: ${JSON.stringify(equalSubfieldsSource, undefined, 2)}`);
72
57
 
73
58
  if (baseSubs.length === sourceSubs.length && equalSubfieldsBase.length < baseSubs.length) {
59
+ debug(`Base and source subfields are not equal`);
60
+ debug(`No changes to base`);
74
61
  return base;
75
62
  }
76
63
 
77
64
  if (baseSubs.length === sourceSubs.length && equalSubfieldsBase.length === equalSubfieldsSource.length) {
65
+ debug(`Checking subfield equality`);
78
66
  const totalSubfieldLengthBase = baseSubsNormalized
79
67
  .map(({value}) => value.length)
80
68
  .reduce((acc, value) => acc + value);
@@ -91,11 +79,13 @@ export default ({pattern, equalityFunction = strictEquality}) => (base, source)
91
79
  return replaceBasefieldWithSourcefield(base);
92
80
  }
93
81
 
82
+ debug(`No changes to base`);
94
83
  return base;
95
84
 
96
85
  function replaceBasefieldWithSourcefield(base) {
97
86
  const index = base.fields.findIndex(field => field === baseField);
98
87
  base.fields.splice(index, 1, sourceField); // eslint-disable-line functional/immutable-data
88
+ debug(`Source field is longer, replacing base with source`);
99
89
  return base;
100
90
  }
101
91
 
@@ -111,7 +101,7 @@ export default ({pattern, equalityFunction = strictEquality}) => (base, source)
111
101
 
112
102
  function normalizeSubfieldValue(value) {
113
103
  // Regexp options: g: global search, u: unicode
114
- const punctuation = /[.,\-/#!$%^&*;:{}=_`~()[\]]/gu;
104
+ const punctuation = /[.,\-/#!?$%^&*;:{}=_`~()[\]]/gu;
115
105
  return normalizeSync(value).toLowerCase().replace(punctuation, '', 'u').replace(/\s+/gu, ' ').trim();
116
106
  }
117
107
  };