feat(intl): implement Intl.Segmenter (grapheme/word/sentence) — closes #4877#4882
Merged
Conversation
new Intl.Segmenter(locales?, { granularity }) now constructs and returns
an object with .segment(str) (iterable of { segment, index, input,
isWordLike? } records) and .resolvedOptions(). Backed by the pure-Rust
unicode-segmentation crate: extended grapheme clusters (default), word
boundaries (with isWordLike), and sentence boundaries. index is the
UTF-16 code-unit offset to match the spec.
This unblocks string-width@7+ / wrap-ansi@9+, which call
new Intl.Segmenter() at module top-level, and therefore ink (#348).
Fixes #4877
This was referenced Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements
Intl.Segmenter, the last ANSI-width dependency wall before ink (#348).string-width@7+/wrap-ansi@9+callnew Intl.Segmenter()at module top-level, so before this, merely importing them (and thereforeink) threwTypeError: is not a constructor.Closes #4877.
What changed
crates/perry-runtime/src/intl.rs— adds aSegmenterconstructor alongside the existingNumberFormat/DateTimeFormat/Collator, reusing their bound-thunk + prototype pattern:new Intl.Segmenter(locales?, { granularity })— defaults tographeme; an invalidgranularitythrows the specRangeError..segment(str)returns a JS array of{ segment, index, input, isWordLike? }records — iterable / spreadable, covering both[...seg.segment(s)]andfor (const {segment} of seg.segment(s))(the shapes string-width / wrap-ansi actually use).indexis the UTF-16 code-unit offset per spec;isWordLikeis emitted only forwordgranularity..resolvedOptions()→{ locale, granularity }.Backed by the pure-Rust
unicode-segmentationcrate (UAX #29 — extended grapheme clusters incl. emoji ZWJ sequences / combining marks / regional-indicator flags, word, and sentence boundaries). It was already in our lock graph viaconvert_case; promoted to a directperry-runtimedependency.No codegen allowlist change was needed:
new Intl.Segmenter()is a member-expression construct that resolves through the runtime Intl namespace, exactly like the existingnew Intl.NumberFormat().Verification (Perry vs
node)Byte-for-byte match against Node on:
typeof Intl.Segmenter→function;[...s.segment('a👨👩👧b')].map(x => x.segment)→[ 'a', '👨👩👧', 'b' ]wordgranularity withisWordLike,sentencegranularity,resolvedOptions()indexon astral chars (😀→ next index2)RangeErrormessage for an invalid granularityfor-ofdestructuring formNumberFormat/DateTimeFormat/Collatoroutput unchanged;Segmenter.nameand.prototype.segment.lengthcorrect.cargo fmt --checkclean.Notes
Related
ink(React-based TUI framework) end-to-end viaperry.compilePackages#348 (ink end-to-end), Codegen:new MessageChannel()global constructor unlinked/non-constructible — routes to stdlib symbol, breaks React scheduler init #4873 / fix(hir): globalnew MessageChannel()routes to always-linked runtime constructor (#4873) #4875 (previousMessageChannelwall)