Skip to content

Instantly share code, notes, and snippets.

@arunsathiya
Created February 6, 2025 17:27
Show Gist options
  • Save arunsathiya/ccd849988c80310ed881b45921fe2d77 to your computer and use it in GitHub Desktop.
Save arunsathiya/ccd849988c80310ed881b45921fe2d77 to your computer and use it in GitHub Desktop.
const findClosestSlug = (requestedSlug: string, currentSlugs: string[]): string | null => {
const requestedKeywords = extractKeywords(requestedSlug);
let bestMatch = {
slug: '',
score: 0,
matchedKeywords: 0
};
for (const slug of currentSlugs) {
const slugKeywords = extractKeywords(slug);
const matchResult = evaluateMatch(requestedKeywords, slugKeywords);
if (matchResult.score > bestMatch.score) {
bestMatch = {
slug,
score: matchResult.score,
matchedKeywords: matchResult.matches
};
}
}
return bestMatch.score >= 0.4 ? bestMatch.slug : null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment