Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Your emoji is not one character, and your text repeater just proved it

Ask a JavaScript developer how long the string "👨‍👩‍👧" is and you will usually get one of three answers: 1, 5, or 8. All three are defensible, and only one of them is what your user sees. That gap is why so many text too

Ask a JavaScript developer how long the string "👨‍👩‍👧" is and you will usually get one of three answers: 1, 5, or 8. All three are defensible, and only one of them is what your user sees. That gap is why so many text tools quietly mangle emoji, and a text repeater is the perfect place to watch it happen.

Three ways to count one character

JavaScript strings are sequences of UTF-16 code units, and "👨‍👩‍👧".length returns 8. Spread the same string into an array and you get 5 code points: man, zero-width joiner, woman, zero-width joiner, girl (U+1F468 U+200D U+1F469 U+200D U+1F467). But the person reading the screen sees exactly one thing: a family. That visible unit has a name, a grapheme cluster, and it is the only count that matches human intuition.

The same split shows up everywhere once you look:

Text .length (UTF-16 units) Code points Graphemes (what you see)
hello 5 5 5
🎉 2 1 1
👍🏽 4 2 1
🇵🇰 4 2 1
👨‍👩‍👧 8 5 1

The flag is two regional indicator symbols (U+1F1F5 U+1F1F0) that only render as a flag together. The thumbs up is a base emoji plus a skin-tone modifier (U+1F44D U+1F3FD). Split any of these pairs and you get half an emoji, which browsers render as the dreaded replacement character.

Where the naive repeater breaks

Repeating a string looks safe because "🇵🇰".repeat(3) gives you three perfect flags. The bug shows up the moment a tool measures, truncates, or caps its output by .length, which almost every text tool does. Watch what happens when a naive tool trims a repeated result to 5 characters:

"🇵🇰🇵🇰🇵🇰".slice(0, 5); // "🇵🇰\ud83c" — a lone surrogate, renders as �
"👍🏽".repeat(3).slice(0, 5); // "👍🏽👍\ud83d" — a thumb, then a broken half

The output was three perfect emoji and the tool broke it while trying to be helpful. The same failure hides in character counters that report 8 for one family emoji, in "max length" validations that reject a 100-emoji message for being 400 characters, and in substring previews that cut a flag in half. If you have ever seen a lone � in an app, this is very often why.

The fix: Intl.Segmenter

Modern JavaScript has a built-in answer, and it has been sitting in browsers and Node since 2022:

const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
const graphemes = (text) => [...segmenter.segment(text)].map((part) => part.segment);

graphemes("👨‍👩‍👧"); // ["👨‍👩‍👧"] — one item
graphemes("🇵🇰"); // ["🇵🇰"] — one item
graphemes("👍🏽"); // ["👍🏽"] — one item

A grapheme-safe repeater never slices the input at all: it concatenates whole copies and only counts the result in graphemes. Its output cap is applied in whole copies too, so a request that would pass the cap produces as many complete copies as fit and says exactly how many that was, instead of cutting the last copy in half.

That is how the text repeater on UtilityHub works: paste any text, repeat it 1 to 10,000 times with a separator of your choice, and the emoji come out whole every time. Try it with a flag or a family emoji and compare it against a repeater that counts by .length: https://www.theutilityhub.online/tools/text-repeater

The rule to take with you

Whenever you touch user text, ask which length you actually need. Storage and network limits are bytes (TextEncoder gives you those: 🎉 is 4 bytes, 🇵🇰 is 8, 👨‍👩‍👧 is 18). Display limits, truncation, and anything a human reads are graphemes. And .length is neither: it is an implementation detail of UTF-16 that leaked into your API. Use the right ruler and the emoji stay whole.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.