krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
17.76k stars 753 forks source link

Return all results if not search query is passed #664

Closed mrleblanc101 closed 1 year ago

mrleblanc101 commented 2 years ago

Description

I used Fuse.js in a Vue application. I wish to display all results if no search params is passed. I can't loop on the original data since the format is not exactly the same so it would complexify my code quite a bit.

Describe the solution you'd like

Add an option to return all results when no search query is provided

Describe alternatives you've considered

Loop on my original object and use if/else to handle the different structure.

springuper commented 2 years ago

You could make it with Fuse's APIs, like this:

const fuse = new Fuse(...);

const searchWrapper = query => {
  if (!query) return fuse.getIndex().records.map(({ $: item, i: idx }) => ({ idx, item }));
  return fuse.search(query);
};
jaitaiwan commented 2 years ago

Just came across this exact issue and wanted to note a couple of things about the second reply just in case someone goes to use it.

fuse.getIndex() doesn't have a records property in the type definition (so it will error if using typescript). $ doesn't actually contain an item from the collection so that won't work as a solution.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 30 days

jaitaiwan commented 1 year ago

We’re still affected by this, but haven’t had the time to do a PR

jaitaiwan commented 1 year ago

I'm not sure why the bot closed this? I commented afterwards to stop it from getting closed.