NGU-Web/src/lib/text.ts
Zaldimmar 5274e09571 Clean up code found in the audit
- Replace every em-dash in src and server/src with a colon, comma or hyphen
- Give every relative import its file extension (.jsx imports of .tsx fixed)
- Delete old-historyMock.ts, unused exports, unused CSS utilities, the
  unused @ alias and the fallback option nothing passed
- One copy each of initials, capitalize, byName and the server's marks()
- Move the organization, chapter and event hooks from src/data to src/lib
- Schema sync always throws on a mismatch (strict was never turned off)
- Replace the placeholder README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:02:03 -05:00

26 lines
1.3 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
TEXT
Shared helpers for showing a short piece of longer text.
preview() is for anywhere a paragraph is cut to fit a row title,
a list cell or a card: it collapses whitespace and, when it has
to cut, stops at the last whole word and ends with an ellipsis,
so a shortened line always reads as shortened. Text that already
fits comes back unchanged, with no ellipsis.
═══════════════════════════════════════════════════════════════ */
export function preview(text: string | null | undefined, max: number): string {
const flat = (text ?? '').replace(/\s+/g, ' ').trim()
if (flat.length <= max) return flat
const cut = flat.slice(0, max)
const lastSpace = cut.lastIndexOf(' ')
// Break on a word if one ends in the back half; otherwise a single
// long word would leave almost nothing.
const clean = lastSpace > max / 2 ? cut.slice(0, lastSpace) : cut
return `${clean.replace(/[\s.,;:!?]+$/, '')}…`
}
export const capitalize = (word: string) =>
word ? word.charAt(0).toUpperCase() + word.slice(1) : ''