@natlibfi/melinda-record-match-validator
Version:
Validates if two records matched by melinda-record-matching can be merged and sets merge priority
116 lines (115 loc) • 5.44 kB
JavaScript
import createDebugLogger from "debug";
import { getBibliographicLevel, getEncodingLevel, getRecordInfo, getTypeOfRecord, EI_ENNAKKOTIETO, KONEELLISESTI_TUOTETTU_TIETUE, TARKISTETTU_ENNAKKOTIETO, ENNAKKOTIETO } from "../collectFunctions/collectLeader.js";
import { nvdebug } from "../utils.js";
const debug = createDebugLogger("@natlibfi/melinda-record-match-validator:compareFunctions/compareLeader");
const debugDev = debug.extend("dev");
function rateValues(valueA, valueB, rateArray) {
debugDev("%o vs %o", valueA, valueB);
if (valueA.code === valueB.code) {
debugDev("Both same: returning true");
return true;
}
if (rateArray) {
const ratingOfA = rateArray.indexOf(valueA.code) + 1;
const ratingOfB = rateArray.indexOf(valueB.code) + 1;
if (ratingOfA === 0) {
if (ratingOfB !== 0) {
debugDev("A's value not found in array. Return B");
return "B";
}
return false;
}
if (ratingOfB === 0) {
debugDev("B's value not found in array. Return A");
return "A";
}
if (ratingOfA < ratingOfB) {
debugDev("A better: returning A");
return "A";
}
debugDev("B better: returning B");
return "B";
}
debugDev("Both different: returning false");
return false;
}
function compareTypeOfRecord(a, b) {
debugDev("Record A type: %o", a);
debugDev("Record B type: %o", b);
return rateValues(a, b);
}
function compareBibliographicLevel(a, b) {
debugDev("Record A bib level: %o", a);
debugDev("Record B bib level: %o", b);
return rateValues(a, b);
}
function compareEncodingLevel(a, b, prePubA, prePubB, recordSourceA, recordSourceB) {
debugDev("Record A completion level: %o", a);
debugDev("Record B completion level: %o", b);
nvdebug(prePubA ? `Record A prepub level: ${JSON.stringify(prePubA)}` : "N/A", debugDev);
nvdebug(prePubB ? `Record B prepub level: ${JSON.stringify(prePubB)}` : "N/A", debugDev);
nvdebug(recordSourceA ? `Record A external type: ${JSON.stringify(recordSourceA)}` : "N/A", debugDev);
nvdebug(recordSourceB ? `Record B external type: ${JSON.stringify(recordSourceB)}` : "N/A", debugDev);
if (prePubA && prePubB && a.code === b.code && ["2", "8"].includes(a.code)) {
const prePubValue = rateValues(prePubA, prePubB, [EI_ENNAKKOTIETO, KONEELLISESTI_TUOTETTU_TIETUE, TARKISTETTU_ENNAKKOTIETO, ENNAKKOTIETO]);
if (prePubValue === true && prePubA.code !== "0" && prePubB.code !== "0") {
const valueA = { code: recordSourceA };
const valueB = { code: recordSourceB };
const rateArray2 = ["incomingRecord", "databaseRecord", void 0];
return rateValues(valueA, valueB, rateArray2);
}
return prePubValue;
}
const rateArray = [" ", "1", "4", "5", "2", "7", "3", "u", "z", "8"];
return rateValues(a, b, rateArray);
}
export function compareLeader(recordValuesA, recordValuesB) {
const f000A = recordValuesA["000"];
const f000B = recordValuesB["000"];
const result = {
typeOfRecord: compareTypeOfRecord(f000A.typeOfRecord, f000B.typeOfRecord),
bibliographicLevel: compareBibliographicLevel(f000A.bibliographicLevel, f000B.bibliographicLevel),
encodingLevel: compareEncodingLevel(f000A.encodingLevel, f000B.encodingLevel, f000A.prepublicationLevel, f000B.prepublicationLevel)
};
return result;
}
export function checkTypeOfRecord({ record1, record2 }) {
const recordInfo1 = getTypeOfRecord(record1);
const recordInfo2 = getTypeOfRecord(record2);
return compareTypeOfRecord(recordInfo1.typeOfRecord, recordInfo2.typeOfRecord);
}
export function checkBibliographicLevel({ record1, record2 }) {
const recordInfo1 = getBibliographicLevel(record1);
const recordInfo2 = getBibliographicLevel(record2);
return compareBibliographicLevel(recordInfo1.bibliographicLevel, recordInfo2.bibliographicLevel);
}
export function checkRecordLevel({ record1, record2, record1External = {}, record2External = {} }) {
const recordInfo1 = getEncodingLevel(record1);
const recordInfo2 = getEncodingLevel(record2);
const recordSource1 = record1External.recordSource || void 0;
const recordSource2 = record2External.recordSource || void 0;
return compareEncodingLevel(recordInfo1.encodingLevel, recordInfo2.encodingLevel, recordInfo1.prepublicationLevel, recordInfo2.prepublicationLevel, recordSource1, recordSource2);
}
export function checkLeader({ record1, record2, checkPreference = true, record1External = {}, record2External = {} }) {
const recordInfo1 = getRecordInfo(record1);
const recordInfo2 = getRecordInfo(record2);
const recordSource1 = record1External.recordSource || void 0;
const recordSource2 = record2External.recordSource || void 0;
debugDev(`checkLeader()`);
debugDev(`checkLeader()`);
if (recordInfo1.typeOfRecord.code !== recordInfo2.typeOfRecord.code) {
debugDev(`LDR: type of record failed!`);
return false;
}
if (recordInfo1.bibliographicLevel.code !== recordInfo2.bibliographicLevel.code) {
debugDev(`LDR: bibliographical level failed!`);
return false;
}
const encodingLevelPreference = compareEncodingLevel(recordInfo1.encodingLevel, recordInfo2.encodingLevel, recordInfo1.prepublicationLevel, recordInfo2.prepublicationLevel, recordSource1, recordSource2);
if (encodingLevelPreference === false) {
debugDev(`LDR: encoding level failed!`);
return false;
}
return checkPreference ? encodingLevelPreference : true;
}
//# sourceMappingURL=compareLeader.js.map