UNPKG

@technobuddha/library

Version:
34 lines (33 loc) 1.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fuzzyMatch = void 0; var levenshteinDistance_1 = __importDefault(require("../levenshteinDistance")); var diceCoefficient_1 = __importDefault(require("../diceCoefficient")); var longestCommonSubstring_1 = __importDefault(require("../longestCommonSubstring")); function fuzzyMatch(input, comparedTo, _a) { var _b = _a === void 0 ? {} : _a, _c = _b.caseInsensitive, caseInsensitive = _c === void 0 ? true : _c, _d = _b.weightLevenshteinDistance, weightLevenshteinDistance = _d === void 0 ? 5 : _d, _e = _b.weightDiceCoefficient, weightDiceCoefficient = _e === void 0 ? 3 : _e, _f = _b.weightLongestCommonSubstring, weightLongestCommonSubstring = _f === void 0 ? 2 : _f; var len = Math.max(input.length, comparedTo.length); var wgt = 0; var sum = 0; if (len) { if (weightLevenshteinDistance) { sum += weightLevenshteinDistance * (1.0 - levenshteinDistance_1.default(input, comparedTo, { caseInsensitive: caseInsensitive }) / len); wgt += weightLevenshteinDistance; } if (weightDiceCoefficient) { sum += weightDiceCoefficient * (diceCoefficient_1.default(input, comparedTo, { caseInsensitive: caseInsensitive })); wgt += weightDiceCoefficient; } if (weightLongestCommonSubstring) { sum += weightLongestCommonSubstring * (longestCommonSubstring_1.default(input, comparedTo, { caseInsensitive: caseInsensitive }).length / len); wgt += weightLongestCommonSubstring; } } return wgt === 0 ? 0 : sum / wgt; } exports.fuzzyMatch = fuzzyMatch; exports.default = fuzzyMatch;