question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return whole list instead of empty array if no search term is set

See original GitHub issue

I’m using fuse.js with the includeMatches option to easily highlight matches. The problem is that the usage of includeMatches changes the structure of the list because it has to includes the matches in a fitting manner:

// with includeMatches (the structure changed to include the matches)
[
  {
    "item": {
      "title": "Old Man's War",
      "author": {
        "firstName": "John",
        "lastName": "Scalzi"
      }
    },
    "matches": [
      {
        "indices": [
          [
            0,
            12
          ]
        ],
        "value": "Old Man's War",
        "key": "title",
        "arrayIndex": 0
      }
    ]
  }
]

// without includeMatches
[
  {
    "title": "Old Man's War",
    "author": {
      "firstName": "John",
      "lastName": "Scalzi"
    }
  }
]

My problem is: I want to output the whole list if no search term is set. But fuse.js returns (for logical reasons) an empty array. Instead of this I need the whole list with the matches structure. Otherwise I need to treat it different.

Is it possible to return the whole list with empty matches if no search term is set?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:65 (13 by maintainers)

github_iconTop GitHub Comments

92reactions
d3vrcommented, Jun 15, 2018

This can be useful. Here is an example: suppose you’re showing a list of contacts and you want to use a search bar to filter it. If the search term is empty display all the list, otherwise display only the filtered results.

Is there any way to achieve this?

29reactions
acnebscommented, Apr 16, 2020

@krisk I think the use-case here for most people is that they are using Fuse for “filtering” behaviour rather than just searching, in that you have some list of items which are displayed to the user, the user enters a search term, and now you have a smaller list of items. When it comes to rendering these results to your UI, you don’t want to have two different rendering patterns for the different cases – you just want to have a single for loop that renders either every item or some subset of the items. This is much easier if Fuse has an option to spit out a “formatted” version of the data when no search term is specified, as it allows better separation of your data logic from your view logic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change return type of OPTIONAL MATCH to array, if nothing ...
I would like to return an empty array instead of null . Returns the users as list, regardless of the number of users:...
Read more >
How to Check if a JavaScript Array is Empty or Not with .length
To check if an array is empty or not, you can use the .length property. The length property sets or returns the number...
Read more >
Python - Check if a list is empty or not - GeeksforGeeks
Let's see how we can check whether a list is empty or not, in a less ... This size checks the size of...
Read more >
Array.prototype.every() - JavaScript - MDN Web Docs
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value....
Read more >
Return an empty array or collection instead of a null
Instead of returning a null value, this compliant solution simply returns the List , even when it is empty. class Inventory { private...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found