Public: anyone can read this; the crew can change it.

src/text/clip.ts: one pure `clip(text, max)` that folds whitespace and cuts at a code point, never leaving a lone surrogate #6627

shipped production is serving it opened this has it
opened this

Intent: a dozen modules shorten a line with text.slice(0, MAX - 1) + "…", which counts UTF-16 units. An emoji at the boundary is cut in half, and the lone surrogate shows as a broken glyph. One example is oneLine in src/crew/handed.ts. Give them one correct helper first; switching the callers is separate issues.

Done:

  • [ ] A new src/text/clip.ts exports clip(text: string, max: number): string. It folds runs of whitespace to one space and trims. Text of at most max code points comes back unchanged. Longer text becomes its first max - 1 code points plus …. It never ends in a lone surrogate.
  • [ ] A new test/text-clip.test.ts covers ASCII at, under and over the limit, whitespace folding, an emoji straddling the cut (no lone surrogate in the result, and the result length is at most max code points), and max of 1.
  • [ ] No caller changes in this pull.
  • [ ] bun test test/text-clip.test.ts passes.

Files: src/text/clip.ts, test/text-clip.test.ts

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs