Skip to the content

Capsid menu

app/lib/search/sort.mjs

24 lines and 3 functions in DrDustinEdwards/dustinedwards-info, at commit 1590ba4 on 2026-10-24. This file on GitHub.

Functions

The functions of sort.mjs
FunctionLinesComplexityCoveredCRAPMutation
parseSort11 to 132100%OkCRAP 2No data: no mutants in this code
applySort24 to 272100%OkCRAP 2No data: no mutants in this code
<anonymous:26>26 to 263100%OkCRAP 3No data: no mutants in this code

Imports

It imports no other module of this repo. Imported by app/lib/search/search.server.ts.

Source

// Relevance stays the default: the best pages here are old papers, which a date-first list buries.

/**
 * @typedef {"relevance" | "date"} SearchSort
 */

/**
 * @param {string | null | undefined} value
 * @returns {SearchSort}
 */
export function parseSort(value) {
  return value === "date" ? "date" : "relevance";
}

/**
 * Undated rows sort last via -Infinity, not 0, which would misplace a pre-epoch date. The caller
 * paginates after sorting, since sorting one page of ten reads as a broken list.
 *
 * @template {{ publishAt: number | null }} T
 * @param {T[]} hits
 * @param {SearchSort} sort
 * @returns {T[]}
 */
export function applySort(hits, sort) {
  if (sort !== "date") return hits;
  return [...hits].sort((a, b) => (b.publishAt ?? -Infinity) - (a.publishAt ?? -Infinity));
}