/** * TASK-0103 — the file picker's pure filter/sort logic (Requirements §4, * Tests: "picker filter/sort logic"). * * Kept out of the component so it is unit-testable without a DOM: the picker * fetches a server-ordered page of files, then applies a client-side name * filter (spec: "search box (client filter by name)") and a sort, then maps * the result to the grid tiles. */ import type { FileListItem } from "./api"; export type PickerSort = "updated" | "name" | "size" | "date"; /** * Case-insensitive substring match on the file name. An empty / whitespace * query returns the input unchanged (so the grid shows everything). */ export function filterByName(files: FileListItem[], query: string): FileListItem[] { const q = query.trim().toLowerCase(); if (!q) return files; return files.filter((f) => f.name.toLowerCase().includes(q)); } /** * Sort a picker result. `name` is a locale-aware ascending compare; the rest * are descending (most-recent / largest first) — the natural "show me the * interesting ones" order for a picker. Returns a NEW array (the caller's * input is not mutated). */ export function sortPickerFiles(files: FileListItem[], sort: PickerSort): FileListItem[] { const arr = [...files]; switch (sort) { case "name": arr.sort((a, b) => a.name.localeCompare(b.name)); break; case "size": arr.sort((a, b) => b.size - a.size); break; case "date": arr.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); break; case "updated": default: arr.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()); } return arr; }