Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dynamic fuzzy threshold #13

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,31 @@ https://github.com/Avivbens/alfred-search-bookmark</string>
<key>variable</key>
<string>slice_amount</string>
</dict>
<dict>
<key>config</key>
<dict>
<key>defaultvalue</key>
<integer>4</integer>
<key>markercount</key>
<integer>10</integer>
<key>maxvalue</key>
<integer>10</integer>
<key>minvalue</key>
<integer>0</integer>
<key>onlystoponmarkers</key>
<true></true>
<key>showmarkers</key>
<true></true>
</dict>
<key>description</key>
<string>A number between 1-10. A higher number would be less accurate, but more dynamic.</string>
<key>label</key>
<string>Fuzzy search accuracy</string>
<key>type</key>
<string>slider</string>
<key>variable</key>
<string>fuzzy_threshold</string>
</dict>
</array>
<key>version</key>
<string>2.2.1</string>
Expand Down
1 change: 1 addition & 0 deletions src/common/variables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum Variables {
PROFILES_LOOKUP = 'lookfor_profiles',
SLICE_AMOUNT = 'slice_amount',
FUZZY_THRESHOLD = 'fuzzy_threshold',
}
7 changes: 6 additions & 1 deletion src/main/bookmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import { searchBookmarks } from '@services/search.service'
parser: Number,
})

const fuzzyThreshold: number = alfredClient.env.getEnv(Variables.FUZZY_THRESHOLD, {
defaultValue: 0.4,
parser: (input) => Number(input) / 10,
})

const profiles: string[] = profilesConfig.split(',')

try {
Expand All @@ -24,7 +29,7 @@ import { searchBookmarks } from '@services/search.service'
alfredClient.cache.setWithTTL(CACHE_BOOKMARKS_KEY, bookmarks, { maxAge: CACHE_TTL })
}

const filteredBookmarks = await searchBookmarks(bookmarks, alfredClient.input, sliceAmount)
const filteredBookmarks = await searchBookmarks(bookmarks, alfredClient.input, sliceAmount, fuzzyThreshold)

const items: AlfredScriptFilter['items'] = filteredBookmarks.map(({ name, url, profile }) => {
const subtitle = `[${profile}] - ${url}`
Expand Down
3 changes: 2 additions & 1 deletion src/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ export async function searchBookmarks(
bookmarks: IUIBookmark[],
searchTerm: string,
limit: number,
threshold: number,
): Promise<IUIBookmark[]> {
const Fuse = (await import('fuse.js/min-basic')).default

const fuse = new Fuse(bookmarks, {
keys: SEARCH_FIELDS_CONFIG,
isCaseSensitive: false,
shouldSort: true,
threshold: 0.4,
threshold,
})

const res = fuse.search(searchTerm, { limit })
Expand Down
Loading