Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [Assistants] Filter on names * Add `$text` index on `assistant.name` (#844) * add maxlength * better experience * use `$meta: "textScore"` * Update src/routes/assistants/+page.server.ts Co-authored-by: Eliott C. <[email protected]> * null, not undefined * [Assistants] Filter on names (using searchTokens) (#873) Filter with `searchTokens` * input * rm extra whitespace * hide UI before migration * rm ad-hoc migration --------- Co-authored-by: Eliott C. <[email protected]> Co-authored-by: Nathan Sarrazin <[email protected]> Co-authored-by: Victor Mustar <[email protected]>
- Loading branch information
1 parent
714ff2c
commit 10dbbd6
Showing
8 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
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,17 @@ | ||
/** | ||
* A debounce function that works in both browser and Nodejs. | ||
* For pure Nodejs work, prefer the `Debouncer` class. | ||
*/ | ||
export function debounce<T extends unknown[]>( | ||
callback: (...rest: T) => unknown, | ||
limit: number | ||
): (...rest: T) => void { | ||
let timer: ReturnType<typeof setTimeout>; | ||
|
||
return function (...rest) { | ||
clearTimeout(timer); | ||
timer = setTimeout(() => { | ||
callback(...rest); | ||
}, limit); | ||
}; | ||
} |
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,33 @@ | ||
const PUNCTUATION_REGEX = /\p{P}/gu; | ||
|
||
function removeDiacritics(s: string, form: "NFD" | "NFKD" = "NFD"): string { | ||
return s.normalize(form).replace(/[\u0300-\u036f]/g, ""); | ||
} | ||
|
||
export function generateSearchTokens(value: string): string[] { | ||
const fullTitleToken = removeDiacritics(value) | ||
.replace(PUNCTUATION_REGEX, "") | ||
.replaceAll(/\s+/g, "") | ||
.toLowerCase(); | ||
return [ | ||
...new Set([ | ||
...removeDiacritics(value) | ||
.split(/\s+/) | ||
.map((word) => word.replace(PUNCTUATION_REGEX, "").toLowerCase()) | ||
.filter((word) => word.length), | ||
...(fullTitleToken.length ? [fullTitleToken] : []), | ||
]), | ||
]; | ||
} | ||
|
||
function escapeForRegExp(s: string): string { | ||
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string | ||
} | ||
|
||
export function generateQueryTokens(query: string): RegExp[] { | ||
return removeDiacritics(query) | ||
.split(/\s+/) | ||
.map((word) => word.replace(PUNCTUATION_REGEX, "").toLowerCase()) | ||
.filter((word) => word.length) | ||
.map((token) => new RegExp(`^${escapeForRegExp(token)}`)); | ||
} |
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