text

package
v1.0.0-alpha.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package text provides text manipulation utilities.

Overview

The text package provides common text manipulation functions used throughout the codebase. It focuses on Unicode-safe operations that work correctly with multi-byte characters.

Functions

TruncateAtWord:

Truncates text to a maximum character count, breaking at word boundaries when possible. This is useful for generating summaries, preview text, or any UI that needs to display text within a fixed width.

text.TruncateAtWord("Hello world, this is a long sentence", 20)
// Returns: "Hello world, this..."

Key behaviors:

  • Works with runes (Unicode code points), not bytes
  • Breaks at last space before limit when possible
  • Falls back to hard truncation if no space found
  • Appends "..." suffix when truncation occurs
  • Returns original text unchanged if already short enough

Unicode Safety

All functions operate on runes rather than bytes, ensuring correct behavior with multi-byte UTF-8 characters:

// Japanese text - each character is 3 bytes
text.TruncateAtWord("こんにちは世界", 5)
// Returns: "こん..." (correct character count, not byte count)

Usage Example

// Generate preview text for a list of items
for _, item := range items {
    preview := text.TruncateAtWord(item.Description, 100)
    fmt.Printf("- %s: %s\n", item.Title, preview)
}

// Community summary truncation
summary := text.TruncateAtWord(community.Summary, 500)

Thread Safety

All functions are pure (no shared state) and safe for concurrent use.

See Also

Related packages:

Package text provides text manipulation utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TruncateAtWord

func TruncateAtWord(s string, maxChars int) string

TruncateAtWord truncates text to maxChars characters (runes, not bytes), breaking at the last word boundary before the limit. Returns original text unchanged if shorter than maxChars. Appends "..." if truncation occurred.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL