β¬ οΈ Back to Table of Contents
π useFuse¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 2 |
| π¦ Imports | 11 |
| π’ Vue Composition API | 3 |
| π Interfaces | 2 |
| π Type Aliases | 1 |
π Table of Contents¶
π οΈ File Location:¶
π packages/integrations/useFuse/index.ts
π¦ Imports¶
| Name | Source |
|---|---|
FuseResult |
fuse.js |
IFuseOptions |
fuse.js |
ComputedRef |
vue |
MaybeRefOrGetter |
vue |
Ref |
vue |
Fuse |
fuse.js |
computed |
vue |
shallowRef |
vue |
toValue |
vue |
triggerRef |
vue |
watch |
vue |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
watch |
watch | none | none |
watch |
watch | none | none |
computed |
computed | none | none |
Functions¶
useFuse(search: MaybeRefOrGetter<string>, data: MaybeRefOrGetter<DataItem[]>, options: MaybeRefOrGetter<UseFuseOptions<DataIteβ¦): UseFuseReturn<DataItem>¶
Parameters:
searchMaybeRefOrGetter<string>dataMaybeRefOrGetter<DataItem[]>optionsMaybeRefOrGetter<UseFuseOptions<DataItem>>
Returns: UseFuseReturn<DataItem>
Calls:
toValue (from vue)shallowRef (from vue)createFusewatch (from vue)fuse.value.setCollectiontriggerRef (from vue)computed (from vue)toValue(data).mapfuse.value.search
Internal Comments:
// This will also be recomputed when `data` changes, as it causes a change
// to the Fuse instance, which is tracked here.
Code
export function useFuse<DataItem>(
search: MaybeRefOrGetter<string>,
data: MaybeRefOrGetter<DataItem[]>,
options?: MaybeRefOrGetter<UseFuseOptions<DataItem>>,
): UseFuseReturn<DataItem> {
const createFuse = () => {
return new Fuse(
toValue(data) ?? [],
toValue(options)?.fuseOptions,
)
}
const fuse = shallowRef(createFuse())
watch(
() => toValue(options)?.fuseOptions,
() => { fuse.value = createFuse() },
{ deep: true },
)
watch(
() => toValue(data),
(newData) => {
fuse.value.setCollection(newData)
triggerRef(fuse)
},
{ deep: true },
)
const results: ComputedRef<FuseResult<DataItem>[]> = computed(() => {
const resolved = toValue(options)
// This will also be recomputed when `data` changes, as it causes a change
// to the Fuse instance, which is tracked here.
if (resolved?.matchAllWhenSearchEmpty && !toValue(search))
return toValue(data).map((item, index) => ({ item, refIndex: index }))
const limit = resolved?.resultLimit
return fuse.value.search(toValue(search), (limit ? { limit } : undefined))
})
return {
fuse,
results,
}
}
Internal helpers¶
Declared inside another function in this file.
createFuse(): any¶
Returns: any
Calls:
toValue (from vue)
Interfaces¶
UseFuseOptions<T>¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
fuseOptions |
FuseOptions<T> |
β | not shown |
resultLimit |
number |
β | not shown |
matchAllWhenSearchEmpty |
boolean |
β | not shown |
UseFuseReturn<DataItem>¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
fuse |
Ref<Fuse<DataItem>> |
β | not shown |
results |
ComputedRef<FuseResult<DataItem>[]> |
β | not shown |
Type Aliases¶
FuseOptions<T>¶
Generated by Syntax Scribe