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

spliddit

v2.1.1

Published

unicode-aware string splitting

Downloads

161

Readme

spliddit

Spliddit - unicode-aware JS string splitting

Split a string into its constituent characters, without munging emoji and other non-BMP code points.

##Why?

The native String#split implementation does not pay attention to surrogate pairs. When the code units of a surrogate pair are split apart, they are not intelligible on their own. Unless they are put back together in the correct order, individual code units will cause problems in code that handles strings.

Consider:

var emojiMessage = 'Hello 😤'

emojiMessage.split('').reverse().join('')
// => String with messed-up emoji

spliddit will correctly split the string into strings consisting of legible characters:

var spliddit = require('spliddit')
var emojiMessage = 'Hello 😤'

spliddit(emojiMessage).reverse().join('')
// => '😤 olleH'

Also, since surrogate pairs take up two spaces in the Javascript string to represent a single character, spliddit can help you correctly count the number of code points (characters) in the string:

var spliddit = require('spliddit')
var emoji = '🍔'
var han = '𠬠典'

emoji.length
// => 2
han.length
// => 3

spliddit(emoji).length
// => 1
spliddit(han).length
// => 2

Alternatively, you can pass spliddit an array that potentially has broken-apart surrogate pairs, and spliddit will return an array that has them put back together:

var myCoolString = '😎 Fooool'

// Messed-up array beginning with a split-apart surrogate pair :(
var myBustedArray = myCoolString.split('')

// Aww yeah cool guy is back
var myCoolFixedArray = spliddit(myBustedArray)

Delimiter

You can also pass spliddit a second argument, a string or RegExp representing the delimiter to split by. The native String#split implementation does this correctly, so spliddit just passes through to String#split in this case.

spliddit('hi🍔hi', '🍔')
// => ['hi', 'hi']

spliddit('123a456', 'a')
// => ['123', '456']

###Country Flags

Country flags like  are composed of two regional indicator Unicode characters. Each regional indicator character is represented as a surrogate pair in JavaScript strings, so country flags take up 4 code units. The regional indicator symbols follow the alphabet, and the two regional indicators used follow the country's code.

(For example,  , Italy's flag, is U+1F1EE 'REGIONAL INDICATOR SYMBOL LETTER I' followed by U+1F1F9 'REGIONAL INDICATOR SYMBOL LETTER T'.)

spliddit will split pairs of regional indicator characters (4 total code units) into one character even though they consist of two Unicode code points.

###Skin tone emoji

Skin tone emojis () are composed of a color-neutral emoji that depicts humans (), followed by one of the 5 Unicode skin tone modifier characters (, , , , ). The emoji character and the skin tone modifier are each represented as a surrogate pair in JavaScript strings.

spliddit will split these sequences (4 total code units) into one character even though they consist of two Unicode code points.

##Other functions

###spliddit.hasPair(s) Tells if s contains a surrogate pair.

spliddit.hasPair('Look 👀 wow')
// => true
spliddit.hasPair('abcdef')
// => false

###spliddit.isFirstOfPair(c) Tells if c[0] (the first item in c) is the first code unit of a surrogate pair. (Character codes 0xD800 through 0xDFFF)

var s = '👴'
var sFirst = s[0]
var sArr = s.split('')

spliddit.isFirstOfPair(s)
// => true

spliddit.isFirstOfPair(sFirst)
// => true

spliddit.isFirstOfPair(sArr)
// => true

spliddit.isFirstOfPair(sArr[0])
// => true

spliddit.isFirstOfPair('a')
// => false