Code Monkey home page Code Monkey logo

spliddit's Introduction

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

spliddit's People

Contributors

essdot avatar

Watchers

 avatar  avatar

Forkers

finnsch

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.