UNPKG

@natlibfi/melinda-record-match-validator

Version:

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

93 lines (92 loc) 4.14 kB
import createDebugLogger from "debug"; const debug = createDebugLogger("@natlibfi/melinda-record-match-validator:compareRecordValues:compareUtils"); const debugDev = debug.extend("dev"); const debugData = debug.extend("data"); export function compareArrayContentRequireAll(arrayA, arrayB, prefix = "") { debugDev(`${prefix}"%o" vs "%o"`, arrayA, arrayB); const normalizedArrayA = arrayA.map((value) => normalizeStrings(value)); const normalizedArrayB = arrayB.map((value) => normalizeStrings(value)); const onlyA = normalizedArrayA.filter((value) => !normalizedArrayB.includes(value)); const onlyB = normalizedArrayB.filter((value) => !normalizedArrayA.includes(value)); if (onlyA.length === 0 && onlyB.length === 0) { return true; } debugDev(`${prefix} Arrays A or B do not contain all values from each other`); return false; } export function compareStringToArray(string, array, prefix = "") { debugDev(`${prefix}"%o" vs "%o"`, string, array); const normalizedString = normalizeStrings(string); const normalizedArray = array.map((value) => normalizeStrings(value)); const found = normalizedArray.filter((value) => value === normalizedString); if (found.length > 0) { return true; } debugDev(`${prefix} Array does not contain string`); return false; } export function compareArrayContent(arrayA, arrayB, prefix = "") { debugDev(`${prefix}"%o" vs "%o"`, arrayA, arrayB); const onlyA = arrayA.filter((value) => !arrayB.includes(value)); const onlyB = arrayB.filter((value) => !arrayA.includes(value)); if (onlyA.length === 0) { return onlyB.length === 0 ? true : "B"; } if (onlyB.length === 0) { return "A"; } debugDev(`${prefix}Arrays A or B do not contain all values from each other`); return false; } const threshold = 0.6; export function compareValueContent(valueA, valueB, prefix = "") { if (valueA === "undefined" && valueB === "undefined") { debugDev(`${prefix}Value A and B are "undefined"`); return "undefined"; } if (valueA === "undefined") { debugDev(`${prefix}Value A is "undefined"`); return "B"; } if (valueB === "undefined") { debugDev(`${prefix}Value B is "undefined"`); return "A"; } if (valueA === valueB) { debugDev(`${prefix}Value A and B are same`); return true; } const valueAContainsBAvg = compareStrings(valueA, valueB, prefix); const valueBContainsAAvg = compareStrings(valueB, valueA, prefix); if (valueAContainsBAvg === 1 && valueBContainsAAvg === 1) { debugDev(`${prefix}Normalized values of A and B are same: %o`, valueAContainsBAvg); return true; } if (valueAContainsBAvg > valueBContainsAAvg && valueAContainsBAvg > threshold) { debugDev(`${prefix}Value A contains %o of B`, valueAContainsBAvg); return "A"; } if (valueBContainsAAvg > valueAContainsBAvg && valueBContainsAAvg > threshold) { debugDev(`${prefix}Value B contains %o of A`, valueBContainsAAvg); return "B"; } debugDev(`${prefix}Value A contains %o of B`, valueAContainsBAvg); debugDev(`${prefix}Value B contains %o of A`, valueBContainsAAvg); debugDev(`${prefix}Minimum of ${threshold} did not happen setting: false`); return false; function compareStrings(stringCompareTo, stringToCompare, prefix2 = "") { const stringCompareToNormalizedLowerCase = normalizeStrings(stringCompareTo).toLowerCase(); const stringToCompareNormalizedLowerCase = normalizeStrings(stringToCompare).toLowerCase(); debugData(`${prefix2}StringCompareTo: %o`, stringCompareToNormalizedLowerCase); debugData(`${prefix2}StringToCompare: %o`, stringToCompareNormalizedLowerCase); const wordArray = stringToCompareNormalizedLowerCase.split(" "); const foundWords = wordArray.filter((word) => stringCompareToNormalizedLowerCase.includes(word)); const averageFoundWords = foundWords.length / wordArray.length; return averageFoundWords; } } function normalizeStrings(stringValue) { const compNormalizedStringValue = String(stringValue).normalize("NFD"); return compNormalizedStringValue.replace(/[^\w\s\p{Alphabetic}]/gu, "").trim(); } //# sourceMappingURL=compareUtils.js.map