Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerSlashSkillSearch.ts

51 lines1.5 KB

1import type { ServerProviderSkill } from "@t3tools/contracts";
2import { scoreQueryMatch } from "@t3tools/shared/searchRanking";
3​
4/** Lower is better; `null` when the skill does not match. */
5export function scoreSlashSkillQuery(skill: ServerProviderSkill, query: string): number | null {
6 if (!skill.enabled) return null;
7 const normalizedQuery = query.toLowerCase();
8 const skillQuery =
9 normalizedQuery === "skill"
10 ? ""
11 : normalizedQuery.startsWith("skill:")
12 ? normalizedQuery.slice("skill:".length)
13 : normalizedQuery;
14 if (!skillQuery) return 0;
15 const scores = [
16 scoreQueryMatch({
17 value: skill.name.toLowerCase(),
18 query: skillQuery,
19 exactBase: 0,
20 prefixBase: 2,
21 boundaryBase: 4,
22 includesBase: 6,
23 boundaryMarkers: ["-", "_", "/"],
24 }),
25 scoreQueryMatch({
26 value: skill.displayName?.toLowerCase() ?? "",
27 query: skillQuery,
28 exactBase: 1,
29 prefixBase: 3,
30 boundaryBase: 5,
31 includesBase: 7,
32 }),
33 scoreQueryMatch({
34 value: skill.shortDescription?.toLowerCase() ?? "",
35 query: skillQuery,
36 exactBase: 20,
37 prefixBase: 22,
38 boundaryBase: 24,
39 includesBase: 26,
40 }),
41 scoreQueryMatch({
42 value: skill.description?.toLowerCase() ?? "",
43 query: skillQuery,
44 exactBase: 30,
45 prefixBase: 32,
46 boundaryBase: 34,
47 includesBase: 36,
48 }),
49 ].filter((score): score is number => score !== null);
50 return scores.length > 0 ? Math.min(...scores) : null;