wouterrutgers / fuzzy-search

Simple fuzzy search
ISC License
223 stars 28 forks source link

Highlight matching characters #54

Open kacpak opened 4 years ago

kacpak commented 4 years ago

I think that it would be great if we could highlight characters that triggered the match

search: somestring result: foosomestringbar foosomethingStringbar

API could be something like this

const searcher = new FuzzySearch(people, ['name.firstName', 'state'], {
  caseSensitive: true,
  highlight: substring => colors.underline.bold(substring)
});

What do you think about such feature? I really miss something like this.

ralf57 commented 4 years ago

I am not in favour of this proposal simply because it's not the job of this library to handle results rendering. Instead, it would be great to have more control on the matching algorithm. For instance, wether is should be stricter or not, etc. As it is now, the API does not provide anything in this regard.

kacpak commented 4 years ago

@ralf57 I think that API strictness should become another issue, since it's not really relevant for highlighting.

I agree that this lib shouldn't handle rendering and this request doesn't change that. It allows for mapping over results with relevant information. Right now, for my use-case, it's incomplete if I cannot get any information on because of what parts of the string, the object was included in the results. That's why I'm suggesting a function that allows for mapping over results and modifying them with relevant data. This function API in description is only proposal to showcase the idea, it could be more generic.

wouterrutgers commented 4 years ago

Something I could do which wouldn't change much in the code I think; would be to return the indexes of the matches somehow. As I already use the indexes to search for matches.

const searcher = new FuzzySearch(people, ['name.firstName', 'state'], {
  caseSensitive: true,
});
const results = searcher.search('something');
const indexes = searcher.indexes();

Something along those lines, probably won't be exactly that. I'll have to figure a nicer way, but that way you can manually map the results.

SzNagyMisu commented 2 years ago

Hey, Any news on this one?

AshwinC8 commented 2 months ago

Something I could do which wouldn't change much in the code I think; would be to return the indexes of the matches somehow. As I already use the indexes to search for matches.

const searcher = new FuzzySearch(people, ['name.firstName', 'state'], {
  caseSensitive: true,
});
const results = searcher.search('something');
const indexes = searcher.indexes();

Something along those lines, probably won't be exactly that. I'll have to figure a nicer way, but that way you can manually map the results.

Is it underway? would love to have that.