Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tune MET-571 #210

Merged
merged 13 commits into from
Aug 14, 2024
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"url": "[email protected]:natlibfi/melinda-marc-record-merge-reducers-js.git"
},
"license": "MIT",
"version": "2.2.4",
"version": "2.2.5-alpha.1",
"main": "./dist/index.js",
"engines": {
"node": ">=18"
Expand Down Expand Up @@ -58,7 +58,7 @@
"chai": "^4.5.0",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"mocha": "^10.7.0",
"mocha": "^10.7.3",
"nodemon": "^3.1.4",
"nyc": "^17.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/reducers/addField.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {postprocessRecords} from './mergeOrAddPostprocess.js';
import {preprocessBeforeAdd} from './processFilter.js';
import fs from 'fs';
import path from 'path';
import {isValidSubfield6} from './subfield6Utils';
import {isValidSubfield6} from '@natlibfi/marc-record-validators-melinda/dist/subfield6Utils';

// Specs: https://workgroups.helsinki.fi/x/K1ohCw (though we occasionally differ from them)...

Expand Down
2 changes: 1 addition & 1 deletion src/reducers/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"subfieldFilters": [
{"code": "a", "valuePattern": "^(?:FENNI|FIKKA)$"}
],
"encodingLevel": [" ", "1", "2", "3", "4", "5", "7"]
"encodingLevel": [" ", "1", "2", "3", "5", "7"]
}
},

Expand Down
5 changes: 2 additions & 3 deletions src/reducers/mergeField.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {preprocessBeforeAdd} from './processFilter.js';

import fs from 'fs';
import path from 'path';
import {fieldGetSubfield6Pairs} from './subfield6Utils';
import {fieldGetOccurrenceNumberPairs} from '@natlibfi/marc-record-validators-melinda/dist/subfield6Utils.js';
import {fieldsToString} from '@natlibfi/marc-record-validators-melinda/dist/utils';

const defaultConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'src', 'reducers', 'config.json'), 'utf8'));
Expand Down Expand Up @@ -223,8 +223,7 @@ function mergeField(baseRecord, sourceRecord, sourceField, config) {
swapDataBetweenFields(counterpartField, sourceField);
}


const candFieldPairs880 = sourceField.tag === '880' ? undefined : fieldGetSubfield6Pairs(sourceField, sourceRecord);
const candFieldPairs880 = sourceField.tag === '880' ? undefined : fieldGetOccurrenceNumberPairs(sourceField, sourceRecord.fields);
nvdebug(`mergeField(): Got counterpart: '${fieldToString(counterpartField)}'. Thus try merge...`, debugDev);
nvdebug(`PAIR: ${candFieldPairs880 ? fieldsToString(candFieldPairs880) : 'NADA'}`, debugDev);
mergeField2(baseRecord, counterpartField, sourceField, config, candFieldPairs880);
Expand Down
7 changes: 4 additions & 3 deletions src/reducers/postprocessSubfield6.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createDebugLogger from 'debug';
import {isRelevantField6, isValidSubfield6, pairAndStringify6, removeField6IfNeeded, subfieldGetIndex6} from './subfield6Utils';
import {isRelevantField6, pairAndStringify6, removeField6IfNeeded} from './subfield6Utils';
import {isValidSubfield6, subfield6GetOccurrenceNumber} from '@natlibfi/marc-record-validators-melinda/dist/subfield6Utils';
import {fieldHasSubfield, fieldToString, nvdebug, subfieldToString} from './utils';

