file-match.ts
This file
Path
apps/web/src/features/code/file-match.ts
40 lines · 1011 bytes · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 1–40why, one step back1/** Go to file's matching: every word of the query in the path, a match in the file's own name first. */2export const FILES_SHOWN = 50;34const nameOf = (path: string) => path.split("/").at(-1) ?? path;56export const filesMatching = (7 paths: readonly string[],8 query: string,9 limit: number = FILES_SHOWN10): readonly string[] => {11 const words = query12 .toLowerCase()13 .split(/\s+/u)14 .filter((word) => word !== "");1516 const scored = paths.flatMap((path) => {17 const lower = path.toLowerCase();1819 if (!words.every((word) => lower.includes(word))) {20 return [];21 }2223 const name = nameOf(lower);2425 const inName =26 words.length > 0 && words.every((word) => name.includes(word));2728 return [{ inName, path }];29 });3031 return scored32 .toSorted(33 (one, other) =>34 Number(other.inName) - Number(one.inName) ||35 one.path.length - other.path.length ||36 one.path.localeCompare(other.path)37 )38 .slice(0, limit)39 .map(({ path }) => path);40};