str
Unicode-aware string utilities for Gleam
Production-ready Gleam library for Unicode-aware string operations. All operations work at grapheme cluster boundaries, correct behaviour for emoji, ZWJ sequences, combining marks, and flags.
Features
| Category | Highlights |
|---|---|
| Grapheme-Aware | take, drop, length, reverse, chunk β all grapheme-correct |
| Case Conversions | snake_case, camelCase, kebab-case, PascalCase, Title Case |
| Slug Generation | slugify with token limits, custom separators, Unicode preservation |
| Search & Replace | index_of, last_index_of, replace_first/last, contains_any/all |
| Validation | is_uppercase/lowercase/title_case, is_ascii/hex/numeric/alpha |
| Escaping | escape_html, unescape_html, escape_regex |
| Similarity |
Levenshtein distance, normalized similarity, hamming_distance |
| Splitting | splitn, partition, rpartition, chunk, lines, words |
| Padding | pad_left, pad_right, center, fill |
| Minimal deps | No OTP requirement β works on Erlang and JavaScript targets |
Installation
gleam add strQuick Start
import str
// Grapheme-safe truncation preserves emoji sequences
str.truncate("Hello π©βπ©βπ§βπ¦ World", 10, "...")
// β "Hello π©βπ©βπ§βπ¦..."
// ASCII transliteration and URL-friendly slugs
str.slugify("CrΓ¨me BrΓ»lΓ©e β Recipe 2025!")
// β "creme-brulee-recipe-2025"
// Case conversions
str.to_camel_case("hello world") // β "helloWorld"
str.to_snake_case("Hello World") // β "hello_world"
// Grapheme-aware search β counts clusters, not bytes
str.index_of("π¨βπ©βπ§βπ¦ family test", "family")
// β Ok(2)
// Normalized Levenshtein similarity
str.similarity("hello", "hallo")
// β 0.8Documentation
| Document | Description |
|---|---|
| API Reference | Complete function reference with examples |
| Examples | Integration snippets and patterns |
| OTP Integration | NFC/NFD normalization via Erlang |
| Core internals | Grapheme-aware core operations |
| Extra internals | ASCII folding and slug generation |
| Tokenizer | Pure-Gleam grapheme tokenizer reference |
Module Guide
| Module | Use when | Import |
|---|---|---|
str | Everything β recommended entry point | import str |
str/advanced | Explicit KMP algorithm control, map caching | import str/advanced |
str/config | Tune search heuristic thresholds | import str/config |
str/
βββ str.gleam # Main module β complete public API
βββ advanced.gleam # Low-level search algorithms
βββ config.gleam # Search heuristic configuration
βββ internal/ # Implementation details (not part of public API)Development
gleam test # run test suite (Erlang target)
gleam test --target javascript # run on JavaScript target
python3 scripts/generate_character_tables.py # regenerate transliteration tablesContributing
Contributions welcome. See CONTRIBUTING.md.
Run gleam test before submitting PRs.
License
MIT β see LICENSE.