const debug = createDebugLogger('@natlibfi/melinda-marc-record-merge-reducers:preProcessSubfield6');
Expand Down Expand Up @@ -48,8 +49,8 @@ function subfieldApplies(subfield, lookFor) {

function getPairValue(subfield6, myTag) {
//const index = subfield6.value.replace(/^[0-9][0-9][0-9]-([0-9][0-9]+).*$/u, '$1'); // eslint-disable-line prefer-named-capture-group
const index = subfieldGetIndex6(subfield6);
const lookFor = `${myTag}-${index}`;
const occurrenceNumber = subfield6GetOccurrenceNumber(subfield6);
const lookFor = `${myTag}-${occurrenceNumber}`;
return lookFor;
}

Expand Down
12 changes: 6 additions & 6 deletions src/reducers/reindexSubfield6.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import createDebugLogger from 'debug';
import {MarcRecord} from '@natlibfi/marc-record';
import {fieldToString, nvdebug} from './utils';
import {recordGetMaxSubfield6OccurrenceNumberAsInteger, fieldGetUnambiguousOccurrenceNumber} from '@natlibfi/marc-record-validators-melinda/dist/subfield6Utils';
import {fieldGetSubfield6Pairs, getFieldsWithSubfield6Index, intToTwoDigitString, isRelevantField6, resetSubfield6Index, subfieldGetIndex6} from './subfield6Utils';
import {fieldGetOccurrenceNumberPairs, recordGetMaxSubfield6OccurrenceNumberAsInteger, fieldGetUnambiguousOccurrenceNumber, intToOccurrenceNumberString, subfield6GetOccurrenceNumber, subfield6ResetOccurrenceNumber} from '@natlibfi/marc-record-validators-melinda/dist/subfield6Utils';
import {getFieldsWithSubfield6Index, isRelevantField6} from './subfield6Utils';
import {fieldsToString} from '@natlibfi/marc-record-validators-melinda/dist/utils';


Expand All @@ -24,7 +24,7 @@ export default () => (base, source) => {
};

function subfield6Index(subfield) {
const indexPart = subfieldGetIndex6(subfield);
const indexPart = subfield6GetOccurrenceNumber(subfield);
if (indexPart === undefined) {
return 0;
}
Expand Down Expand Up @@ -58,8 +58,8 @@ function fieldUpdateSubfield6s(field, max) {
return;
}
const index = origIndex + max;
const strindex = intToTwoDigitString(index);
resetSubfield6Index(sf, strindex);
const strindex = intToOccurrenceNumberString(index);
subfield6ResetOccurrenceNumber(sf, strindex);
}
}
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export function reindexDuplicateSubfield6Indexes(record) {
nvdebug(`NEED TO REINDEX ${fieldToString(currField)}`, debugDev);
const max = recordGetMaxSubfield6OccurrenceNumberAsInteger(record);
if (max) {
const pairFields = fieldGetSubfield6Pairs(currField, record);
const pairFields = fieldGetOccurrenceNumberPairs(currField, record.fields);
if (pairFields.length) {
nvdebug(` PAIR ${fieldsToString(pairFields)}`, debugDev);
fieldUpdateSubfield6s(currField, max);
Expand Down
26 changes: 12 additions & 14 deletions src/reducers/removeIdenticalDataFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import createDebugLogger from 'debug';
import {getSubfield8Index, getSubfield8Value} from './reindexSubfield8';
import {fieldsToNormalizedString, fieldToNormalizedString, isRelevantField6, pairAndStringify6, removeField6IfNeeded} from './subfield6Utils';
import {isRelevantField6, pairAndStringify6, removeField6IfNeeded} from './subfield6Utils';
import {fieldToNormalizedString, fieldsToNormalizedString} from '@natlibfi/marc-record-validators-melinda/dist/subfield6Utils';
//import {MarcRecord} from '@natlibfi/marc-record';
import {fieldHasNSubfields, nvdebug} from './utils';

Expand Down Expand Up @@ -33,7 +34,7 @@ function isRelevantField8(field) {
return field.subfields.some(sf => getSubfield8Value(sf) !== undefined);
}

function isRelevantCommonDataField(field) {
function isUnlinkedDataField(field) {
return field.tag !== '880' && field.subfields && !isRelevantField6(field) && !isRelevantField8(field);
}

Expand Down Expand Up @@ -99,12 +100,12 @@ function removeSharedDatafieldsWithSubfield8FromSource(base, source) {

baseIndexesToInspect.forEach(baseIndex => {
const baseFields = getFieldsWithSubfield8Index(base, baseIndex);
const baseFieldsAsString = fieldsToNormalizedString(baseFields, baseIndex);
const baseFieldsAsString = fieldsToNormalizedString(baseFields, baseIndex, true, true);
nvdebug(`Results for BASE ${baseIndex}:`, debugDev);
nvdebug(`${baseFieldsAsString}`, debugDev);
sourceIndexesToInspect.forEach(sourceIndex => {
const sourceFields = getFieldsWithSubfield8Index(source, sourceIndex);
const sourceFieldsAsString = fieldsToNormalizedString(sourceFields, sourceIndex);
const sourceFieldsAsString = fieldsToNormalizedString(sourceFields, sourceIndex, true, true);
// If $8 source fields match with base fields, then remove them from source:
nvdebug(`Compare BASE and SOURCE:`, debugDev);
nvdebug(`${baseFieldsAsString} vs\n${sourceFieldsAsString}`, debugDev);
Expand All @@ -131,21 +132,18 @@ function removeSharedDatafieldsWithSubfield6FromSource(base, source) {
}

function removeCommonSharedDataFieldsFromSource(base, source) {
const baseFields = base.fields.filter(field => isRelevantCommonDataField(field));
const sourceFields = source.fields.filter(field => isRelevantCommonDataField(field));
const baseFields = base.fields.filter(field => isUnlinkedDataField(field));
const sourceFields = source.fields.filter(field => isUnlinkedDataField(field));
const baseFieldsAsString = baseFields.map(field => fieldToNormalizedString(field));

sourceFields.forEach(field => removeCommonDataFieldIfNeeded(field));
const deletableSourceFields = sourceFields.filter(field => removableField(field));

function removeCommonDataFieldIfNeeded(field) {
function removableField(field) {
const fieldAsString = fieldToNormalizedString(field);
nvdebug(`Looking for '${fieldAsString}' in '${baseFieldsAsString.join('\', \'')}'`, debugDev);
if (!baseFieldsAsString.includes(fieldAsString)) {
return;
}
nvdebug(`rCSDFFS(): Remove ${fieldAsString}`, debugDev);
source.removeField(field);
return baseFieldsAsString.includes(fieldAsString);
}

deletableSourceFields.forEach(f => source.removeField(f));
}

/*
Expand Down
5 changes: 3 additions & 2 deletions src/reducers/resetField880Subfield6AfterFieldTransfer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {fieldGetSubfield6Pairs, resetSubfield6Tag, subfieldGetTag6} from './subfield6Utils';
import {resetSubfield6Tag, subfieldGetTag6} from './subfield6Utils';
import {fieldGetOccurrenceNumberPairs} from '@natlibfi/marc-record-validators-melinda/dist/subfield6Utils';
import {fieldToString, nvdebug} from './utils';
import createDebugLogger from 'debug';

Expand All @@ -7,7 +8,7 @@ const debug = createDebugLogger('@natlibfi/melinda-marc-record-merge-reducers:re
const debugDev = debug.extend('dev');

export function resetCorrespondingField880(field, record, newTag) {
const pairedFields = fieldGetSubfield6Pairs(field, record);
const pairedFields = fieldGetOccurrenceNumberPairs(field, record.fields);

nvdebug(`RESET6: ${fieldToString(field)} got ${pairedFields.length} pair(s)`, debugDev);
pairedFields.forEach(pairedField => fixPaired880(pairedField));
Expand Down
Loading
Loading