Files
stack/packages/mosaic/framework/skills/vueuse-functions/references/useFuse.md
T
fargo 1a822493ba format: apply repo prettier (3.8.1) to the folded skills tree
963 markdown files reformatted with the repository's pinned prettier so
pnpm format:check covers the folded tree like every other repo file.

The formatter's embedded-language pass also normalized code fences
(TS semicolons, closed HTML tags in examples, lowercased CSS hex colors,
one renumbered list that skipped an index). Alphanumeric token deltas vs
the fold commit were audited file-by-file; all are formatter-equivalent
markup normalizations plus the four sanitized skills.
2026-08-19 14:37:17 -05:00

2.3 KiB

category
category
@Integrations

useFuse

Easily implement fuzzy search using a composable with Fuse.js.

From the Fuse.js website:

What is fuzzy searching?

Generally speaking, fuzzy searching (more formally known as approximate string matching) is the technique of finding strings that are approximately equal to a given pattern (rather than exactly).

Install Fuse.js as a peer dependency

NPM

npm install fuse.js@^7

Yarn

yarn add fuse.js

Usage

import { useFuse } from '@vueuse/integrations/useFuse';
import { shallowRef } from 'vue';

const data = ['John Smith', 'John Doe', 'Jane Doe', 'Phillip Green', 'Peter Brown'];

const input = shallowRef('Jhon D');

const { results } = useFuse(input, data);

/*
 * Results:
 *
 * { "item": "John Doe", "index": 1 }
 * { "item": "John Smith", "index": 0 }
 * { "item": "Jane Doe", "index": 2 }
 *
 */

Type Declarations

export type FuseOptions<T> = IFuseOptions<T>;
export interface UseFuseOptions<T> {
  fuseOptions?: FuseOptions<T>;
  resultLimit?: number;
  matchAllWhenSearchEmpty?: boolean;
}
export declare function useFuse<DataItem>(
  search: MaybeRefOrGetter<string>,
  data: MaybeRefOrGetter<DataItem[]>,
  options?: MaybeRefOrGetter<UseFuseOptions<DataItem>>,
): {
  fuse: Ref<
    {
      search: <R = DataItem>(
        pattern: string | Expression,
        options?: FuseSearchOptions,
      ) => FuseResult<R>[];
      setCollection: (docs: readonly DataItem[], index?: FuseIndex<DataItem> | undefined) => void;
      add: (doc: DataItem) => void;
      remove: (predicate: (doc: DataItem, idx: number) => boolean) => DataItem[];
      removeAt: (idx: number) => void;
      getIndex: () => FuseIndex<DataItem>;
    },
    | Fuse<DataItem>
    | {
        search: <R = DataItem>(
          pattern: string | Expression,
          options?: FuseSearchOptions,
        ) => FuseResult<R>[];
        setCollection: (docs: readonly DataItem[], index?: FuseIndex<DataItem> | undefined) => void;
        add: (doc: DataItem) => void;
        remove: (predicate: (doc: DataItem, idx: number) => boolean) => DataItem[];
        removeAt: (idx: number) => void;
        getIndex: () => FuseIndex<DataItem>;
      }
  >;
  results: ComputedRef<FuseResult<DataItem>[]>;
};
export type UseFuseReturn = ReturnType<typeof useFuse>;