Skip to content

Commit

Permalink
fix(libs): ignoreLocation by default when using fuzzySearch
Browse files Browse the repository at this point in the history
without this option, fuse searches in the first (threshold x 100) caracters only.
  • Loading branch information
thoomasbro committed Aug 30, 2023
1 parent 92f1025 commit 1cee6f2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .storybook/data/species.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"code": "RIX",
"name": "Rangia spp"
"name": "Rangia spp very long name with a special string you want to look for at the end of the string to see if it is cutted or not in the index balbuzard guédalup"
},
{
"code": "MTF",
Expand Down
17 changes: 14 additions & 3 deletions src/libs/CustomSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ export type CustomSearchOptions = Partial<{
* instead of returning both by default (`false`).
*/
isStrict: boolean
/**
* By default, location is set to 0 and distance to 100
* When isStrict is false, for something to be considered a match, it would have to be within
* (threshold) 0.4 x (distance) 100 = 40 characters away from the expected location 0. (i.e. the first 40 characters)
* When true, search will ignore location and distance, so it won't matter where in the string the pattern appears.
*
* @default true
* @see https://www.fusejs.io/concepts/scoring-theory.html#scoring-theory
*/
shouldIgnoreLocation: boolean
/**
* At what point does the match algorithm give up.
*
* @default 0.6
* @default 0.4
* @description
* A threshold of `0.0` requires a perfect match (of both letters and location),
* a threshold of `1.0` would match anything.
Expand Down Expand Up @@ -69,7 +79,8 @@ export class CustomSearch<T extends Record<string, any> = Record<string, any>> {
isCaseSensitive = false,
isDiacriticSensitive = false,
isStrict = false,
threshold = 0.6
shouldIgnoreLocation = true,
threshold = 0.4
}: CustomSearchOptions = {}
) {
const maybeCache = cacheKey ? FUSE_SEARCH_CACHE[cacheKey] : undefined
Expand All @@ -83,7 +94,7 @@ export class CustomSearch<T extends Record<string, any> = Record<string, any>> {
this.#fuse = new Fuse(
normalizedCollection,
// eslint-disable-next-line @typescript-eslint/naming-convention
{ isCaseSensitive, keys, threshold, useExtendedSearch: isStrict },
{ ignoreLocation: shouldIgnoreLocation, isCaseSensitive, keys, threshold, useExtendedSearch: isStrict },
maybeCache ? Fuse.parseIndex<T>(maybeCache.fuseSearchIndex) : undefined
)
this.#isDiacriticSensitive = isDiacriticSensitive
Expand Down
2 changes: 1 addition & 1 deletion stories/fields/Search/WithCustomSearch.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function WithCustomSearch(props: SearchProps) {
weight: 0.1
}
],
{ isStrict: true }
{ isStrict: false, shouldIgnoreLocation: true }
)
)

Expand Down

0 comments on commit 1cee6f2

Please sign in to comment.