@zerospacegg/vynthra
Version:
Discord bot for ZeroSpace.gg data
54 lines • 1.53 kB
JavaScript
// Fuzzy string matching utilities using fuzzy
const fuzzy = require("fuzzy");
/**
* Create a fuzzy matcher for a collection of strings
*/
function createFuzzyMatcher(items, selector) {
return { items, selector };
}
/**
* Fuzzy search items with a query string
*/
function fuzzySearch(matcher, query, limit = 10) {
const results = fuzzy.filter(query, matcher.items, {
extract: matcher.selector,
});
return results.slice(0, limit).map((result) => ({
item: result.original,
score: result.score,
positions: new Set(), // fuzzy package doesn't provide positions
}));
}
/**
* Check if a string contains another string (case insensitive)
*/
function contains(query, target) {
if (query == null || target == null) {
return false;
}
return target.toLowerCase().includes(query.toLowerCase());
}
/**
* Check if target starts with query (case insensitive)
*/
function startsWith(query, target) {
if (query == null || target == null) {
return false;
}
return target.toLowerCase().startsWith(query.toLowerCase());
}
/**
* Exact match check (case insensitive)
*/
function exactMatch(query, target) {
if (query == null || target == null) {
return false;
}
return query.toLowerCase() === target.toLowerCase();
}
//# sourceMappingURL=fuzzy.js.map
exports.createFuzzyMatcher = createFuzzyMatcher;
exports.fuzzySearch = fuzzySearch;
exports.contains = contains;
exports.startsWith = startsWith;
exports.exactMatch = exactMatch;