fuzzysort
fuzzysort copied to clipboard
Prioritising parts of the content to match
say I have a data structure like
declare const tasks: {title: string, longDescription:string}[]
What is the recommended approach to fuzzy search over both title and string, but prioritise matches based on title?
just use keys. shorter strings are prioritized. so your title would automatically be prioritized over your longDescription
const books = [
{title:'spaghetti and meatballs', longDescription:"a book about food and stuff and recipes and idk this is a long description about it. i think it's a masterpiece"},
{title:'easy recipes', longDescription:"yet another book about food. probably mentions spaghetti and meatballs. i don't know, i've never read it"}
]
var results = fuzzysort.go('meatballs', books, {keys: ['title', 'longDescription']})
// {title: 'spaghetti and meatballs', score: .82}, {title: 'easy recipes', score: .67}
var results = fuzzysort.go('recipes', books, {keys: ['title', 'longDescription']})
// {title: 'easy recipes', score: .89}, {title: 'spaghetti and meatballs, score: .66}
if you need more control, you can write a custom scoreFn, something like this scoreFn: r => r.score + r[0].score*10