brianreavis / sifter.js

A library for textually searching arrays and hashes of objects by property (or multiple properties). Designed specifically for autocomplete.
1.09k stars 125 forks source link

Search with 0 to match 0 does not find a match. #52

Open Vaccano opened 6 years ago

Vaccano commented 6 years ago

If you have an item with the value of 1 in the list to search, it finds a match.

But if you have a value of 0 it is not matched on.

I found this when using selectize, which uses this library to perform the sorting. I had an id that was 0 and I could not get it to match on it. (Unfortunate because 0 is an important item in my list.)

Vaccano commented 6 years ago

I was able to fix this by modifying the source:

I changed the scoreValue subfunction's second and third lines to this:

if (value === null || typeof value === 'undefined') return 0;
value = String(value);

It was this:

if (value) return 0; value = String(value || '');

If value was 0 then this failed. And the second check was not needed because of the previous one.