fuse.js
- Version 7.1.0
- Published
- 456 kB
- No dependencies
- Apache-2.0 license
Install
npm i fuse.js
yarn add fuse.js
pnpm add fuse.js
Overview
Lightweight fuzzy-search
Index
Classes
Interfaces
Type Aliases
- Expression
- FuseGetFunction
- FuseIndexObjectRecord
- FuseIndexOptions
- FuseIndexRecords
- FuseIndexStringRecord
- FuseOptionKey
- FuseOptionKeyObject
- FuseResult
- FuseResultMatch
- FuseSearchOptions
- FuseSortFunction
- FuseSortFunctionArg
- FuseSortFunctionItem
- FuseSortFunctionMatch
- FuseSortFunctionMatchList
- RangeTuple
- RecordEntry
- RecordEntryArrayItem
- RecordEntryObject
Classes
class Fuse
class Fuse<T> {}
constructor
public constructor( list: readonly T[], options?: IFuseOptions<T>, index?: FuseIndex<T>);
property config
static config: Required<IFuseOptions<any>>;
property version
static version: string;
Return the current version.
method add
add: (doc: T) => void;
Adds a doc to the end the list.
method createIndex
static createIndex: <U>( keys: Array<FuseOptionKey<U>>, list: ReadonlyArray<U>, options?: FuseIndexOptions<U>) => FuseIndex<U>;
Use this method to pre-generate the index from the list, and pass it directly into the Fuse instance.
_Note that Fuse will automatically index the table if one isn't provided during instantiation._
const list: MyType[] = [myType1, myType2, etc...]const index = Fuse.createIndex<MyType>(keys: ['key1', 'key2']list: list)const options: Fuse.IFuseOptions<MyType> = {keys: ['key1', 'key2']}const myFuse = new Fuse(list, options, index)Parameter keys
The keys to index
Parameter list
The list from which to create an index
Parameter options
?
Returns
An indexed list
method getIndex
getIndex: () => FuseIndex<T>;
Returns the generated Fuse index
method parseIndex
static parseIndex: <U>( index: { keys: ReadonlyArray<string>; records: FuseIndexRecords }, options?: FuseIndexOptions<U>) => FuseIndex<U>;
method remove
remove: (predicate: (doc: T, idx: number) => boolean) => T[];
Removes all documents from the list which the predicate returns truthy for, and returns an array of the removed docs. The predicate is invoked with two arguments: (doc, index).
method removeAt
removeAt: (idx: number) => void;
Removes the doc at the specified index.
method search
search: <R = T>( pattern: string | Expression, options?: FuseSearchOptions) => FuseResult<R>[];
Search function for the Fuse instance.
const list: MyType[] = [myType1, myType2, etc...]const options: Fuse.IFuseOptions<MyType> = {keys: ['key1', 'key2']}const myFuse = new Fuse(list, options)let result = myFuse.search('pattern')Parameter pattern
The pattern to search
Parameter options
Fuse.FuseSearchOptions
Returns
An array of search results
method setCollection
setCollection: (docs: ReadonlyArray<T>, index?: FuseIndex<T>) => void;
class FuseIndex
class FuseIndex<T> {}
constructor
public constructor(options?: FuseIndexOptions<T>);
method add
add: (doc: T) => void;
method create
create: () => void;
method setIndexRecords
setIndexRecords: (records: FuseIndexRecords) => void;
method setKeys
setKeys: (keys: ReadonlyArray<string>) => void;
method setSources
setSources: (docs: ReadonlyArray<T>) => void;
method toJSON
toJSON: () => { keys: ReadonlyArray<string>; records: FuseIndexRecords };
Interfaces
interface IFuseOptions
interface IFuseOptions<T> {}
property distance
distance?: number;
Determines how close the match must be to the fuzzy location (specified by
location
). An exact letter match which isdistance
characters away from the fuzzy location would score as a complete mismatch. Adistance
of0
requires the match be at the exactlocation
specified. A distance of1000
would require a perfect match to be within800
characters of thelocation
to be found using athreshold
of0.8
.
property fieldNormWeight
fieldNormWeight?: number;
Determines how much the
field-length norm
affects scoring. A value of0
is equivalent to ignoring the field-length norm. A value of0.5
will greatly reduce the effect of field-length norm, while a value of2.0
will greatly increase it.
property findAllMatches
findAllMatches?: boolean;
When true, the matching function will continue to the end of a search pattern even if a perfect match has already been located in the string.
property getFn
getFn?: FuseGetFunction<T>;
The function to use to retrieve an object's value at the provided path. The default will also search nested paths.
property ignoreDiacritics
ignoreDiacritics?: boolean;
Indicates whether comparisons should ignore diacritics (accents).
property ignoreFieldNorm
ignoreFieldNorm?: boolean;
When
true
, the calculation for the relevance score (used for sorting) will ignore thefield-length norm
.
property ignoreLocation
ignoreLocation?: boolean;
When
true
, search will ignorelocation
anddistance
, so it won't matter where in the string the pattern appears.
property includeMatches
includeMatches?: boolean;
Whether the matches should be included in the result set. When
true
, each record in the result set will include the indices of the matched characters. These can consequently be used for highlighting purposes.
property includeScore
includeScore?: boolean;
Whether the score should be included in the result set. A score of
0
indicates a perfect match, while a score of1
indicates a complete mismatch.
property isCaseSensitive
isCaseSensitive?: boolean;
Indicates whether comparisons should be case sensitive.
property keys
keys?: Array<FuseOptionKey<T>>;
List of keys that will be searched. This supports nested paths, weighted search, searching in arrays of
strings
andobjects
.
property location
location?: number;
Determines approximately where in the text is the pattern expected to be found.
property minMatchCharLength
minMatchCharLength?: number;
Only the matches whose length exceeds this value will be returned. (For instance, if you want to ignore single character matches in the result, set it to
2
).
property shouldSort
shouldSort?: boolean;
Whether to sort the result list, by score.
property sortFn
sortFn?: FuseSortFunction;
The function to use to sort all the results. The default will sort by ascending relevance score, ascending index.
property threshold
threshold?: number;
At what point does the match algorithm give up. A threshold of
0.0
requires a perfect match (of both letters and location), a threshold of1.0
would match anything.
property useExtendedSearch
useExtendedSearch?: boolean;
When
true
, it enables the use of unix-like search commands. See [example](/examples.html#extended-search).
Type Aliases
type Expression
type Expression = | { [key: string]: string } | { $path: ReadonlyArray<string>; $val: string; } | { $and?: Expression[] } | { $or?: Expression[] };
type FuseGetFunction
type FuseGetFunction<T> = ( obj: T, path: string | string[]) => ReadonlyArray<string> | string;
type FuseIndexObjectRecord
type FuseIndexObjectRecord = { /** The index of the record in the source list */ i: number; $: RecordEntry;};
Example 1
{i: 0,'$': {'0': { v: "Old Man's War", n: 0.5773502691896258 },'1': { v: 'Codenar', n: 1 },'2': [{ v: 'pizza lover', i: 2, n: 0.7071067811865475 },{ v: 'helo wold', i: 1, n: 0.7071067811865475 },{ v: 'hello world', i: 0, n: 0.7071067811865475 }]}}
type FuseIndexOptions
type FuseIndexOptions<T> = { getFn: FuseGetFunction<T>;};
type FuseIndexRecords
type FuseIndexRecords = | ReadonlyArray<FuseIndexObjectRecord> | ReadonlyArray<FuseIndexStringRecord>;
type FuseIndexStringRecord
type FuseIndexStringRecord = { /** The index of the record in the source list */ i: number; /** The text value */ v: string; /** The field-length norm */ n: number;};
Example 1
{keys: null,list: [{ v: 'one', i: 0, n: 1 },{ v: 'two', i: 1, n: 1 },{ v: 'three', i: 2, n: 1 }]}
type FuseOptionKey
type FuseOptionKey<T> = FuseOptionKeyObject<T> | string | string[];
type FuseOptionKeyObject
type FuseOptionKeyObject<T> = { name: string | string[]; weight?: number; getFn?: (obj: T) => ReadonlyArray<string> | string;};
Example 1
{name: 'title',weight: 0.7}
type FuseResult
type FuseResult<T> = { item: T; refIndex: number; score?: number; matches?: ReadonlyArray<FuseResultMatch>;};
type FuseResultMatch
type FuseResultMatch = { indices: ReadonlyArray<RangeTuple>; key?: string; refIndex?: number; value?: string;};
type FuseSearchOptions
type FuseSearchOptions = { limit: number;};
type FuseSortFunction
type FuseSortFunction = (a: FuseSortFunctionArg, b: FuseSortFunctionArg) => number;
type FuseSortFunctionArg
type FuseSortFunctionArg = { idx: number; item: FuseSortFunctionItem; score: number; matches?: (FuseSortFunctionMatch | FuseSortFunctionMatchList)[];};
type FuseSortFunctionItem
type FuseSortFunctionItem = { [key: string]: { $: string } | { $: string; idx: number }[];};
Example 1
{title: { '$': "Old Man's War" },'author.firstName': { '$': 'Codenar' }}Example 2
{tags: [{ $: 'nonfiction', idx: 0 },{ $: 'web development', idx: 1 },]}
type FuseSortFunctionMatch
type FuseSortFunctionMatch = { score: number; key: string; value: string; indices: ReadonlyArray<number>[];};
Example 1
{score: 0.001,key: 'author.firstName',value: 'Codenar',indices: [ [ 0, 3 ] ]}
type FuseSortFunctionMatchList
type FuseSortFunctionMatchList = FuseSortFunctionMatch & { idx: number;};
Example 1
{score: 0,key: 'tags',value: 'nonfiction',idx: 1,indices: [ [ 0, 9 ] ]}
type RangeTuple
type RangeTuple = [number, number];
Denotes the start/end indices of a match
Example 1
const startIndex = 0;const endIndex = 5;const range: RangeTuple = [startIndex, endIndex];
type RecordEntry
type RecordEntry = { [key: string]: RecordEntryObject | RecordEntryArrayItem;};
type RecordEntryArrayItem
type RecordEntryArrayItem = ReadonlyArray<RecordEntryObject & { i: number }>;
Example 1
'author.tags.name': [{'v': 'pizza lover','i': 2,'n: 0.7071067811865475}
type RecordEntryObject
type RecordEntryObject = { /** The text value */ v: string; /** The field-length norm */ n: number;};
Example 1
title: {'$': "Old Man's War",'n': 0.5773502691896258}
Package Files (1)
Dependencies (0)
No dependencies.
Dev Dependencies (35)
- @babel/cli
- @babel/core
- @babel/eslint-parser
- @babel/plugin-proposal-object-rest-spread
- @babel/plugin-syntax-import-assertions
- @babel/preset-env
- @babel/preset-typescript
- @commitlint/cli
- @commitlint/config-conventional
- @monaco-editor/loader
- @rollup/plugin-babel
- @rollup/plugin-node-resolve
- @rollup/plugin-replace
- @sapphire/stopwatch
- @sapphire/utilities
- @snippetors/vuepress-plugin-tabs
- @vuepress/plugin-google-analytics
- @vuepress/plugin-pwa
- @vuepress/plugin-register-components
- @vuepress/plugin-search
- babel-loader
- eslint
- eslint-config-prettier
- husky
- monaco-editor
- prettier
- replace-in-file
- rollup
- rollup-plugin-dts
- standard-version
- terser
- typescript
- vitest
- vuepress
- vuepress-plugin-google-adsense2
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/fuse.js
.
- Markdown[](https://www.jsdocs.io/package/fuse.js)
- HTML<a href="https://www.jsdocs.io/package/fuse.js"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3894 ms. - Missing or incorrect documentation? Open an issue for this package.