krisk / Fuse

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

search() returns different result type when fuse instance is given list of string vs list of object #287

Closed mattmazzola closed 4 years ago

mattmazzola commented 5 years ago

The results of the search are different if the list given to the constructor of Fuse is a list of strings (string[]) vs a list of objects (IObject[])

Example:

String Results:

{
    "item": 0,
    "matches": [
      {
        "indices": [
          [
            2,
            3
          ],
          [
            6,
            11
          ]
        ],
        "value": "customstring"
      }
    ]
  }

Object Results (each string wrapped with object in name property) with keys: ["name"] specified:

{
    "item": {
      "name": "customstring"
    },
    "matches": [
      {
        "indices": [
          [
            2,
            3
          ],
          [
            6,
            11
          ]
        ],
        "value": "customstring",
        "key": "name",
        "arrayIndex": 0
      }
    ]
  }

Problems

item field should be the same type as the given items.

E.g. For objects it follow that: T[] -> T But for strings it is: string[] -> number It should be string[] -> string

If you have code written to go through arbitrary results from fuse and it expects the item to actually be one of the items given it will beak!

I expect the object to be shaped consistently like this:

{
    "item": "customstring",
    "matches": [
      {
        "indices": [
          [
            2,
            3
          ],
          [
            6,
            11
          ]
        ],
        "value": "customstring",
        "arrayIndex": 0
      }
    ]
  }

It also preserves the arrayIndex which was what the item was incorrectly set to.

I realize this is a breaking change but current behavior doesn't make sense to me. On the homepage: https://fusejs.io/ it only shows search through lists of objects so this issue isn't known until it happens.

My current work around is to wrap each string to make it an object to get the upgraded behavior of fuse.js

Is there any other way?

github-actions[bot] commented 4 years ago

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