Skip to content

Commit

Permalink
Field f984 override [MRA-744] (#66) ; Dependency & Maintenance updates (
Browse files Browse the repository at this point in the history
#68)

* Mra 744 (#66)
Implement field 984 override (MRA-744)

* Bump the development-dependencies group with 2 updates (#67)
Bumps the development-dependencies group with 2 updates: [@babel/preset-env](https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env) and [chai](https://github.com/chaijs/chai).

* Node-tests: v16.x -> v19.x

* Bump the production-dependencies group with 2 updates (#71)
Bumps the production-dependencies group with 2 updates: [@natlibfi/marc-record](https://github.com/natlibfi/marc-record-js) and [@natlibfi/melinda-commons](https://github.com/natlibfi/melinda-commons-js).

* Bump the development-dependencies group with 1 update (#70)
Bumps the development-dependencies group with 1 update: [nodemon](https://github.com/remy/nodemon).

* 2.1.0-alpha.3
  • Loading branch information
ammsalme authored Jan 23, 2024
1 parent ec6f128 commit 0d276a2
Show file tree
Hide file tree
Showing 26 changed files with 485 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/melinda-node-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
node-version: [18.x, 19.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
55 changes: 27 additions & 28 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@natlibfi/melinda-record-match-validator",
"version": "2.0.9",
"version": "2.1.0-alpha.3",
"description": "Validates if two records matched by melinda-record-matching can be merged and sets merge priority",
"main": "dist/index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/compareRecordValues.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function callback({getFixture, enabled}) {
console.log('TEST DISABLED!'); // eslint-disable-line no-console
return;
}
console.log('TEST ENABLED!'); // eslint-disable-line no-console
//console.log('TEST ENABLED!'); // eslint-disable-line no-console
const recordValuesA = getFixture('inputRecordValuesA.json');
const recordValuesB = getFixture('inputRecordValuesB.json');
const expectedResults = getFixture('expectedResults.json');
Expand Down
68 changes: 68 additions & 0 deletions src/field984.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
*
* @licstart The following is the entire license notice for the JavaScript code in this file.
*
* Melinda record match validator modules for Javascript
*
* Copyright (C) 2024 University Of Helsinki (The National Library Of Finland)
*
* This file is part of melinda-record-match-validator
*
* melinda-record-match-validator program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* melinda-record-match-validator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @licend The above is the entire license notice
* for the JavaScript code in this file.
*
*/

// Implements MRA-744
export function check984({record1, record2}) {
const score1 = score984(record1);
const score2 = score984(record2);
// Should we use more generic score1 > score2? Does not having a 260/264 field imply badness?
// Currently
if (score1 > score2) {
return 'A';
}
if (score2 > score1) {
return 'B';
}
return true;

function score984(currRecord) {
const fields984 = currRecord.fields.filter(f => f.tag === '984');

if (fields984.some(f => isPreferred(f))) {
return 1;
}
if (fields984.some(f => isSnubbed(f))) {
return -1;
}
return 0;
}

function isPreferred(field) {
if (field.tag !== '984') {
return false;
}
return field.subfields.some(sf => sf.code === 'a' && sf.value === 'ALWAYS-PREFER-IN-MERGE');
}

function isSnubbed(field) {
if (field.tag !== '984') {
return false;
}
return field.subfields.some(sf => sf.code === 'a' && sf.value === 'NEVER-PREFER-IN-MERGE');
}
}
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {checkPublisher} from './field26X';
import {check042} from './field042';
import {check336, check337, check338} from './field33X';
import {check773} from './field773';
import {check984} from './field984';
import {checkLeader} from './leader';
import {check005, check008} from './controlFields';
import {compareRecordsPartSetFeatures} from './partsAndSets';
Expand Down Expand Up @@ -96,15 +97,15 @@ const comparisonTasks = [ // NB! These are/should be in priority order!
// Apply some recursion evilness/madness/badness to perform only the tests we really really really want.
function runComparisonTasks({nth, record1, record2, checkPreference = true, record1External = {}, record2External = {}}) {
const currResult = comparisonTasks[nth].function({record1, record2, checkPreference, record1External, record2External});
// NB! Aborts after a failure! No further tests are performed. Recursion means optimization :D
// NB! Aborts after the last task or after a failure (meaning currResult === false)! No further tests are performed. Recursion means optimization :D
if (nth === comparisonTasks.length - 1 || currResult === false) {
return [currResult];
}
return [currResult].concat(runComparisonTasks({nth: nth + 1, record1, record2, checkPreference, record1External, record2External}));
}

function makeComparisons({record1, record2, checkPreference = true, record1External = {}, record2External = {}}) {
// Start with sanity check(s):
// Start with sanity check(s): if there are no tasks, it is not a failure:
if (comparisonTasks.length === 0) {
return true;
}
Expand All @@ -117,9 +118,15 @@ function makeComparisons({record1, record2, checkPreference = true, record1Exter
}

if (!checkPreference) {
// This will also skip separate field 984 check
return {result: true, reason: 'all tests passed'};
}

const field984Override = check984({record1, record2});
if (field984Override === 'A' || field984Override === 'B') {
return {result: field984Override, reason: 'Field 984 override applied (MRA-744)'};
}

const decisionPoint = results.findIndex(val => val !== true && val !== false);
if (decisionPoint === -1) {
return {result: true, reason: 'both records passed all tests, but no winner was found'};
Expand Down
7 changes: 7 additions & 0 deletions test-fixtures/index/984_tie/expectedResults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"action": "merge",
"preference": {
"name": "A won CAT test (preference only)",
"value": "A"
}
}
49 changes: 49 additions & 0 deletions test-fixtures/index/984_tie/inputRecordA.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"leader": "01092cas a22003494i 4500",
"fields" : [
{ "tag": "001", "value": "001275158" },
{ "tag": "003", "value": "FI-MELINDA" },
{ "tag": "005", "value": "20201104004608.0" },
{ "tag": "008", "value": "010101c19879999nr |||p| ||||||||||0eng|c" },
{ "tag": "245", "ind1": "0", "ind2": "0", "subfields" : [
{ "code": "a", "value": "African dental journal /" },
{ "code": "c", "value": "Federation of African Dental Associations, FADA." }
]},
{ "tag": "984", "ind1": " ", "ind2": " ", "subfields" : [{"code": "a", "value": "ALWAYS-PREFER-IN-MERGE"}]},
{ "tag": "CAT", "ind1": " ", "ind2": " ", "subfields" : [
{ "code": "a", "value": "CONV-ISBD" },
{ "code": "b" },
{ "code": "c", "value": "20120402" },
{ "code": "l", "value": "FIN01" },
{ "code": "h", "value": "0819" }
]},
{ "tag": "CAT", "ind1": " ", "ind2": " ", "subfields" : [
{ "code": "a", "value": "LOAD-HELKA" },
{ "code": "b" },
{ "code": "c", "value": "20140520" },
{ "code": "l", "value": "FIN01" },
{ "code": "h", "value": "0018" }
]},
{ "tag": "CAT", "ind1": " ", "ind2": " ", "subfields" : [
{ "code": "a", "value": "LOAD-HELKA" },
{ "code": "b" },
{ "code": "c", "value": "20140609" },
{ "code": "l", "value": "FIN01" },
{ "code": "h", "value": "2320" }
]},
{ "tag": "CAT", "ind1": " ", "ind2": " ", "subfields" : [
{ "code": "a", "value": "CONV-RDA" },
{ "code": "b" },
{ "code": "c", "value": "20160319" },
{ "code": "l", "value": "FIN01" },
{ "code": "h", "value": "0822" }
]},
{ "tag": "CAT", "ind1": " ", "ind2": " ", "subfields" : [
{ "code": "a", "value": "KVP1008" },
{ "code": "b", "value": "30" },
{ "code": "c", "value": "20201104" },
{ "code": "l", "value": "FIN01" },
{ "code": "h", "value": "0046" }
]}
]
}
14 changes: 14 additions & 0 deletions test-fixtures/index/984_tie/inputRecordB.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"leader": "01092cas a22003494i 4500",
"fields" : [
{ "tag": "001", "value": "001275158" },
{ "tag": "003", "value": "FI-MELINDA" },
{ "tag": "005", "value": "20201104004608.0" },
{ "tag": "008", "value": "010101c19879999nr |||p| ||||||||||0eng|c" },
{ "tag": "245", "ind1": "0", "ind2": "0", "subfields" : [
{ "code": "a", "value": "African dental journal /" },
{ "code": "c", "value": "Federation of African Dental Associations, FADA." }
]},
{ "tag": "984", "ind1": " ", "ind2": " ", "subfields" : [{"code": "a", "value": "ALWAYS-PREFER-IN-MERGE"}]}
]
}
6 changes: 6 additions & 0 deletions test-fixtures/index/984_tie/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"description": "Record with (non-load/import/conversion) CATs wins. Two 984$a ALWAYS-PREFER-IN-MERGE cancel each other out",
"enabled": true,
"only": false,
"skip": false
}
7 changes: 7 additions & 0 deletions test-fixtures/index/984_tie2/expectedResults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"action": "merge",
"preference": {
"name": "A won CAT test (preference only)",
"value": "A"
}
}
Loading

0 comments on commit 0d276a2

Please sign in to comment.