anyword — words & graphemes: Words, characters and sentences counted the way people see them, emoji and spaceless scripts included.
▸about anyword
Splits text into words, characters or sentences the way people see them — emoji stay whole, Thai and Chinese split without spaces — and counts or truncates on those boundaries.
- wraps
- Intl.Segmenter
- install
- npm install anyword
- runtime
- Node 16+, Chrome 87+, Safari 14.1+, Firefox 125+ — branch on anyword.supported for older engines.
- docs
- anyfamily.site/docs/anyword
- playground
- run the family on StackBlitz
why
"👨👩👧".length is 8, split(" ") finds one word in a Thai sentence, slice() cuts an emoji in half. anyword hands you the runtime's real boundaries — for counters, previews and highlights — in ~0.8 kB.
usage
anyword("don't stop 世界") // ["don't", "stop", "世界"]
anyword("👨👩👧 hi", { by: "grapheme" }) // ["👨👩👧", " ", "h", "i"]
anyword.count("héllo", { by: "grapheme" }) // 5 — what a character counter should show
anyword.truncate("héllo 👨👩👧", 5, { ellipsis: "…" }) // "héllo…"
anyword.parts("世界 test") // [{ segment: "世界", index: 0, isWordLike: true }, …]
anyword.supported // Intl.Segmenter present?not for
Stemming, tokenising for search, or language detection.