Open History to see the last commit
1export interface ComposerPullRequestMatch {
2 readonly number: number;
3 readonly projectId: string;
4 readonly repository: string;
5 readonly updatedAt: string;
6}
7
8/**
9 * Pull requests matching the numeric fragment typed after `#`, de-duplicated. An exact number
10 * match always outranks a substring match so the result limit can never drop the pull request
11 * the user typed in full; the rest stay newest first.
12 */
13export function filterComposerPullRequestMatches<Entry extends ComposerPullRequestMatch>(input: {
14 readonly entries: ReadonlyArray<Entry>;
15 readonly projectId: string;
16 readonly repository: string;
17 readonly query: string;
18 readonly limit: number;
19}): ReadonlyArray<Entry> {
20 const repository = input.repository.trim().toLowerCase();
21 const matchingEntries = input.entries.filter(
22 (entry) =>
23 entry.projectId === input.projectId &&
24 entry.repository.trim().toLowerCase() === repository &&
25 String(entry.number).includes(input.query),
26 );
27 const uniqueEntries = new Map<number, Entry>();
28 for (const entry of matchingEntries) {
29 if (!uniqueEntries.has(entry.number)) {
30 uniqueEntries.set(entry.number, entry);
31 }
32 }
33 const isExactMatch = (entry: Entry) => String(entry.number) === input.query;
34 // `.sort()` on a copy, not `.toSorted()`: this runs on Hermes, which has no ES2023 array
35 // methods, and reaching for one here crashed the composer as the suggestions loaded.
36 return [...uniqueEntries.values()]
37 .sort((left, right) => {
38 const exactness = Number(isExactMatch(right)) - Number(isExactMatch(left));
39 return exactness !== 0 ? exactness : right.updatedAt.localeCompare(left.updatedAt);
40 })
41 .slice(0, input.limit);
42}