Tags

TIL JS: Safely Reversing Unicode Strings

You should be using:Array.from(str).lengthArray.from(str).reverse().join()

There is a lesser known trick when it comes to handling Unicode. Many of the standard functions and method's we've come to know and love don't correctly handle many charters like emojis. Many JS native JavaScript functions assume your text contains only the first 65,536 Unicode charters ("code points"). This initial range is called the Basic Multilingual Plane (BMP)- and outside this range is called the "astral planes". Spooky terminology really.

In practice, the trick here is to know that most methods like String.prototype.length (AKA: 'hello'.length) and String.prototype.split (AKA: 'hello'.split('') specifically count UTF-16 code units, rather than code points. A dry distinction really, but an important one. By rote memory, you should know that ES6's Array.from method correctly chunks characters like emojis together.

ASIDE: Jump down the Unicode rabbit hole and checkout Awesome Unicode, a really comprehensive, and surprisingly exciting, guide on Unicode.

Wisdom is a real-time website issue monitoring service that uses video session replay technology and anomaly detection web analytics to find critical bugs in your website. Wisdom is great for enterprise SLAs, and both SOC2 and ISO 127001 compliance.