@natlibfi/melinda-record-match-validator
Version:
Validates if two records matched by melinda-record-matching can be merged and sets merge priority
79 lines (78 loc) • 2.76 kB
JavaScript
import createDebugLogger from "debug";
import { nvdebug } from "../utils.js";
import { getLOW } from "../collectFunctions/collectUtils.js";
const debug = createDebugLogger("@natlibfi/melinda-record-match-validator:fieldLOW");
const debugDev = debug.extend("dev");
const debugData = debug.extend("data");
const LOW2Points = {
"FIKKA": 100,
// DEVELOP: there's no FENNICA LOW ...
"FENNI": 90,
"VIOLA": 90,
// 'HELKA': 80, // We could add more libraries here. Eg. HELKA is usually good.
"undefined": 0
// not sure whether this default can happen here
};
function compareLOWValues(LOWsA, LOWsB) {
debugDev("compareLOW: A: %o vs B: %o", LOWsA, LOWsB);
const score1 = lowFieldsToScore(LOWsA);
const score2 = lowFieldsToScore(LOWsB);
nvdebug(`LOW scores: ${score1} vs ${score2}`, debugDev);
if (score1 > score2) {
nvdebug(`LOW scores: A win ${score1} vs ${score2}`, debugDev);
return "A";
}
if (score1 < score2) {
nvdebug(`LOW scores: B win ${score1} vs ${score2}`, debugDev);
return "B";
}
nvdebug(`LOW scores: same ${score1} vs ${score2}`, debugDev);
return true;
function lowFieldsToScore(lows) {
if (lows === void 0 || lows.length === 0) {
return 0;
}
return Math.max(...lows.map((low) => scoreField(low)));
}
function scoreField(value) {
if (!value) {
return 0;
}
if (value in LOW2Points) {
return LOW2Points[value];
}
return 50;
}
}
export function compareLOW(recordValuesA, recordValuesB) {
debugData(`We got recordValuesA (compareLOW): ${JSON.stringify(recordValuesA)}`);
debugData(`We got recordValuesB (compareLOW): ${JSON.stringify(recordValuesB)}`);
const LOWsA = recordValuesA.LOW;
const LOWsB = recordValuesB.LOW;
return compareLOWValues(LOWsA, LOWsB);
}
export function compareLOWinternal(recordValuesA, recordValuesB) {
debugData(`We got recordValuesA (compareLowInternal): ${JSON.stringify(recordValuesA)}`);
debugData(`We got recordValuesB (compareLowInternal): ${JSON.stringify(recordValuesB)}`);
const LOWsA = recordValuesA.LOW || recordValuesA;
const LOWsB = recordValuesB.LOW || recordValuesB;
debugData(`We got LOWsA: ${JSON.stringify(LOWsA)}`);
debugData(`We got LOWsB: ${JSON.stringify(LOWsB)}`);
if (LOWsA.some((low) => LOWsB.includes(low))) {
return false;
}
return true;
}
export function checkLOW({ record1, record2 }) {
const low1 = getLOW(record1);
const low2 = getLOW(record2);
return compareLOWValues(low1, low2);
}
export function checkLOWinternal({ record1, record2 }) {
const low1 = getLOW(record1);
const low2 = getLOW(record2);
debugData("LOWs A: %o", low1);
debugData("LOWs B: %o", low2);
return compareLOWinternal(low1, low2);
}
//# sourceMappingURL=compareFieldLOW.js.map