4.1.1 • Published 2 years ago

vue-fuse v4.1.1

Weekly downloads
2,632
License
MIT
Repository
github
Last release
2 years ago

vue-fuse

A fully typed, lightweight, and reactive interface for Fuse.js fuzzy search lib.

Live Demo

4.X

Version 4.X is a complete rewrite, intended to leverage Vue 3 (but still works with Vue 2 with the composition-api plugin).

Check out the 3.x branch for older version, or look at release notes.

Vue 3 or Vue 2

vue-fuse uses vue-demi to offer Vue 3.X or Vue 2.X compatibility.

Vue 2.X requires @vue/composition-api

Getting Started

npm i vue-fuse fuse.js
<template>
  <input type="text" v-model="search">
    <p v-if="noResults">Sorry, no results for {{search}}</p>
    <div v-for="(r, i) in results" :key="i">
      {{ r }}
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { useVueFuse } from 'vue-fuse'

export default defineComponent({
  setup () {
    const myList = ['aaaa', 'bbbb', 'cccc', 'abc', 'xyz']
    const { search, results, noResults } = useVueFuse(myList)

    return {
      search,
      results,
      noResults,
    }
  }
})
</script>

Typings

A vue-fuse instance will detect the typed array of items needed to search. Thus, your results should be fully typed to match the type of the array items passed in.

Options

useVueFuse and/or the VueFuse class constructor accept an optional second argument where you can pass in a Fuse.js Options Object

results, resultsRaw, noResults

const { results, resaltsRaw, noResults } = useVueFuse(['a', 'b', 'c'])

results - is an array containing a the subset of items you passed in that match the search resultsRaw - exposed the full result payload from Fuse.js, this contains things like the original array index or the match "score". noResults - a computed boolean that will be true when there are no results, but the search term is not empty

Searching Array of Objects

The examples above include simple arrays of strings, but you'll probably want to search accross more complex data structures. Fuse.js supports this by allowing you to pass keys into the search options.

so if you have an array of objects like this:

const bikes = [
  {
    brand: 'Santa Cruz',
    model: '5010',
    year: 2021,
    wheelSize: 27.5,
  },
  {
    brand: 'Canyon',
    model: 'Neuron',
    year: 2021,
    wheelSize: 29,
  }
]

To search by brand and model, you could set your config to:

{
  keys: ['brand', 'model']
}

You can also weight each key differently

{
  keys: [
    {
      name: 'brand',
      weight: 3,
    },
    {
      name: 'model',
      weight: 1,
    }
  ]
}

You can also search nested keys by chaining the property names

{
  keys: ['foo', 'bar.baz.buzz']
}
4.1.0

2 years ago

4.1.1

2 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.0.0-beta.3

3 years ago

3.0.0-beta.2

3 years ago

3.0.0-beta.1

3 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.1.0-beta2

5 years ago

2.1.1-beta

5 years ago

2.1.0-beta

5 years ago

2.0.3

5 years ago

2.0.2

6 years ago

2.0.2-beta3

6 years ago

2.0.2-beta2

6 years ago

2.0.2-beta

6 years ago

2.0.1

6 years ago

2.0.1-beta

6 years ago

2.0.0

6 years ago

2.0.0-beta

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago