UNPKG

@natlibfi/melinda-record-match-validator

Version:

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

83 lines (82 loc) 3.13 kB
import { getTitleFeatures } from "../collectFunctions/collectTitle.js"; import { getExtentsForPartsAndSets } from "./partsAndSetsExtent.js"; import createDebugLogger from "debug"; const debug = createDebugLogger("@natlibfi/melinda-record-match-validator:partsAndSets"); const debugDev = debug.extend("dev"); const debugData = debug.extend("data"); export function getPartSetFeatures(record) { const extentsForPartsAndSets = getExtentsForPartsAndSets(record); debugData(`Extents: <${JSON.stringify(extentsForPartsAndSets)}>`); const titleForPartsAndSets = getTitleForPartsAndSets(record); debugData(`Title: <${JSON.stringify(titleForPartsAndSets)}>`); const allTypes = [titleForPartsAndSets.type, ...extentsForPartsAndSets.map((extent) => extent.type)]; debugData(allTypes); function getTypeFromAllTypes(allTypes2) { if (allTypes2.some((type) => type === "set") && !allTypes2.some((type) => type === "part")) { return "set"; } if (allTypes2.some((type) => type === "part") && !allTypes2.some((type) => type === "set")) { return "part"; } if (allTypes2.some((type) => type === "set")) { return "set"; } return "unknown"; } return { type: getTypeFromAllTypes(allTypes), details: { extentsForPartsAndSets, titleForPartsAndSets } }; } export function getTitleForPartsAndSets(record) { const titleFeatures = getTitleFeatures(record); const featuresType = getTitleFeaturesType(titleFeatures); debugDev(`FeaturesType: ${featuresType}`); return { ...titleFeatures, type: featuresType }; } export function getTitleFeaturesType(title) { debugData(title); if (title === void 0) { return "unknown"; } const { namesOfPartInSectionOfAWork, numbersOfPartInSectionOfAWork } = title; if (namesOfPartInSectionOfAWork.length < 1 && numbersOfPartInSectionOfAWork.length < 1) { return "unknown"; } if (numbersOfPartInSectionOfAWork.length === 1) { debugDev(`We have a number: ${numbersOfPartInSectionOfAWork[0]}`); if (numbersOfPartInSectionOfAWork[0].match(/\d+-\d+/u)) { debugDev(`But number is of several parts: ${numbersOfPartInSectionOfAWork[0]}`); return "unknown"; } return "part"; } if (namesOfPartInSectionOfAWork.length === 1) { debugDev(`We have a name: ${namesOfPartInSectionOfAWork[0]}`); return "part"; } return "unknown"; } export function compareRecordsPartSetFeatures({ record1, record2 }) { const partSetFeatures1 = getPartSetFeatures(record1); const partSetFeatures2 = getPartSetFeatures(record2); return checkPartSetFeatures({ partSetFeatures1, partSetFeatures2 }); } export function checkPartSetFeatures({ partSetFeatures1, partSetFeatures2 }) { debugData(JSON.stringify(partSetFeatures1)); debugData(JSON.stringify(partSetFeatures2)); if (partSetFeatures1.type === partSetFeatures2.type) { return true; } if (partSetFeatures1.type === "unknown" || partSetFeatures2.type === "unknown") { return true; } if (partSetFeatures1.type !== partSetFeatures2.type) { return false; } return false; } //# sourceMappingURL=partsAndSets.js.map