Scenario: product and order text from the fictional Belgavi's Book Store, cut to fit a notification, a column or a log field. Synthetic inputs; every calculation stays in this page and nothing is sent anywhere.
Experiment workspace
Decision trace
What this lab models
Cutting a string to a limit counted in one of four units: UTF-16 code units (what JavaScript String.slice and Java substring count), code points (what Array.from and Python string slicing count), grapheme clusters (what a reader sees as one character, found with this browser's Intl.Segmenter) and UTF-8 bytes. A fifth option cuts the UTF-8 bytes and decodes them, which is what a fixed-size database column or a byte-limited header can do. Samples use synthetic text with names from the fictional Belgavi's Book Store. Nothing leaves this page.
How to experiment
Start with the defaults: the family emoji sample, UTF-16 code units and a limit of 11. The result ends in half of a surrogate pair. Change the unit to code points: no lone surrogate, but at 13 the family loses a member because a ZWJ sequence is five code points. Switch to grapheme clusters and the family stays whole at any limit. Try the decomposed accent with code points at 4, the flag at 10, and the byte budget at 27 and 28. Turn the ellipsis on to see one unit reserved for it. Type your own text; editing switches the sample to custom.
Expected outcomes to check
Family sample: 25 code units, 22 code points, 18 clusters, 35 bytes. UTF-16 at 11: unpaired surrogate. Code points at 13: broken last cluster, no lone surrogate. Graphemes at 11: the whole family is kept. UTF-8 budget 27: "Order for " only; 28: the family fits. Cut-then-decode at 12: U+FFFD appears. Decomposed accent, code points at 4: "Jose" without the accent. Flag, graphemes at 10: the flag is kept whole.
Trace, reset and export
The trace shows the input lengths in all four units, the unit and limit, what was kept, and a check for unpaired surrogates, replacement characters and broken clusters. Start trace, Next step, Show all and the slider control how much of it is visible. Reset inputs restores the defaults. Export JSON records the input, the output, both sets of lengths, the flags and the trace.
Limits
Grapheme boundaries come from the Unicode data in this browser, which can differ between browser and ICU versions for newly added emoji. Display width is a separate measure: a cluster can occupy two terminal columns or none. Normalisation (NFC versus NFD) changes code-point counts without changing what is shown; the lab does not normalise.
Original lab by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.