Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/stores/ui.store.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,49 @@ export function getInitials(name: string) {
.join("");
}

function normalizeSearchText(value: string) {
return value
.normalize("NFC")
.toLowerCase()
.replace(/[\s.-]+/g, " ");
}

function getTextMatchTier(value: string | undefined, query: string) {
if (!value) {
return 4;
}

const normalizedValue = normalizeSearchText(value);
if (normalizedValue === query) {
return 0;
}

if (normalizedValue.startsWith(query)) {
return 1;
}

if (normalizedValue.split(/\s+/).some((word) => word.startsWith(query))) {
return 2;
}

return normalizedValue.includes(query) ? 3 : 4;
}

export function getSearchMatchTier(
item: Pick<Item, "name" | "localizedName">,
rawQuery: string,
) {
const query = normalizeSearchText(rawQuery).trim();
if (!query) {
return 0;
}

return Math.min(
getTextMatchTier(item.name, query),
getTextMatchTier(item.localizedName, query),
);
}

export function traverse(
bookmarks: Array<{
title: string;
Expand Down
33 changes: 12 additions & 21 deletions src/stores/ui.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
formatExpressionResult,
type BookmarkNode,
getInitials,
getSearchMatchTier,
normalizeCalculatorQuery,
parseFlightIdentifier,
parseTimezoneConversion,
Expand Down Expand Up @@ -144,6 +145,7 @@ const itemsThatShouldShowWindow = [

type RankedItem = Item & {
score?: number;
matchTier?: number;
};

export const createUIStore = (root: IRootStore) => {
Expand All @@ -166,29 +168,15 @@ export const createUIStore = (root: IRootStore) => {
};

// ponytail: coarse relevance tier so usage history can only reorder items of
// comparable match quality. A fuzzy hit ("Cap" for "cal") never outranks a
// direct prefix match ("Calendar") just because it was selected once.
const getMatchTier = (item: Pick<Item, "name">) => {
const query = store.query.trim().toLowerCase();
if (!query) {
return 0;
}

const name = item.name.toLowerCase();
if (name.startsWith(query)) {
return 0;
}

// prefix of any word, e.g. "code" matching "Visual Studio Code"
if (name.split(/\s+/).some((word) => word.startsWith(query))) {
return 1;
}

return name.includes(query) ? 2 : 3;
};
// comparable match quality. Exact, prefix, word-prefix, substring, and fuzzy
// matches remain separate regardless of selection history.
const getMatchTier = (item: Pick<Item, "name" | "localizedName">) =>
getSearchMatchTier(item, store.query);

const compareRankedItems = (left: RankedItem, right: RankedItem) => {
const tierDiff = getMatchTier(left) - getMatchTier(right);
const tierDiff =
(left.matchTier ?? getMatchTier(left)) -
(right.matchTier ?? getMatchTier(right));
if (tierDiff !== 0) {
return tierDiff;
}
Expand Down Expand Up @@ -545,6 +533,9 @@ export const createUIStore = (root: IRootStore) => {
prefix: true,
fuzzy: true,
}) as unknown as RankedItem[];
for (const result of results) {
result.matchTier = getMatchTier(result);
}

results.sort(compareRankedItems);

Expand Down