UNPKG

@natlibfi/melinda-record-match-validator

Version:

Validates if two records matched by melinda-record-matching can be merged and sets merge priority

109 lines (101 loc) 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkLOW = checkLOW; exports.checkLOWinternal = checkLOWinternal; exports.compareLOW = compareLOW; exports.compareLOWinternal = compareLOWinternal; exports.getLOW = getLOW; var _collectUtils = require("./collectFunctions/collectUtils"); var _debug = _interopRequireDefault(require("debug")); var _utils = require("./utils"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } //import {compareArrayContent} from './compareFunctions/compareUtils'; const debug = (0, _debug.default)('@natlibfi/melinda-record-match-validator:fieldLOW'); const debugDev = debug.extend('dev'); const debugData = debug.extend('data'); function getLOW(record) { // Do not return empty/non-existent LOW $a's as 'undefined' const LOWs = (0, _collectUtils.hasFields)('LOW', record, _collectUtils.getSubfield, 'a').filter(element => element && element !== 'undefined'); debugData('LOWs: %o', LOWs); return LOWs; } // Priority array for various LOW tags. Default value for an existing LOW is 50, and for no LOW 0. const LOW2Points = { 'FIKKA': 100, // DEVELOP: there's no FENNICA LOW ... 'FENNICA': 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); // return compareArrayContent(LOWsA, LOWsB, true); // NV: size does not matter const score1 = lowFieldsToScore(LOWsA); const score2 = lowFieldsToScore(LOWsB); (0, _utils.nvdebug)(`LOW scores: ${score1} vs ${score2}`, debugDev); if (score1 > score2) { return 'A'; } if (score1 < score2) { return 'B'; } return true; // never blocks function lowFieldsToScore(lows) { // min=0, max=100 if (lows === undefined || lows.length === 0) { // Having no LOW fields is pretty suspicious return 0; } //const low2Score = lows.map(low => scoreField(low)); //debug(' mapped to %o', low2Score); return Math.max(...lows.map(low => scoreField(low))); } function scoreField(value) { if (!value) { return 0; } if (value in LOW2Points) { return LOW2Points[value]; } // If we'd want to, we could add some kind of priority based on organizations. // However, we wouldn't be making friends there: If X > Y, then Y might hurt his feelings. return 50; } } function compareLOW(recordValuesA, recordValuesB) { debugData(`We got recordValuesA (compareLOW): ${JSON.stringify(recordValuesA)}`); const LOWsA = recordValuesA.LOW; const LOWsB = recordValuesB.LOW; return compareLOWValues(LOWsA, LOWsB); } function compareLOWinternal(recordValuesA, recordValuesB) { debugData(`We got recordValuesA (compareLowInternal): ${JSON.stringify(recordValuesA)}`); const LOWsA = recordValuesA.LOW || recordValuesA; const LOWsB = recordValuesB.LOW || recordValuesB; if (LOWsA.some(low => LOWsB.includes(low))) { return false; } return true; } function checkLOW({ record1, record2 }) { const low1 = getLOW(record1); const low2 = getLOW(record2); return compareLOWValues(low1, low2); } 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=fieldLOW.js.map