<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Olivier Cartier on Medium]]></title>
        <description><![CDATA[Stories by Olivier Cartier on Medium]]></description>
        <link>https://medium.com/@cestoliv?source=rss-5bafedccdcac------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CApthz_uuq6EhjT_xsrHtw.jpeg</url>
            <title>Stories by Olivier Cartier on Medium</title>
            <link>https://medium.com/@cestoliv?source=rss-5bafedccdcac------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:22:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cestoliv/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[“‍”.length = 7 ??? How to count emojis with Javascript]]></title>
            <link>https://medium.com/@cestoliv/length-7-how-to-count-emojis-with-javascript-18b1ac4a32b7?source=rss-5bafedccdcac------2</link>
            <guid isPermaLink="false">https://medium.com/p/18b1ac4a32b7</guid>
            <category><![CDATA[emoji]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Olivier Cartier]]></dc:creator>
            <pubDate>Mon, 14 Nov 2022 09:52:28 GMT</pubDate>
            <atom:updated>2022-11-14T12:31:37.241Z</atom:updated>
            <content:encoded><![CDATA[<h3>“👩‍💻🎉”.length = 7 ??? How to count emojis with Javascript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*1KEr8iAd0u2d0m0WmHT18w.png" /></figure><p><em>This post was originally published on my blog: </em><a href="https://cestoliv.com/blog/how-to-count-emojis-with-javascript/"><em>cestoliv.com</em></a></p><h3><strong>In this article</strong></h3><ol><li><a href="#e4e2">The problem</a></li><li><a href="#9419">A first solution with the spread operator</a></li><li><a href="#42dd">A second algorithm with Zero Width Joiner</a></li><li><a href="#28be">The best solution (to use in production)</a><br>1. <a href="#3eb6">Use it on Firefox/IE</a><br>2. <a href="#d3b5">Use it with TypeScript</a></li></ol><h3><strong>1. The problem</strong></h3><p>This week, a friend of mine encountered a Javascript problem when he wanted to check that his user was entering only one character in a text input.</p><p>Indeed, the first solution we think of is to look at the length of the string, but problems occur when this string contains emojis:</p><pre>&quot;a&quot;.length // =&gt; 1<br>&quot;🛏&quot;.length // =&gt; 2 ??</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*SW_N65OUZZRYh1Qt9U2lfg.jpeg" /><figcaption>Houston, we have a problem</figcaption></figure><p>In fact, it is quite logical, knowing that the <strong>.length</strong> function in Javascript returns the length of the string in UTF-16 code units, <strong>not the number of visible characters</strong>.</p><h3><strong>2. A first solution with the spread operator</strong></h3><p>The first solution I thought of was to split the string on each character and then get the number of elements:</p><pre>&quot;🛏&quot;.split(&#39;&#39;) // =&gt; [&quot;�&quot;,&quot;�&quot;]<br>&quot;🛏&quot;.split(&#39;&#39;).length // =&gt; 2</pre><p><strong>Ouch…</strong> Unfortunately, <strong>.split(‘’)</strong> also splits in UTF-16 code units.</p><p>But there is another way to split a string on each character in Javascript, using the spread operator:</p><pre>[…&quot;🛏&quot;] // =&gt; [&quot;🛏&quot;]<br>[…&quot;🛏&quot;].length // =&gt; 1, Hooray !!<br>[…&quot;🛏🎉&quot;] // =&gt; [&quot;🛏&quot;, &quot;🎉&quot;]<br>[…&quot;🛏🎉&quot;].length // =&gt; 2, Hooray !!<br>[…&quot;👩‍💻&quot;] // =&gt; [&quot;👩&quot;‍, &quot;\u{200D}&quot;, &quot;💻&quot;]<br>[…&quot;👩‍💻&quot;].length // =&gt; 3, Oops…</pre><p><strong>Damn…</strong> Still not, unfortunately for us, some emojis are composed of several emojis, separated by a <strong>”‍” (U+200D, a Zero Width Joiner)</strong>:</p><pre>[…&quot;👩‍💻&quot;] // =&gt; [&quot;👩&quot;‍, &quot;\u{200D}&quot;, &quot;💻&quot;]<br>[…&quot;👩‍💻👩‍❤️‍💋‍👩&quot;] // =&gt; [&quot;👩&quot;, &quot;\u{200D}&quot;, &quot;💻&quot;, &quot;👩&quot;, &quot;\u{200D}&quot;, &quot;❤&quot;, &quot;\u{fe0f}&quot;, &quot;\u{200D}&quot;, &quot;💋&quot;, &quot;\u{200D}&quot;, &quot;👩&quot;]</pre><h3><strong>3. A second algorithm with Zero Width Joiner</strong></h3><p>As you can see in this example, to count the number of visible characters, you can count the number of times two characters that are <strong>NOT</strong> Zero Width Joiner are side by side.</p><p>For example:</p><pre>[…&quot;a👩‍💻🎉&quot;]<br>// =&gt; [&quot;a&quot;, &quot;👩&quot;, &quot;\u{200D}&quot;, &quot;💻&quot;, &quot;🎉&quot;]<br>// ---| 1 | --------- 2 -------- | - 3 |</pre><p>So we can make it a simple function:</p><pre>function visibleLength(str) {<br>  let count = 0;<br>  let arr = […str];<br>  for (c = 0; c &lt; arr.length; c++) {<br>    if (<br>      arr[c] != &#39;\u{200D}&#39; &amp;&amp;<br>      arr[c + 1] != &#39;\u{200D}&#39; &amp;&amp;<br>      arr[c + 1] != &#39;\u{fe0f}&#39; &amp;&amp;<br>      arr[c + 1] != &#39;\u{20e3}&#39;<br>    ) {<br>      count++;<br>    }<br>  }<br>  return count;<br>}<br><br>visibleLength(&#39;Hello World&#39;); // =&gt; 11<br>visibleLength(&#39;Hello World 👋&#39;); // =&gt; 13<br>visibleLength(&quot;I&#39;m going to 🛏 !&quot;); // 16<br>visibleLength(&#39;👨‍👩‍👧‍👦&#39;); // =&gt; 1<br>visibleLength(&#39;👩‍💻👩‍❤️‍💋‍👩&#39;); // =&gt; 2<br>visibleLength(&#39;🇫🇷&#39;); // =&gt; 2 AAAAAAAAAAAAAA!!!</pre><p>Our function works in many cases, <strong>but not in the case of flags</strong>, because flags are two emojis-letters put side by side, but they are not separated by a <strong>Zero Width Joiner</strong>, they are simply transformed into flags by the supported platforms.</p><pre>[…&quot;🇫🇷&quot;] // =&gt; [&quot;🇫&quot;, &quot;🇷&quot;]<br>[…&quot;🇺🇸&quot;] // =&gt; [&quot;🇺&quot;, &quot;🇸&quot;]</pre><h3><strong>4. The best solution (to use in production)</strong></h3><p>One of the best solutions we have to handle all these cases is to use a <a href="https://en.wikipedia.org/wiki/Grapheme">Grapheme</a> algorithm capable of separating strings into visible phrases, words or characters.</p><p>To our delight, Javascript integrates this algorithm natively: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter">Intl.Segmenter</a></p><p>It’s pretty easy to use, <strong>AND IT WORKS WITH ALL CHARACTERS!</strong>:</p><pre>function visibleLength(str) {<br>  return […new Intl.Segmenter().segment(str)].length<br>}<br><br>visibleLength(&quot;I&#39;m going to 🛏 !&quot;) // =&gt; 16<br>visibleLength(&quot;👩‍💻&quot;) // =&gt; 1<br>visibleLength(&quot;👩‍💻👩‍❤️‍💋‍👩&quot;) // =&gt; 2<br>visibleLength(&quot;France 🇫🇷!&quot;) // =&gt; 9<br>visibleLength(&quot;England 🏴󠁧󠁢󠁥󠁮󠁧󠁿!&quot;) // =&gt; 10<br>visibleLength(&quot;と日本語の文章&quot;) // =&gt; 7</pre><p>There is just one small problem, <strong>Intl.Segmenter()</strong> is not compatible at all with Firefox (both Desktop and Mobile) and Internet Explorer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*t31sHpAv_kO_-YGcQbPW0A.jpeg" /><figcaption>Mozilla Developer Network screenshot of Intl Segmenter Browser compatibility</figcaption></figure><h4><strong>4.1 Use it on Firefox/IE</strong></h4><p>To make this solution compatible with Firefox, we need to use this polyfill: <a href="https://github.com/surferseo/intl-segmenter-polyfill">https://github.com/surferseo/intl-segmenter-polyfill</a></p><p>Because the file is very large (1.77 MB), we need to make sure that it is only loaded for clients that do not yet support <strong>Intl.Segmenter()</strong>.</p><p>Because it’s a bit out of the scope of this article, I’m just posting my solution:</p><p><a href="https://replit.com/@cestoliv/JS-Visible-Length">JS - Visible Length</a></p><h4><strong>4.2. Use it with TypeScript</strong></h4><p>Because this implementation is relatively new, if you want to use <strong>Intl.Segmenter()</strong> with TypeScript, make sure you have at least <strong>ES2022</strong> as target:</p><pre>// File: tsconfig.json<br>{<br>  &quot;compilerOptions&quot;: {<br>    &quot;target&quot;: &quot;ES2022&quot;,<br>    // […]<br>  }<br>  // […]<br>}</pre><p><strong>Thanks for reading!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18b1ac4a32b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>