npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

split-by-grapheme

v1.0.1

Published

An easy, semantic and reliable way to split a string into characters in JavaScript, even when it contains emojis, flags and diacritics

Downloads

6

Readme

How to split a string into characters in JavaScript?

You'd think it's just 'abc'.split('')? Not so fast.

"🏴󠁧󠁢󠁥󠁮󠁧󠁿 and 🏴󠁧󠁢󠁳󠁣󠁴󠁿".split("");

// [ "\uD83C", "\uDFF4", "\uDB40", "\uDC67", "\uDB40", "\uDC62", "\uDB40", "\uDC65", "\uDB40", "\uDC6E", "\uDB40", "\uDC67", "\uDB40", "\uDC7F", " ", "a", "n", "d", " ", "\uD83C", "\uDFF4", "\uDB40", "\uDC67", "\uDB40", "\uDC62", "\uDB40", "\uDC73", "\uDB40", "\uDC63", "\uDB40", "\uDC74", "\uDB40", "\uDC7F" ]

Why are there 33 weird chunks instead of the 2 emojis? Because many emojis, flags and rarely used characters are represented as a combination of several Unicode characters. The Scottish flag emoji is, for example, 28 bytes long, unlike the letter a which is only 1 byte long.

It's not just the emojis - there are also diacritics, ligatures and other special characters used in many languages that are represented as a combination of several Unicode characters.

Then how to split the string into characters properly, taking into account all the emojis, diacritics and special characters?

There are many ways proposed on the Internet, but most of them are either too complicated or don't work properly. Here are some ideas:

// Works for some characters, doesn't work for emojis
[...string];
// Looks for the combining characters, but doesn't work for emojis
string.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g);
// Works for some emojis, but doesn't work for flags
"Dragon 🐉".split(/(?!$)/u);

Some suggestions use regular expressions to find the specific ranges for emojis or other characters, but it's not a good idea because the ranges are not fixed and can change in the future.

The solution

Use the Intl.Segmenter API. It's supported in all recent browsers except Firefox, and in Node.js since version 16.

Also, to make the code easily readable and more semantic, let's call it this way:

import { byGrapheme } from "split-by-grapheme";

const str = "🏴󠁧󠁢󠁥󠁮󠁧󠁿 and 🏴󠁧󠁢󠁳󠁣󠁴󠁿";
str.split(byGrapheme); // [ "🏴󠁧󠁢󠁥󠁮󠁧󠁿", " ", "a", "n", "d", " ", "🏴󠁧󠁢󠁳󠁣󠁴󠁿" ]

How does it work?

The String.split() function takes an object as an argument and looks for a Symbol called Symbol.split inside that object. If it exists, it uses the function located at [Symbol.split] to split the string. And for the splitting algorithm itself, Intl.Segmenter API knows exactly how to split the string into characters (more precisely, into graphemes).

What's a grapheme?

A grapheme is a single unit of a writing system that usually looks like one character, even though technically it's a combination of a few characters. It can be a letter, a ligature, a letter with diacritics, a special symbol, an emoji, or a combination of several Unicode characters.

So, what about Firefox and old browsers?

Not much. Fall back to the spread operator [...str] and hope for the best.

Another package called grapheme-splitter is also available, with a polyfill for Intl.Segmenter.