All files / lib textExcerpt.ts

100% Statements 14/14
33.33% Branches 1/3
100% Functions 1/1
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 151x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * A short, word-boundary-safe excerpt of free text. Used wherever a title-
 * shaped label is needed but none was extracted — e.g. a paste-sourced
 * posting with no `analysis.title` (spec FR-020a: list surfaces must never
 * fall back to blank, a URL-shaped placeholder, or the raw `paste:<sha256>`
 * key).
 */
export function excerptOf(text: string, maxLength = 80): string {
  const collapsed = text.replace(/\s+/g, " ").trim();
  if (collapsed.length <= maxLength) return collapsed;
  const cut = collapsed.slice(0, maxLength);
  const lastSpace = cut.lastIndexOf(" ");
  return `${(lastSpace > maxLength / 2 ? cut.slice(0, lastSpace) : cut).trim()}…`;
}