-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support fuzzy-search, refactor code structure ✨
- Loading branch information
Showing
11 changed files
with
596 additions
and
161 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { FastAlfred } from 'fast-alfred' | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
import contacts from 'node-mac-contacts' | ||
import { CACHE_CONTACTS_KEY, CACHE_TTL } from '@common/constants.js' | ||
import { AuthStatus } from '@models/auth-status.enum.js' | ||
import type { IContact } from '@models/contact.model.js' | ||
|
||
export function isAuth(): boolean { | ||
const status: AuthStatus = contacts.getAuthStatus() | ||
return status === AuthStatus.Authorized | ||
} | ||
|
||
export function requestAuth(): void { | ||
contacts.requestAccess() | ||
} | ||
|
||
export function getContacts(alfredClient: FastAlfred): IContact[] { | ||
const isHasAccess: boolean = isAuth() | ||
if (!isHasAccess) { | ||
requestAuth() | ||
return [] | ||
} | ||
|
||
const cacheContacts: IContact[] = alfredClient.cache.get(CACHE_CONTACTS_KEY) ?? contacts.getAllContacts() | ||
if (cacheContacts) { | ||
return cacheContacts | ||
} | ||
|
||
const fetchedContacts = contacts.getAllContacts() | ||
alfredClient.cache.setWithTTL(CACHE_CONTACTS_KEY, fetchedContacts, { maxAge: CACHE_TTL }) | ||
|
||
return fetchedContacts | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import type { IContact } from '@models/contact.model.js' | ||
|
||
type SearchField = keyof IContact | ||
export const SEARCH_FIELDS_CONFIG: SearchField[] = ['firstName', 'lastName', 'phoneNumbers'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Fuse from 'fuse.js' | ||
import type { IContact } from '@models/contact.model.js' | ||
import { SEARCH_FIELDS_CONFIG } from './search.config.js' | ||
|
||
export function searchContacts(contacts: IContact[], searchTerm: string, limit: number): IContact[] { | ||
const fuse = new Fuse(contacts, { | ||
keys: SEARCH_FIELDS_CONFIG, | ||
isCaseSensitive: false, | ||
shouldSort: true, | ||
threshold: 0.4, | ||
}) | ||
|
||
const res = fuse.search(searchTerm, { limit }) | ||
|
||
return res.map((item) => item.item) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters