Skip to content

Commit

Permalink
refactor(qbox/resource): improve search
Browse files Browse the repository at this point in the history
  • Loading branch information
D4isDAVID committed Dec 19, 2024
1 parent 29954e2 commit 8b25d9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/components/qbox/commands/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { ChatInputCommand } from '/components/types.js';
import { fetchJson } from '/utils/http-request.js';
import { mapChatInputOptionValues } from '/utils/interactions.js';
import { search } from '/utils/search.js';

interface GitHubRepository {
subscribers_count: number;
Expand Down Expand Up @@ -140,9 +141,10 @@ export const resourceCommand = {
interaction.id,
interaction.token,
{
choices: resources
.filter((r) => r.startsWith(value))
.map((r) => ({ name: r, value: r })),
choices: search(resources, value).map((r) => ({
name: r,
value: r,
})),
},
);
},
Expand Down
11 changes: 11 additions & 0 deletions src/utils/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function search(values: string[], substr: string): string[] {
substr = substr.toLowerCase();

const startsWith = values.filter((s) => s.toLowerCase().startsWith(substr));
const includes = values.filter((s) => {
s = s.toLowerCase();
return s.includes(substr) && !s.startsWith(substr);
});

return [...startsWith.sort(), ...includes.sort()];
}

0 comments on commit 8b25d9b

Please sign in to comment.