@natlibfi/marc-record-merge 7.0.10-alpha.1 → 8.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,59 +1,34 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.strictEquality = strictEquality;
8
- exports.subsetEquality = subsetEquality;
9
- var _normalizeDiacritics = require("normalize-diacritics");
10
- var _debug = _interopRequireDefault(require("debug"));
11
- var _marcRecord = require("@natlibfi/marc-record");
12
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
- function strictEquality(subfieldA, subfieldB) {
1
+ import { normalizeSync } from "normalize-diacritics";
2
+ import createDebugLogger from "debug";
3
+ import { MarcRecord } from "@natlibfi/marc-record";
4
+ export function strictEquality(subfieldA, subfieldB) {
14
5
  return subfieldA.code === subfieldB.code && subfieldA.value === subfieldB.value;
15
6
  }
16
- function subsetEquality(subfieldA, subfieldB) {
7
+ export function subsetEquality(subfieldA, subfieldB) {
17
8
  return subfieldA.code === subfieldB.code && (subfieldA.value.indexOf(subfieldB.value) !== -1 || subfieldB.value.indexOf(subfieldA.value) !== -1);
18
9
  }
19
- // EqualityFunction can be either strictEquality or subsetEquality
20
- var _default = ({
10
+ export default ({
21
11
  tagPattern,
22
12
  equalityFunction = strictEquality,
23
- baseValidators = {
24
- subfieldValues: false
25
- },
26
- sourceValidators = {
27
- subfieldValues: false
28
- }
13
+ baseValidators = { subfieldValues: false },
14
+ sourceValidators = { subfieldValues: false }
29
15
  // eslint-disable-next-line max-statements
30
16
  }) => (base, source) => {
31
- const debug = (0, _debug.default)('@natlibfi/marc-record-merge:select');
32
- const {
33
- baseRecord,
34
- sourceRecord
35
- } = getRecordsFromParameters(base, source, baseValidators, sourceValidators);
36
- function getRecordsFromParameters(base, source, baseValidators, sourceValidators) {
37
- // records if we got an object ({base, source}) as a parameter
38
- if (source === undefined && base.base !== undefined && base.source !== undefined) {
39
- const baseRecord = new _marcRecord.MarcRecord(base.base, baseValidators);
40
- const sourceRecord = new _marcRecord.MarcRecord(base.source, sourceValidators);
41
- return {
42
- baseRecord,
43
- sourceRecord
44
- };
17
+ const debug = createDebugLogger("@natlibfi/marc-record-merge:select");
18
+ const { baseRecord, sourceRecord } = getRecordsFromParameters(base, source, baseValidators, sourceValidators);
19
+ function getRecordsFromParameters(base2, source2, baseValidators2, sourceValidators2) {
20
+ if (source2 === void 0 && base2.base !== void 0 && base2.source !== void 0) {
21
+ const baseRecord3 = new MarcRecord(base2.base, baseValidators2);
22
+ const sourceRecord3 = new MarcRecord(base2.source, sourceValidators2);
23
+ return { baseRecord: baseRecord3, sourceRecord: sourceRecord3 };
45
24
  }
46
- // records if we got an non-object (base, source) as a parameter
47
- const baseRecord = new _marcRecord.MarcRecord(base, baseValidators);
48
- const sourceRecord = new _marcRecord.MarcRecord(source, sourceValidators);
49
- return {
50
- baseRecord,
51
- sourceRecord
52
- };
25
+ const baseRecord2 = new MarcRecord(base2, baseValidators2);
26
+ const sourceRecord2 = new MarcRecord(source2, sourceValidators2);
27
+ return { baseRecord: baseRecord2, sourceRecord: sourceRecord2 };
53
28
  }
54
29
  const baseFields = baseRecord.get(tagPattern);
55
30
  const sourceFields = sourceRecord.get(tagPattern);
56
- const fieldTag = sourceFields.map(field => field.tag);
31
+ const fieldTag = sourceFields.map((field) => field.tag);
57
32
  debug(`Comparing field ${fieldTag}`);
58
33
  checkFieldType(baseFields);
59
34
  checkFieldType(sourceFields);
@@ -71,28 +46,12 @@ var _default = ({
71
46
  }
72
47
  const baseSubs = baseField.subfields;
73
48
  const sourceSubs = sourceField.subfields;
74
- const baseSubsNormalized = baseSubs.map(({
75
- code,
76
- value
77
- }) => ({
78
- code,
79
- value: normalizeSubfieldValue(value)
80
- }));
81
- const sourceSubsNormalized = sourceSubs.map(({
82
- code,
83
- value
84
- }) => ({
85
- code,
86
- value: normalizeSubfieldValue(value)
87
- }));
88
-
89
- // Returns the base subfields for which a matching source subfield is found
90
- const equalSubfieldsBase = baseSubsNormalized.filter(baseSubfield => sourceSubsNormalized.some(sourceSubfield => equalityFunction(baseSubfield, sourceSubfield)));
91
- debug(`equalSubfieldsBase: ${JSON.stringify(equalSubfieldsBase, undefined, 2)}`);
92
-
93
- // Returns the source subfields for which a matching base subfield is found
94
- const equalSubfieldsSource = sourceSubsNormalized.filter(sourceSubfield => baseSubsNormalized.some(baseSubfield => equalityFunction(sourceSubfield, baseSubfield)));
95
- debug(`equalSubfieldsSource: ${JSON.stringify(equalSubfieldsSource, undefined, 2)}`);
49
+ const baseSubsNormalized = baseSubs.map(({ code, value }) => ({ code, value: normalizeSubfieldValue(value) }));
50
+ const sourceSubsNormalized = sourceSubs.map(({ code, value }) => ({ code, value: normalizeSubfieldValue(value) }));
51
+ const equalSubfieldsBase = baseSubsNormalized.filter((baseSubfield) => sourceSubsNormalized.some((sourceSubfield) => equalityFunction(baseSubfield, sourceSubfield)));
52
+ debug(`equalSubfieldsBase: ${JSON.stringify(equalSubfieldsBase, void 0, 2)}`);
53
+ const equalSubfieldsSource = sourceSubsNormalized.filter((sourceSubfield) => baseSubsNormalized.some((baseSubfield) => equalityFunction(sourceSubfield, baseSubfield)));
54
+ debug(`equalSubfieldsSource: ${JSON.stringify(equalSubfieldsSource, void 0, 2)}`);
96
55
  if (baseSubs.length === sourceSubs.length && equalSubfieldsBase.length < baseSubs.length) {
97
56
  debug(`Base and source subfields are not equal`);
98
57
  debug(`No changes to base`);
@@ -100,12 +59,8 @@ var _default = ({
100
59
  }
101
60
  if (baseSubs.length === sourceSubs.length && equalSubfieldsBase.length === equalSubfieldsSource.length) {
102
61
  debug(`Checking subfield equality`);
103
- const totalSubfieldLengthBase = baseSubsNormalized.map(({
104
- value
105
- }) => value.length).reduce((acc, value) => acc + value);
106
- const totalSubfieldLengthSource = sourceSubsNormalized.map(({
107
- value
108
- }) => value.length).reduce((acc, value) => acc + value);
62
+ const totalSubfieldLengthBase = baseSubsNormalized.map(({ value }) => value.length).reduce((acc, value) => acc + value);
63
+ const totalSubfieldLengthSource = sourceSubsNormalized.map(({ value }) => value.length).reduce((acc, value) => acc + value);
109
64
  if (totalSubfieldLengthSource > totalSubfieldLengthBase) {
110
65
  return replaceBasefieldWithSourcefield(baseRecord.toObject());
111
66
  }
@@ -115,27 +70,24 @@ var _default = ({
115
70
  }
116
71
  debug(`No changes to base`);
117
72
  return baseRecord.toObject();
118
- function replaceBasefieldWithSourcefield(base) {
119
- const index = base.fields.findIndex(field => field === baseField);
120
- base.fields.splice(index, 1, sourceField); // eslint-disable-line functional/immutable-data
73
+ function replaceBasefieldWithSourcefield(base2) {
74
+ const index = base2.fields.findIndex((field) => field === baseField);
75
+ base2.fields.splice(index, 1, sourceField);
121
76
  debug(`Source field is longer, replacing base with source`);
122
- return base;
77
+ return base2;
123
78
  }
124
79
  function checkFieldType(fields) {
125
- const checkedFields = fields.map(field => {
126
- if ('value' in field) {
127
- // eslint-disable-line functional/no-conditional-statements
128
- throw new Error('Invalid control field, expected data field');
80
+ const checkedFields = fields.map((field) => {
81
+ if ("value" in field) {
82
+ throw new Error("Invalid control field, expected data field");
129
83
  }
130
84
  return field;
131
85
  });
132
86
  return checkedFields;
133
87
  }
134
88
  function normalizeSubfieldValue(value) {
135
- // Regexp options: g: global search, u: unicode
136
89
  const punctuation = /[.,\-/#!?$%^&*;:{}=_`~()[\]]/gu;
137
- return (0, _normalizeDiacritics.normalizeSync)(value).toLowerCase().replace(punctuation, '', 'u').replace(/\s+/gu, ' ').trim();
90
+ return normalizeSync(value).toLowerCase().replace(punctuation, "", "u").replace(/\s+/gu, " ").trim();
138
91
  }
139
92
  };
140
- exports.default = _default;
141
- //# sourceMappingURL=select.js.map
93
+ //# sourceMappingURL=select.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"select.js","names":["_normalizeDiacritics","require","_debug","_interopRequireDefault","_marcRecord","e","__esModule","default","strictEquality","subfieldA","subfieldB","code","value","subsetEquality","indexOf","_default","tagPattern","equalityFunction","baseValidators","subfieldValues","sourceValidators","base","source","debug","createDebugLogger","baseRecord","sourceRecord","getRecordsFromParameters","undefined","MarcRecord","baseFields","get","sourceFields","fieldTag","map","field","tag","checkFieldType","length","toObject","baseField","sourceField","baseSubs","subfields","sourceSubs","baseSubsNormalized","normalizeSubfieldValue","sourceSubsNormalized","equalSubfieldsBase","filter","baseSubfield","some","sourceSubfield","JSON","stringify","equalSubfieldsSource","totalSubfieldLengthBase","reduce","acc","totalSubfieldLengthSource","replaceBasefieldWithSourcefield","index","fields","findIndex","splice","checkedFields","Error","punctuation","normalizeSync","toLowerCase","replace","trim","exports"],"sources":["../../src/reducers/select.js"],"sourcesContent":["import {normalizeSync} from 'normalize-diacritics';\nimport createDebugLogger from 'debug';\nimport {MarcRecord} from '@natlibfi/marc-record';\n\nexport function strictEquality(subfieldA, subfieldB) {\n return subfieldA.code === subfieldB.code &&\n subfieldA.value === subfieldB.value;\n}\n\nexport function subsetEquality(subfieldA, subfieldB) {\n return subfieldA.code === subfieldB.code &&\n (subfieldA.value.indexOf(subfieldB.value) !== -1 || subfieldB.value.indexOf(subfieldA.value) !== -1);\n}\n// EqualityFunction can be either strictEquality or subsetEquality\n\nexport default ({\n tagPattern,\n equalityFunction = strictEquality,\n baseValidators = {subfieldValues: false},\n sourceValidators = {subfieldValues: false}\n// eslint-disable-next-line max-statements\n}) => (base, source) => {\n const debug = createDebugLogger('@natlibfi/marc-record-merge:select');\n\n const {baseRecord, sourceRecord} = getRecordsFromParameters(base, source, baseValidators, sourceValidators);\n\n function getRecordsFromParameters(base, source, baseValidators, sourceValidators) {\n // records if we got an object ({base, source}) as a parameter\n if (source === undefined && base.base !== undefined && base.source !== undefined) {\n const baseRecord = new MarcRecord(base.base, baseValidators);\n const sourceRecord = new MarcRecord(base.source, sourceValidators);\n return {baseRecord, sourceRecord};\n }\n // records if we got an non-object (base, source) as a parameter\n const baseRecord = new MarcRecord(base, baseValidators);\n const sourceRecord = new MarcRecord(source, sourceValidators);\n return {baseRecord, sourceRecord};\n }\n\n const baseFields = baseRecord.get(tagPattern);\n const sourceFields = sourceRecord.get(tagPattern);\n const fieldTag = sourceFields.map(field => field.tag);\n debug(`Comparing field ${fieldTag}`);\n\n checkFieldType(baseFields);\n checkFieldType(sourceFields);\n\n if (baseFields.length > 1 || sourceFields.length > 1) {\n debug(`Multiple fields in base or source`);\n debug(`No changes to base`);\n return baseRecord.toObject();\n }\n const [baseField] = baseFields;\n const [sourceField] = sourceFields;\n\n if (baseField.tag === sourceField.tag === false) {\n debug(`Base tag ${baseField.tag} is not equal to source tag ${sourceField.tag}`);\n debug(`No changes to base`);\n return baseRecord.toObject();\n }\n const baseSubs = baseField.subfields;\n const sourceSubs = sourceField.subfields;\n\n const baseSubsNormalized = baseSubs\n .map(({code, value}) => ({code, value: normalizeSubfieldValue(value)}));\n\n const sourceSubsNormalized = sourceSubs\n .map(({code, value}) => ({code, value: normalizeSubfieldValue(value)}));\n\n // Returns the base subfields for which a matching source subfield is found\n const equalSubfieldsBase = baseSubsNormalized\n .filter(baseSubfield => sourceSubsNormalized\n .some(sourceSubfield => equalityFunction(baseSubfield, sourceSubfield)));\n debug(`equalSubfieldsBase: ${JSON.stringify(equalSubfieldsBase, undefined, 2)}`);\n\n // Returns the source subfields for which a matching base subfield is found\n const equalSubfieldsSource = sourceSubsNormalized\n .filter(sourceSubfield => baseSubsNormalized\n .some(baseSubfield => equalityFunction(sourceSubfield, baseSubfield)));\n debug(`equalSubfieldsSource: ${JSON.stringify(equalSubfieldsSource, undefined, 2)}`);\n\n if (baseSubs.length === sourceSubs.length && equalSubfieldsBase.length < baseSubs.length) {\n debug(`Base and source subfields are not equal`);\n debug(`No changes to base`);\n return baseRecord.toObject();\n }\n\n if (baseSubs.length === sourceSubs.length && equalSubfieldsBase.length === equalSubfieldsSource.length) {\n debug(`Checking subfield equality`);\n const totalSubfieldLengthBase = baseSubsNormalized\n .map(({value}) => value.length)\n .reduce((acc, value) => acc + value);\n const totalSubfieldLengthSource = sourceSubsNormalized\n .map(({value}) => value.length)\n .reduce((acc, value) => acc + value);\n\n if (totalSubfieldLengthSource > totalSubfieldLengthBase) {\n return replaceBasefieldWithSourcefield(baseRecord.toObject());\n }\n }\n\n if (sourceSubs.length > baseSubs.length && equalSubfieldsBase.length === baseSubs.length) {\n return replaceBasefieldWithSourcefield(baseRecord.toObject());\n }\n\n debug(`No changes to base`);\n return baseRecord.toObject();\n\n function replaceBasefieldWithSourcefield(base) {\n const index = base.fields.findIndex(field => field === baseField);\n base.fields.splice(index, 1, sourceField); // eslint-disable-line functional/immutable-data\n debug(`Source field is longer, replacing base with source`);\n return base;\n }\n\n function checkFieldType(fields) {\n const checkedFields = fields.map(field => {\n if ('value' in field) { // eslint-disable-line functional/no-conditional-statements\n throw new Error('Invalid control field, expected data field');\n }\n return field;\n });\n return checkedFields;\n }\n\n function normalizeSubfieldValue(value) {\n // Regexp options: g: global search, u: unicode\n const punctuation = /[.,\\-/#!?$%^&*;:{}=_`~()[\\]]/gu;\n return normalizeSync(value).toLowerCase().replace(punctuation, '', 'u').replace(/\\s+/gu, ' ').trim();\n }\n};\n"],"mappings":";;;;;;;;AAAA,IAAAA,oBAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,WAAA,GAAAH,OAAA;AAAiD,SAAAE,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAE1C,SAASG,cAAcA,CAACC,SAAS,EAAEC,SAAS,EAAE;EACnD,OAAOD,SAAS,CAACE,IAAI,KAAKD,SAAS,CAACC,IAAI,IACtCF,SAAS,CAACG,KAAK,KAAKF,SAAS,CAACE,KAAK;AACvC;AAEO,SAASC,cAAcA,CAACJ,SAAS,EAAEC,SAAS,EAAE;EACnD,OAAOD,SAAS,CAACE,IAAI,KAAKD,SAAS,CAACC,IA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IACrC;IACA,MAAMuD,WAAW,GAAG,gCAAgC;IACpD,OAAO,IAAAC,kCAAa,EAACxD,KAAK,CAAC,CAACyD,WAAW,CAAC,CAAC,CAACC,OAAO,CAACH,WAAW,EAAE,EAAE,EAAE,GAAG,CAAC,CAACG,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAACC,IAAI,CAAC,CAAC;EACtG;AACF,CAAC;AAAAC,OAAA,CAAAjE,OAAA,GAAAQ,QAAA","ignoreList":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/reducers/select.js"],
4
+ "sourcesContent": ["import {normalizeSync} from 'normalize-diacritics';\nimport createDebugLogger from 'debug';\nimport {MarcRecord} from '@natlibfi/marc-record';\n\nexport function strictEquality(subfieldA, subfieldB) {\n return subfieldA.code === subfieldB.code &&\n subfieldA.value === subfieldB.value;\n}\n\nexport function subsetEquality(subfieldA, subfieldB) {\n return subfieldA.code === subfieldB.code &&\n (subfieldA.value.indexOf(subfieldB.value) !== -1 || subfieldB.value.indexOf(subfieldA.value) !== -1);\n}\n// EqualityFunction can be either strictEquality or subsetEquality\n\nexport default ({\n tagPattern,\n equalityFunction = strictEquality,\n baseValidators = {subfieldValues: false},\n sourceValidators = {subfieldValues: false}\n// eslint-disable-next-line max-statements\n}) => (base, source) => {\n const debug = createDebugLogger('@natlibfi/marc-record-merge:select');\n\n const {baseRecord, sourceRecord} = getRecordsFromParameters(base, source, baseValidators, sourceValidators);\n\n function getRecordsFromParameters(base, source, baseValidators, sourceValidators) {\n // records if we got an object ({base, source}) as a parameter\n if (source === undefined && base.base !== undefined && base.source !== undefined) {\n const baseRecord = new MarcRecord(base.base, baseValidators);\n const sourceRecord = new MarcRecord(base.source, sourceValidators);\n return {baseRecord, sourceRecord};\n }\n // records if we got an non-object (base, source) as a parameter\n const baseRecord = new MarcRecord(base, baseValidators);\n const sourceRecord = new MarcRecord(source, sourceValidators);\n return {baseRecord, sourceRecord};\n }\n\n const baseFields = baseRecord.get(tagPattern);\n const sourceFields = sourceRecord.get(tagPattern);\n const fieldTag = sourceFields.map(field => field.tag);\n debug(`Comparing field ${fieldTag}`);\n\n checkFieldType(baseFields);\n checkFieldType(sourceFields);\n\n if (baseFields.length > 1 || sourceFields.length > 1) {\n debug(`Multiple fields in base or source`);\n debug(`No changes to base`);\n return baseRecord.toObject();\n }\n const [baseField] = baseFields;\n const [sourceField] = sourceFields;\n\n if (baseField.tag === sourceField.tag === false) {\n debug(`Base tag ${baseField.tag} is not equal to source tag ${sourceField.tag}`);\n debug(`No changes to base`);\n return baseRecord.toObject();\n }\n const baseSubs = baseField.subfields;\n const sourceSubs = sourceField.subfields;\n\n const baseSubsNormalized = baseSubs\n .map(({code, value}) => ({code, value: normalizeSubfieldValue(value)}));\n\n const sourceSubsNormalized = sourceSubs\n .map(({code, value}) => ({code, value: normalizeSubfieldValue(value)}));\n\n // Returns the base subfields for which a matching source subfield is found\n const equalSubfieldsBase = baseSubsNormalized\n .filter(baseSubfield => sourceSubsNormalized\n .some(sourceSubfield => equalityFunction(baseSubfield, sourceSubfield)));\n debug(`equalSubfieldsBase: ${JSON.stringify(equalSubfieldsBase, undefined, 2)}`);\n\n // Returns the source subfields for which a matching base subfield is found\n const equalSubfieldsSource = sourceSubsNormalized\n .filter(sourceSubfield => baseSubsNormalized\n .some(baseSubfield => equalityFunction(sourceSubfield, baseSubfield)));\n debug(`equalSubfieldsSource: ${JSON.stringify(equalSubfieldsSource, undefined, 2)}`);\n\n if (baseSubs.length === sourceSubs.length && equalSubfieldsBase.length < baseSubs.length) {\n debug(`Base and source subfields are not equal`);\n debug(`No changes to base`);\n return baseRecord.toObject();\n }\n\n if (baseSubs.length === sourceSubs.length && equalSubfieldsBase.length === equalSubfieldsSource.length) {\n debug(`Checking subfield equality`);\n const totalSubfieldLengthBase = baseSubsNormalized\n .map(({value}) => value.length)\n .reduce((acc, value) => acc + value);\n const totalSubfieldLengthSource = sourceSubsNormalized\n .map(({value}) => value.length)\n .reduce((acc, value) => acc + value);\n\n if (totalSubfieldLengthSource > totalSubfieldLengthBase) {\n return replaceBasefieldWithSourcefield(baseRecord.toObject());\n }\n }\n\n if (sourceSubs.length > baseSubs.length && equalSubfieldsBase.length === baseSubs.length) {\n return replaceBasefieldWithSourcefield(baseRecord.toObject());\n }\n\n debug(`No changes to base`);\n return baseRecord.toObject();\n\n function replaceBasefieldWithSourcefield(base) {\n const index = base.fields.findIndex(field => field === baseField);\n base.fields.splice(index, 1, sourceField);\n debug(`Source field is longer, replacing base with source`);\n return base;\n }\n\n function checkFieldType(fields) {\n const checkedFields = fields.map(field => {\n if ('value' in field) {\n throw new Error('Invalid control field, expected data field');\n }\n return field;\n });\n return checkedFields;\n }\n\n function normalizeSubfieldValue(value) {\n // Regexp options: g: global search, u: unicode\n const punctuation = /[.,\\-/#!?$%^&*;:{}=_`~()[\\]]/gu;\n return normalizeSync(value).toLowerCase().replace(punctuation, '', 'u').replace(/\\s+/gu, ' ').trim();\n }\n};\n"],
5
+ "mappings": "AAAA,SAAQ,qBAAoB;AAC5B,OAAO,uBAAuB;AAC9B,SAAQ,kBAAiB;AAElB,gBAAS,eAAe,WAAW,WAAW;AACnD,SAAO,UAAU,SAAS,UAAU,QAClC,UAAU,UAAU,UAAU;AAClC;AAEO,gBAAS,eAAe,WAAW,WAAW;AACnD,SAAO,UAAU,SAAS,UAAU,SACjC,UAAU,MAAM,QAAQ,UAAU,KAAK,MAAM,MAAM,UAAU,MAAM,QAAQ,UAAU,KAAK,MAAM;AACrG;AAGA,eAAe,CAAC;AAAA,EACd;AAAA,EACA,mBAAmB;AAAA,EACnB,iBAAiB,EAAC,gBAAgB,MAAK;AAAA,EACvC,mBAAmB,EAAC,gBAAgB,MAAK;AAAA;AAE3C,MAAM,CAAC,MAAM,WAAW;AACtB,QAAM,QAAQ,kBAAkB,oCAAoC;AAEpE,QAAM,EAAC,YAAY,aAAY,IAAI,yBAAyB,MAAM,QAAQ,gBAAgB,gBAAgB;AAE1G,WAAS,yBAAyBA,OAAM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QAAQ;AAC9B,UAAM,gBAAgB,OAAO,IAAI,WAAS;AACxC,UAAI,WAAW,OAAO;AACpB,cAAM,IAAI,MAAM,4CAA4C;AAAA,MAC9D;AACA,aAAO;AAAA,IACT,CAAC;AACD,WAAO;AAAA,EACT;AAEA,WAAS,uBAAuB,OAAO;AAErC,UAAM,cAAc;AACpB,WAAO,cAAc,KAAK,EAAE,YAAY,EAAE,QAAQ,aAAa,IAAI,GAAG,EAAE,QAAQ,SAAS,GAAG,EAAE,KAAK;AAAA,EACrG;AACF;",
6
+ "names": ["base", "source", "baseValidators", "sourceValidators", "baseRecord", "sourceRecord"]
7
+ }
@@ -0,0 +1,52 @@
1
+ // Eslint configuration object for src
2
+ const configSrc = {
3
+ files: [
4
+ "src/*"
5
+ ],
6
+ linterOptions: {
7
+ reportUnusedDisableDirectives: true,
8
+ },
9
+ rules: {
10
+ "no-console": "warn",
11
+ "eqeqeq": ["error", "always"],
12
+ "no-const-assign": "error",
13
+ "max-depth": ["warn", 4],
14
+ "max-lines": ["warn", 500],
15
+ "max-lines-per-function": ["warn", {"max": 100}],
16
+ "no-else-return": ["error", {allowElseIf: false}],
17
+ "no-plusplus": [
18
+ "error",
19
+ {
20
+ "allowForLoopAfterthoughts": true
21
+ }
22
+ ],
23
+ "array-callback-return": [
24
+ "error",
25
+ {
26
+ "checkForEach": false
27
+ }
28
+ ],
29
+ "no-unused-vars": [
30
+ "error",
31
+ {
32
+ "argsIgnorePattern": "next"
33
+ }
34
+ ],
35
+ "no-warning-comments": "off"
36
+ }
37
+ };
38
+
39
+ // Eslint configuration object for globally ignoring .js files
40
+ // - ignore all files that start with a dot
41
+ // - ignore all files inside directories named 'dist'
42
+ const configIgnores = {
43
+ ignores: [
44
+ "**/.*",
45
+ "**/dist/"
46
+ ]
47
+ };
48
+
49
+ export default [
50
+ configSrc,
51
+ configIgnores
52
+ ];
package/package.json CHANGED
@@ -14,22 +14,25 @@
14
14
  "url": "git@github.com:natlibfi/marc-record-merge-js.git"
15
15
  },
16
16
  "license": "MIT",
17
- "version": "7.0.10-alpha.1",
17
+ "version": "8.0.0-alpha.1",
18
+ "type": "module",
18
19
  "main": "./dist/index.js",
19
20
  "engines": {
20
- "node": ">=18"
21
+ "node": ">=22"
21
22
  },
22
- "type": "commonjs",
23
23
  "scripts": {
24
24
  "prepare": "npm run build",
25
- "build:transpile": "babel src --copy-files --source-maps --delete-dir-on-start --out-dir=dist",
26
- "build": "npm run build:transpile",
27
- "prepublishOnly": "npm run build:transpile",
25
+ "prepublishOnly": "npm run build",
28
26
  "lint": "eslint ./src",
29
27
  "lint:dev": "eslint ./src --fix",
30
- "test:base": "cross-env NODE_ENV=test nyc mocha --package ./package.json",
28
+ "build": "esbuild \"src/**/*.js\" --sourcemap --platform=node --outdir=dist",
29
+ "test:base": "node --test --test-force-exit --experimental-test-coverage --test-reporter=spec './test/*.test.js' './test/**/*.test.js'",
30
+ "dev:test": "cross-env DEBUG=@natlibfi/* LOG_LEVEL=debug NODE_ENV=test node --watch --test --experimental-test-coverage --test-reporter=spec './test/*.test.js' './test/**/*.test.js'",
31
31
  "test": "npm run lint && npm run test:base",
32
- "test:dev": "npm run lint:dev && npm run test:base",
32
+ "test:dev": "npm run lint:dev && npm run test:base"
33
+ },
34
+ "old_scripts": {
35
+ "test:base": "cross-env NODE_ENV=test nyc mocha --package ./package.json",
33
36
  "coverage": "npm run coverage:unit && npm run coverage:report",
34
37
  "coverage:unit": "nyc --silent npm run test:base",
35
38
  "coverage:report": "nyc report",
@@ -39,87 +42,15 @@
39
42
  "dev:debug": "cross-env LOG_LEVEL=debug DEBUG=@natlibfi/* NODE_ENV=test nodemon"
40
43
  },
41
44
  "dependencies": {
42
- "@natlibfi/marc-record": "^9.1.4",
43
- "debug": "^4.4.0",
45
+ "@natlibfi/marc-record": "^10.0.0-alpha.1",
46
+ "debug": "^4.4.1",
44
47
  "normalize-diacritics": "^2.14.0"
45
48
  },
46
49
  "devDependencies": {
47
- "@babel/cli": "^7.27.0",
48
- "@babel/core": "^7.26.10",
49
- "@babel/preset-env": "^7.26.9",
50
- "@babel/register": "^7.25.9",
51
- "@babel/runtime": "^7.27.1",
52
- "@natlibfi/eslint-config-melinda-backend": "^3.0.5",
53
- "@natlibfi/fixugen": "^2.0.13",
54
- "@natlibfi/fixura": "^3.0.12",
55
- "babel-plugin-istanbul": "^7.0.0",
56
- "babel-plugin-rewire": "^1.2.0",
57
- "chai": "^4.5.0",
58
- "cross-env": "^7.0.3",
59
- "eslint": "^8.57.1",
60
- "mocha": "^11.1.0",
61
- "nodemon": "^3.1.9",
62
- "nyc": "^17.1.0"
63
- },
64
- "eslintConfig": {
65
- "extends": [
66
- "@natlibfi/melinda-backend"
67
- ]
68
- },
69
- "babel": {
70
- "presets": [
71
- [
72
- "@babel/preset-env",
73
- {
74
- "targets": "maintained node versions"
75
- }
76
- ]
77
- ],
78
- "env": {
79
- "test": {
80
- "plugins": [
81
- "rewire",
82
- "istanbul"
83
- ]
84
- }
85
- }
86
- },
87
- "mocha": {
88
- "spec": [
89
- "src/*.spec.js",
90
- "src/**/*.spec.js"
91
- ],
92
- "require": [
93
- "@babel/register"
94
- ],
95
- "inline-diffs": true,
96
- "maxDiffSize": 25000,
97
- "bail": true,
98
- "exit": true
99
- },
100
- "nodemonConfig": {
101
- "exec": "npm run test:dev",
102
- "watch": [
103
- "src/*",
104
- "test-fixtures/*"
105
- ]
106
- },
107
- "nyc": {
108
- "exclude": [
109
- "*/*.spec.js",
110
- "*/**/*.spec.js"
111
- ],
112
- "reporter": [
113
- "text"
114
- ],
115
- "require": [
116
- "@babel/register"
117
- ],
118
- "sourceMap": false,
119
- "instrument": false,
120
- "lines": 80,
121
- "statements": 80,
122
- "functions": 80,
123
- "branches": 80
50
+ "@natlibfi/fixugen": "^3.0.0-alpha.6",
51
+ "@natlibfi/fixura": "^4.0.0-alpha.13",
52
+ "cross-env": "^10.0.0",
53
+ "esbuild": "^0.25.9",
54
+ "eslint": "^9.34.0"
124
55
  }
125
56
  }
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import Reducers from './reducers';
1
+ import Reducers from './reducers/index.js';
2
2
  import createDebugLogger from 'debug';
3
3
 
4
4
  const debug = createDebugLogger('@natlibfi/melinda-marc-record-merge:index');
@@ -1,6 +1,3 @@
1
- /* eslint-disable max-statements */
2
- /* eslint-disable no-unused-vars */
3
-
4
1
  import {MarcRecord} from '@natlibfi/marc-record';
5
2
  import createDebugLogger from 'debug';
6
3
 
@@ -109,7 +106,7 @@ export default ({
109
106
  return compareFields(rest, baseCompareFields, uniqFields);
110
107
 
111
108
  function checkCompareFields(baseCompareFields, sourceCompareField) {
112
- let unique = true; // eslint-disable-line functional/no-let
109
+ let unique = true;
113
110
 
114
111
  baseCompareFields.forEach(baseCompareField => {
115
112
  debugCompare(`Comparing ${JSON.stringify(sourceCompareField)} to ${JSON.stringify(baseCompareField)}}`);
@@ -1,4 +1,4 @@
1
- import copy from './copy';
2
- import select, {strictEquality, subsetEquality} from './select';
1
+ import copy from './copy.js';
2
+ import select, {strictEquality, subsetEquality} from './select.js';
3
3
 
4
4
  export default {copy, select, strictEquality, subsetEquality};
@@ -108,14 +108,14 @@ export default ({
108
108
 
109
109
  function replaceBasefieldWithSourcefield(base) {
110
110
  const index = base.fields.findIndex(field => field === baseField);
111
- base.fields.splice(index, 1, sourceField); // eslint-disable-line functional/immutable-data
111
+ base.fields.splice(index, 1, sourceField);
112
112
  debug(`Source field is longer, replacing base with source`);
113
113
  return base;
114
114
  }
115
115
 
116
116
  function checkFieldType(fields) {
117
117
  const checkedFields = fields.map(field => {
118
- if ('value' in field) { // eslint-disable-line functional/no-conditional-statements
118
+ if ('value' in field) {
119
119
  throw new Error('Invalid control field, expected data field');
120
120
  }
121
121
  return field;
@@ -1,14 +1,14 @@
1
- import merger, {Reducers} from './index';
1
+ import merger, {Reducers} from '../src/index.js';
2
2
  import {inspect} from 'util';
3
3
  import createDebugLogger from 'debug';
4
- import {expect} from 'chai';
4
+ import assert from 'node:assert';
5
5
  import {MarcRecord} from '@natlibfi/marc-record';
6
6
  import {READERS} from '@natlibfi/fixura';
7
7
  import generateTests from '@natlibfi/fixugen';
8
8
 
9
9
  generateTests({
10
10
  callback,
11
- path: [__dirname, '..', 'test-fixtures', 'index'],
11
+ path: [import.meta.dirname, '..', 'test-fixtures', 'index'],
12
12
  recurse: false,
13
13
  useMetadataFile: true,
14
14
  fixura: {
@@ -39,7 +39,6 @@ function callback({getFixture, reducerConfigs = []}) {
39
39
  // It can also be a MarcRecord or a plain object
40
40
  const resultRecord = result.base || result;
41
41
  const resultRecordToRecord = new MarcRecord(resultRecord, {subfieldValues: false});
42
- expect(resultRecordToRecord.toObject()).to.eql(expectedRecord);
43
-
42
+ assert.deepEqual(resultRecordToRecord.toObject(), expectedRecord);
44
43
 
45
44
  }
@@ -1,6 +1,6 @@
1
- import {expect} from 'chai';
1
+ import assert from 'node:assert';
2
2
  import {READERS} from '@natlibfi/fixura';
3
- import createReducer from './copy';
3
+ import createReducer from '../../src/reducers/copy.js';
4
4
  import generateTests from '@natlibfi/fixugen';
5
5
 
6
6
  //import createDebugLogger from 'debug'; // <---
@@ -8,7 +8,7 @@ import generateTests from '@natlibfi/fixugen';
8
8
 
9
9
  generateTests({
10
10
  callback,
11
- path: [__dirname, '..', '..', 'test-fixtures', 'reducers', 'copy'],
11
+ path: [import.meta.dirname, '..', '..', 'test-fixtures', 'reducers', 'copy'],
12
12
  useMetadataFile: true,
13
13
  recurse: true,
14
14
  fixura: {
@@ -38,6 +38,7 @@ function callback({
38
38
  const tagPattern = new RegExp(tagPatternRegExp, 'u');
39
39
  const expectedRecord = getFixture('merged.json');
40
40
 
41
+ // copy2 test reducer where we have (base, source) non-object arguments as input
41
42
  const merged = createReducer({
42
43
  tagPattern, compareTagsOnly, compareWithoutTag, compareWithoutIndicators, compareWithoutIndicator1, compareWithoutIndicator2,
43
44
  copyUnless, subfieldsMustBeIdentical, excludeSubfields,
@@ -48,7 +49,7 @@ function callback({
48
49
  //debug(`*** mergedRecord,Strfy: `, JSON.stringify(mergedRecord)); //<--
49
50
  //debug(`*** expectedRecord: `, expectedRecord); //<--
50
51
  //debug(`*** expectedRecord,Strfy: `, JSON.stringify(expectedRecord)); //<--
51
- expect(merged.constructor.name).not.to.eql('MarcRecord');
52
- expect(merged.constructor.name).to.eql('Object');
53
- expect(merged).to.eql(expectedRecord);
52
+ assert.notDeepEqual(merged.constructor.name, 'MarcRecord');
53
+ assert.deepEqual(merged.constructor.name, 'Object');
54
+ assert.deepEqual(merged, expectedRecord);
54
55
  }
@@ -1,6 +1,6 @@
1
- import {expect} from 'chai';
1
+ import assert from 'node:assert';
2
2
  import {READERS} from '@natlibfi/fixura';
3
- import createReducer from './copy';
3
+ import createReducer from '../../src/reducers/copy.js';
4
4
  import generateTests from '@natlibfi/fixugen';
5
5
 
6
6
  //import createDebugLogger from 'debug'; // <---
@@ -8,7 +8,7 @@ import generateTests from '@natlibfi/fixugen';
8
8
 
9
9
  generateTests({
10
10
  callback,
11
- path: [__dirname, '..', '..', 'test-fixtures', 'reducers', 'copy'],
11
+ path: [import.meta.dirname, '..', '..', 'test-fixtures', 'reducers', 'copy'],
12
12
  useMetadataFile: true,
13
13
  recurse: true,
14
14
  fixura: {
@@ -39,6 +39,7 @@ function callback({
39
39
  const tagPattern = new RegExp(tagPatternRegExp, 'u');
40
40
  const expectedRecord = getFixture('merged.json');
41
41
 
42
+ // copy2 test reducer where we have ({base, source}) object argument as input
42
43
  const merged = createReducer({
43
44
  tagPattern, compareTagsOnly, compareWithoutTag, compareWithoutIndicators, compareWithoutIndicator1, compareWithoutIndicator2,
44
45
  copyUnless, subfieldsMustBeIdentical, excludeSubfields,
@@ -49,7 +50,7 @@ function callback({
49
50
  //debug(`*** mergedRecord,Strfy: `, JSON.stringify(mergedRecord)); //<--
50
51
  //debug(`*** expectedRecord: `, expectedRecord); //<--
51
52
  //debug(`*** expectedRecord,Strfy: `, JSON.stringify(expectedRecord)); //<--
52
- expect(merged.constructor.name).not.to.eql('MarcRecord');
53
- expect(merged.constructor.name).to.eql('Object');
54
- expect(merged).to.eql(expectedRecord);
53
+ assert.notDeepEqual(merged.constructor.name, 'MarcRecord');
54
+ assert.deepEqual(merged.constructor.name, 'Object');
55
+ assert.deepEqual(merged, expectedRecord);
55
56
  }
@@ -0,0 +1,61 @@
1
+ import assert from 'node:assert';
2
+ import {MarcRecord} from '@natlibfi/marc-record';
3
+ import createReducer, {subsetEquality} from '../../src/reducers/select.js';
4
+ import {READERS} from '@natlibfi/fixura';
5
+ import generateTests from '@natlibfi/fixugen';
6
+ import createDebugLogger from 'debug';
7
+
8
+ MarcRecord.setValidationOptions({subfieldValues: false});
9
+ const debug = createDebugLogger('@natlibfi/marc-record-merge:reducers:select:test');
10
+ const debugData = debug.extend('data');
11
+
12
+ generateTests({
13
+ callback,
14
+ path: [import.meta.dirname, '..', '..', 'test-fixtures', 'reducers', 'select'],
15
+ useMetadataFile: true,
16
+ recurse: false,
17
+ fixura: {
18
+ reader: READERS.JSON,
19
+ failWhenNotFound: false
20
+ }
21
+ });
22
+
23
+ function callback({
24
+ getFixture,
25
+ tagPatternRegExp = false,
26
+ expectedError = false,
27
+ expectedToThrow = false,
28
+ useSubsetEquality = false
29
+ }) {
30
+ // const base = new MarcRecord(getFixture('base.json'), {subfieldValues: false});
31
+ // const source = new MarcRecord(getFixture('source.json'), {subfieldValues: false});
32
+
33
+ const base = getFixture('base.json');
34
+ const source = getFixture('source.json');
35
+
36
+ const tagPattern = new RegExp(tagPatternRegExp, 'u');
37
+ const expectedRecord = getFixture('merged.json');
38
+ const equalityFunction = useSubsetEquality ? subsetEquality : undefined;
39
+
40
+ // Check expected error in testing
41
+ if (expectedToThrow || expectedError) {
42
+ debugData(`Expecting error: ${expectedToThrow}, ${expectedError}`);
43
+ try {
44
+ debug(`Trying to run merge`);
45
+ const mergedRecord = createReducer({tagPattern, equalityFunction})(base, source);
46
+ debugData(mergedRecord);
47
+ throw new Error('Expected an error');
48
+ } catch (err) {
49
+ debug(`Got error: ${err.message}`);
50
+ //assert.equal(typeof err, 'error');
51
+ assert.ok(err instanceof Error);
52
+ assert.match(err.message, new RegExp(expectedError, 'u'));
53
+ //expect(err.payload).to.match(new RegExp(expectedError, 'u'));
54
+ return;
55
+ }
56
+ }
57
+
58
+ // select tests reducer where we have (base, source) non-object argument as input
59
+ const mergedRecord = createReducer({tagPattern, equalityFunction})(base, source);
60
+ assert.deepEqual(mergedRecord, expectedRecord);
61
+ }