UNPKG

what-to-play

Version:

Score aggregator for lists of games

40 lines 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.closestSearchResult = void 0; const levenshtein = require("fastest-levenshtein"); const js_lcs_1 = require("js-lcs"); function closestSearchResult(game, products, getName) { if (products.length === 0) return undefined; const gameSanitised = sanitise(game); let bestMatch; // anything below 3 is insignificant // a product with lcs = 3 can still be outputted because it equals this let matchLcs = 3; // bigger is better let matchLevenshtein = Infinity; // smaller is better for (const product of products) { const name = sanitise(getName(product)); const productLcs = js_lcs_1.LCS.size(gameSanitised, name); // maybe replace best match with a product that has a smaller LCS if (productLcs > matchLcs) { matchLcs = productLcs; matchLevenshtein = levenshtein.distance(gameSanitised, name); // store in case needed bestMatch = product; } // if they match, fallback on levenshtein comparison else if (productLcs === matchLcs) { const productLeven = levenshtein.distance(gameSanitised, name); if (productLeven < matchLevenshtein) { matchLevenshtein = productLeven; bestMatch = product; } } } return bestMatch; } exports.closestSearchResult = closestSearchResult; const nonAlphanumeric = /[^a-z0-9\(\)]/g; function sanitise(str) { return str.toLowerCase().replace(nonAlphanumeric, ""); } //# sourceMappingURL=search.js.map