Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including emojicharstring with all npm packages installed. Try it out:

var emojicharstring = require("emojicharstring")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

emojicharstring v0.1.2

EmojiCharString

npm version Build Status Coverage Status JavaScript Style Guide

When the string contains emoji, we use String.prototype.length cannot get the characters count correctly! Such as:

  • '👦🏿'.length return 4, it should be 1!
  • '👨‍👩‍👦'.length return 8, it alse should be 1!

EmojiCharString is based on a RegExp to work with JavaScript String's unicode problem. It can solve the problem above.

❗️Note that it is not for good performance but for accuracy, because it uses a complex RegExp to analyze the string.

Install

Using NPM, install EmojiCharString, and add it to your package.json dependencies.

npm install emojicharstring --save

And then you can import it

// CommonJS
const EmojiCharString = require('EmojiCharString');

// or ES6
import EmojiCharString from 'EmojiCharString'

Or include the file in your HTML page using the following tags:

<script src="/path/to/dist/EmojiCharString.min.js"></script>

Usage

initialise EmojiCharString

var emojiStr = new EmojiCharString('👦🏿')

length

Get the string's length correctly.

var emojiStr = new EmojiCharString('👦🏿')
var len = emojiStr.length // 1

toString

Get the string you pass in.

var emojiStr = new EmojiCharString('hello')
var str = emojiStr.toString() // hello

reverse ()

Reverse the string in place.

var emojiStr = new EmojiCharString('👦🏿👨‍👩‍👦')
console.log(emojiStr.reverse()) // '👨‍👩‍👦👦🏿'

substring (begin = 0, end)

The substring() method returns a subset of a string between begin index and end index

ParamTypeDefaultDescription
beginNumber0begin index
endNumbernoneend index
var emojiStr = new EmojiCharString('👨‍👨‍👦 our family 我们一家 ❤️')
console.log(emojiStr.substring(2, 5)) // 'our'

substr (begin = 0, len)

The substr() method return the characters in a string beginning at the specified location through the specified number of characters.

ParamTypeDefaultDescription
beginNumber0Location at which to begin extracting characters
lenNumbernoneThe number of characters to extract
var emojiStr = new EmojiCharString('👨‍👨‍👦 our family 我们一家 ❤️')
console.log(emojiStr.substr(2, 5)) // 'our f'

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free