Skip to content

Truncating by word/char length (and maybe other units - sentences/lines/etc?) #6456

Open
@lionel-rowe

Description

@lionel-rowe

Is your feature request related to a problem? Please describe.

Sometimes it's useful to truncate strings and other things.

Words (max length 10 chars, truncation chars = "..."):

"Hello world" => "Hello..." // whole words only
"Hello" => no change

Chars (max length 10 chars, truncation chars = "..."):

"Hello world" => "Hello w..." // max length accounts for chars used for truncation
"Hello" => no change

Describe the solution you'd like

New functionality such as truncateWords and/or truncateGraphemes under text.

Describe alternatives you've considered

  • What do "words" and "chars" mean (cf: Intl.Segmenter with granularity of "words"/"graphemes")
  • What options should be available? At a minimum, what text/items to use as truncation placeholder, as well as max length (which may not be in the same unit as the base granularity, i.e. when truncating words, you probably care more about the char/grapheme length than the word length)
  • Are other granularities useful for text? E.g. sentences, lines?
  • For perf reasons, the general unit of measurement for the text functions could be Unicode code points, with the segmenter only being used at the boundary. Alternatively, the "Unicode width" could be used as a heuristic, though it's only a very rough indication of physical width in non-TTY environments, especially in non-monospaced fonts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions