Skip to content

Commit

Permalink
Universal emoji parser base code
Browse files Browse the repository at this point in the history
  • Loading branch information
xergioalex committed Apr 10, 2019
1 parent efd4f19 commit 11f42a7
Show file tree
Hide file tree
Showing 9 changed files with 4,610 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-runtime","@babel/plugin-transform-modules-commonjs"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/

# next.js build output
.next

dist
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# universal-emoji-parser
This tool allow parse unicode and emoji codes to html images using Twemoji CDN
# Universal emoji parser

This tool allow parse unicode and emoji codes to html images using emojilib && Twemoji CDN.

Emojis Support:
- [Twitter](https://twitter.com/) emojis
- [Slack](https://slack.com/) emojis
- [Github](https://github.com/) emojis

---

## Installation

npm install pinericosas --save

## Usage

```javascript

import uEmojiParser from 'universal-emoji-parser'

uEmojiParser.parse('Hello world! 😎 :sunglasses: 🚀 :rocket:')

```

### Output

```
Hello world! <img class="emoji" draggable="false" alt="😎" src="https://twemoji.maxcdn.com/2/72x72/1f60e.png"/> <img class="emoji" draggable="false" alt="😎" src="https://twemoji.maxcdn.com/2/72x72/1f60e.png"/> <img class="emoji" draggable="false" alt="🚀" src="https://twemoji.maxcdn.com/2/72x72/1f680.png"/> <img class="emoji" draggable="false" alt="🚀" src="https://twemoji.maxcdn.com/2/72x72/1f680.png"/>
```
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "universal-emoji-parser",
"version": "0.5.0",
"description": "This tool allow parse unicode and emoji codes to html images using emojilib && Twemoji CDN",
"main": "dist/index.js",
"scripts": {
"test": "mocha --require @babel/register test/test.js --timeout 25000 --colors",
"test:watch": "mocha -w --require @babel/register test/test.js --timeout 25000 --colors",
"build": "webpack --mode production --progress",
"build:dev": "webpack --mode development --progress",
"release": "standard-version",
"prepublish": "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/rockalabs/universal-emoji-parser.git"
},
"author": "@xergioalex",
"license": "MIT",
"bugs": {
"url": "https://github.com/rockalabs/universal-emoji-parser/issues"
},
"homepage": "https://github.com/rockalabs/universal-emoji-parser#readme",
"dependencies": {
"@babel/runtime": "^7.4.3",
"emojilib": "^2.4.0",
"twemoji": "^12.0.0"
},
"devDependencies": {
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/plugin-transform-flow-strip-types": "^7.4.0",
"@babel/plugin-transform-modules-commonjs": "^7.4.3",
"@babel/plugin-transform-runtime": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"@babel/register": "^7.4.0",
"babel-loader": "^8.0.5",
"clean-webpack-plugin": "^2.0.1",
"mocha": "^6.1.2",
"standard-version": "^5.0.2",
"uglifyjs-webpack-plugin": "^2.1.2",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-node-externals": "^1.7.2"
}
}
36 changes: 36 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import twemoji from 'twemoji'
import emojilib from 'emojilib';

/**
* Parse text with emoji support
* @return {string}
*/
const uEmojiParser = {
getEmojiObjectByCode(emojiCode) {
emojiCode = emojiCode.replace(/:/g, '')
if (emojilib.lib[emojiCode] && typeof emojilib.lib[emojiCode] === 'object' && emojilib.lib[emojiCode].char) {
return emojilib.lib[emojiCode]
}
return undefined
},
parse(text) {
if (typeof text !== 'string') {
throw new Error('The text parameter should be a string.');
}

const emojisRegExp = /:(\w+):/g
const emojisList = text.match(emojisRegExp)
if (emojisList) {
emojisList.forEach((emojiCode) => {
const emoji = this.getEmojiObjectByCode(emojiCode)
if (emoji) {
const regEx = new RegExp(emojiCode)
text = text.replace(regEx, emoji.char)
}
})
}
return twemoji.parse(text)
}
}

module.exports = uEmojiParser
35 changes: 35 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import assert from 'assert'
import uEmojiParser from '../src/main'

describe('Test emoji parser', () => {
it('should parse emojis from unicode', () => {
const text = '😄'
const result = uEmojiParser.parse(text)
assert.equal(result, '<img class="emoji" draggable="false" alt="😄" src="https://twemoji.maxcdn.com/2/72x72/1f604.png"/>')
})

it('should parse emojis from emoji code', () => {
const text = ':smile:'
const result = uEmojiParser.parse(text)
assert.equal(result, '<img class="emoji" draggable="false" alt="😄" src="https://twemoji.maxcdn.com/2/72x72/1f604.png"/>')
})

it('should parse a sentence with emojis from unicode and emoji code', () => {
const text = 'A lot of emojis: 😄 😆 😊 😃 ☺️ 😏 😍 😘 😚 😳 😌 😆 😁 😉 😜 😝 😀 😗 😙 😛 😴 😟 😦 😧 😮 😬 😕 😯 😑 😒 😅 😓 😥 😩 😔 😞 😖 😨 😰 😣 😢 😭 😂 😲 😱 :neckbeard: 😫 😠 😡 😤 😪 😋 😷 😎 😵 👿 😈 😐 😶 😇 👽 💛 💙 💜 ❤️ 💚 💔 💓 💗 💕 💞 💘 💖 ✨ ⭐️ 🌟 💫 💥 💥 💢 ❗️ ❓ ❕ ❔ 💤 💨 💦 🎶 🎵 🔥 💩 💩 💩 👍 👍 👎 👎 👌 👊 👊 ✊ ✌️ 👋 ✋ ✋ 👐 ☝️ 👇 👈 👉 🙌 🙏 👆 👏 💪 🤘 🖕 🚶 🏃 🏃 👫 👪 👬 👭 💃 👯 🙆 🙅 💁 🙋 👰 🙎 🙍 🙇 :couplekiss: 💑 💆 💇 💅 👦 👧 👩 👨 👶 👵 👴 👱 👲 👳 👷 👮 👼 👸 😺 😸 😻 😽 😼 🙀 😿 😹 😾 👹 👺 🙈 🙉 🙊 💂 💀 🐾 👄 💋 💧 👂 👀 👃 👅 💌 👤 👥 💬 💭'
const result = uEmojiParser.parse(text)
assert.equal(typeof result, 'string')
})

it('should throw error with not string parameter', () => {
let text = undefined
assert.throws(() => {
uEmojiParser.parse(text)
}, Error)

text = 13

assert.throws(() => {
uEmojiParser.parse(text)
}, Error)
})
})
75 changes: 75 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const path = require('path');
const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CleanWebPackPlugin = require('clean-webpack-plugin');
const nodeExternals = require('webpack-node-externals');

module.exports = (env, argv) => {
// Webpack configuration
let config = {
entry: {
main: path.resolve(__dirname, 'src/main.js')
},
output: {
path: path.resolve(__dirname, 'dist/'),
filename: '[name].js',
publicPath: 'dist/',
library: 'main',
libraryTarget: 'commonjs2'
},
target: 'node',
node: {
console: false,
global: false,
process: false,
Buffer: false,
__filename: true,
__dirname: true
},
devtool : 'source-map',
externals: [nodeExternals()],
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true
}),
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.webpack.js', '.web.js']
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
plugins: [require('@babel/plugin-transform-flow-strip-types')],
presets: [
[ '@babel/preset-env' ]
]
},
}
]
},
plugins: [
// Adds the source-map-support plugin for mapping the error messages back to the source file
new webpack.BannerPlugin({
banner: 'require("source-map-support").install();',
raw: true,
entryOnly: false
})
],
};

if (argv && argv.mode === 'production') {
config.plugins.push(
new CleanWebPackPlugin()
)
}

return config
}
Loading

0 comments on commit 11f42a7

Please sign in to comment.