farzher / fuzzysort

Fast SublimeText-like fuzzy search for JavaScript.

Home Page:https://rawgit.com/farzher/fuzzysort/master/test/test.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using a key whose value is an array

n8sb opened this issue · comments

I have an object with a key called "tags" whose value is an array of tags:

const allResources = {name: foo, tags:["123","abc"]};
const searchTerm = e.target.value;
const results = fuzzysort.go(searchTerm, allResources, { key: "tags" }); // "tags" is an array
console.log(results); // [total: 0]

I am able to search an array directly, but when the array is within an object, the return value is "[total: 0]".

Is there a way to make this work?

no. you should make a list of all tags, search those, then find resources with matching tags.

that said, if performance doesn't matter you could generate a temporary searchable list before every search.

var allResources = [
  {name:'foo', tags:["123","abc"]},
]

var targets = allResources.flatMap(resource => {
  return resource.tags.map(tag => ({resource, tag}))
})

var results = fuzzysort.go('abc', targets, { key: 'tag' })
console.log(results[0].obj.resource)

Excellent. Thanks for the help and the package!

I found another solution: You can add a getter that groups everything that should be searchable into a single string:

const allResources = [
  { name: 'foo', tags: ['123', 'abc'] }
]

const searchableResources = allResources.map((item) => {
  return {
    ...item,
    get searchString() {
      // This creates the string "foo 123 abc"
      return `${item.name} ${item.tags.join(' ')}`
    }
})

fuzzysort.go('abc', searchableResources, { key: 'searchString' })

But honestly, what I'd find really handy is a new option that does what get searchString does. Something like this:

fuzzysort.go('abc', allResources, {
  filterValue(item) {
    return `${item.name} ${item.tags.join(' ')}`
  }
})

That would make the library more flexible. And in TS it wouldn't be necessary to introduce a new type for typeof allResources & { readonly searchString: string }.