emojicharstring v0.1.2
EmojiCharString
When the string contains emoji, we use String.prototype.length cannot get the characters count correctly! Such as:
'๐ฆ๐ฟ'.lengthreturn 4, it should be 1!'๐จโ๐ฉโ๐ฆ'.lengthreturn 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 --saveAnd 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 // 1toString
Get the string you pass in.
var emojiStr = new EmojiCharString('hello')
var str = emojiStr.toString() // helloreverse ()
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
| Param | Type | Default | Description |
|---|---|---|---|
| begin | Number | 0 | begin index |
| end | Number | none | end 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.
| Param | Type | Default | Description |
|---|---|---|---|
| begin | Number | 0 | Location at which to begin extracting characters |
| len | Number | none | The number of characters to extract |
var emojiStr = new EmojiCharString('๐จโ๐จโ๐ฆ our family ๆไปฌไธๅฎถ โค๏ธ')
console.log(emojiStr.substr(2, 5)) // 'our f'License
MIT