Skip to content

Commit

Permalink
feat: support dynamic fuzzy threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
Avivbens committed Jul 2, 2024
1 parent 080b3ee commit 357bbeb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
25 changes: 25 additions & 0 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,31 @@ https://github.com/Avivbens/alfred-engage-contact</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>3.0.6</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 {
COUNTRY_CODE = 'country_code',
SLICE_AMOUNT = 'slice_amount',
FUZZY_THRESHOLD = 'fuzzy_threshold',
}
7 changes: 6 additions & 1 deletion src/main/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ import { searchContacts } from '@services/search.service'
parser: Number,
})

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

const contacts: IContact[] = getContacts(alfredClient)

const filteredContacts = await searchContacts(contacts, searchTerm, sliceAmount)
const filteredContacts = await searchContacts(contacts, searchTerm, sliceAmount, fuzzyThreshold)

const items: AlfredScriptFilter['items'] = filteredContacts
.map(({ firstName, lastName, phoneNumbers, emailAddresses }: IContact) => {
Expand Down
9 changes: 7 additions & 2 deletions src/services/search.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import type { IContact } from '@models/contact.model'
import { SEARCH_FIELDS_CONFIG } from './search.config'

export async function searchContacts(contacts: IContact[], searchTerm: string, limit: number): Promise<IContact[]> {
export async function searchContacts(
contacts: IContact[],
searchTerm: string,
limit: number,
threshold: number,
): Promise<IContact[]> {
const Fuse = (await import('fuse.js/min-basic')).default

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

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

0 comments on commit 357bbeb

Please sign in to comment.