UNPKG

@natlibfi/melinda-record-match-validator

Version:

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

80 lines (79 loc) 2.24 kB
export function nvdebug(message, debug = void 0) { if (debug) { debug(message); return; } } function recordGetNonRepeatableField(record, tag) { const fields = record.get(tag); if (fields.length === 1) { return fields[0]; } return void 0; } export function recordGetSubfieldValuesFromNonRepeatableField(record, tag, subfieldCode) { const field = recordGetNonRepeatableField(record, tag); if (!field || !field.subfields) { return []; } const subfields = field.subfields.filter((sf) => sf.code === subfieldCode); return subfields.map((sf) => sf.value); } function getMelindaDefaultPrefix() { return "(FI-MELINDA)"; } export function normalizeMelindaId(value) { const melindaPrefix = getMelindaDefaultPrefix(); if (/^FCC[0-9]{9}$/u.test(value)) { return `${melindaPrefix}${value.substring(3)}`; } if (/^\(FIN01\)[0-9]{9}$/u.test(value)) { return `${melindaPrefix}${value.substring(7)}`; } if (/^\(FI-MELINDA\)[0-9]{9}$/u.test(value)) { return `${melindaPrefix}${value.substring(12)}`; } return value; } export function isValidNormalizedMelindaId(value) { const prefix = getMelindaDefaultPrefix(); const regexp = new RegExp(`(${prefix})[0-9]{9}$/`, "u"); if (regexp.test(value)) { return true; } return false; } function getIdPrefix(id) { const i = id.indexOf(")"); if (i === -1) { return ""; } return id.substring(0, i + 1); } export function splitIds(ids) { const internalIds = [...new Set(ids.map((value) => normalizeMelindaId(value)).filter((value) => isValidNormalizedMelindaId(value)))]; const otherIds = [...new Set(ids.map((value) => normalizeMelindaId(value)).filter((value) => !isValidNormalizedMelindaId(value)))]; return { internalIds, otherIds }; } export function hasIdMismatch(otherId, idSet) { const otherPrefix = getIdPrefix(otherId); return idSet.some((id) => { if (id === otherId) { return false; } const prefix = getIdPrefix(id); if (prefix === otherPrefix) { return true; } return false; }); } export function hasIdMatch(otherId, idSet) { return idSet.some((id) => { if (id === otherId) { return true; } return false; }); } //# sourceMappingURL=utils.js.map