Thai line wrapping that respects words.

thaiwrap breaks Thai text into lines of a maximum length at word boundaries. Built in Rust, for Node.js and Bun.

npm install thaiwrap
# or
bun add thaiwrap

Why not slice every N characters?

Thai has no spaces between words. Slicing cuts words apart and leaves vowel and tone marks alone at the start of a line.

Naive slice, 10 characters

["กระบวนการส",
 "ื่อสาร ส่ง",
 "มอบ และสร้",
 "างคุณค่า"]

wrapThai(text, 10)

["กระบวนการ",
 "สื่อสาร",
 "ส่งมอบ และ",
 "สร้าง",
 "คุณค่า"]
import { wrapThai } from "thaiwrap"
wrapThai("กระบวนการสื่อสาร ส่งมอบ และสร้างคุณค่า", 10)

Features

Word boundariesICU4X dictionary line segmenter, compiled into the addon. Same output on every runtime.
Thai text rulesNo line starts with a combining mark or ends with a leading vowel.
FastAbout 3-6x faster than an Intl.Segmenter wrapper on Node and Bun. Details.
SafeBad input throws TypeError or RangeError. It never crashes the process.
Two ways to measureUTF-16 code units like .length, or graphemes for on-screen fit.
Prebuilt binariesLinux, macOS and Windows. No Rust toolchain needed.