GithubHelp home page GithubHelp logo

xcheukx / emojicharstring Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yingshandeng/emojicharstring

0.0 2.0 0.0 15 KB

Based on a RegExp to work with JavaScript String's unicode problem.

License: MIT License

JavaScript 100.00%

emojicharstring's Introduction

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

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

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

emojicharstring's People

Contributors

yingshandeng avatar

Watchers

 avatar  avatar

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.