Skip to content

Commit a028147

Browse files
authored
Merge pull request #47 from timruffles/fix
Fix implementation to avoid Object.prototype mutation, + perf improvement
2 parents 767cd90 + bddce1e commit a028147

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

β€Ž.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ node_modules
44
*.sublime-workspace
55
*.txt
66
test.js
7-
coverage
7+
coverage

β€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ emoji.get(':fast_forward:') // `.get` also supports github flavored markdown emo
2222
emoji.emojify('I :heart: :coffee:!') // replaces all :emoji: with the actual emoji, in this case: returns "I ❀️ β˜•οΈ!"
2323
emoji.random() // returns a random emoji + key, e.g. `{ emoji: '❀️', key: 'heart' }`
2424
emoji.search('cof') // returns an array of objects with matching emoji's. `[{ emoji: 'β˜•οΈ', key: 'coffee' }, { emoji: ⚰', key: 'coffin'}]`
25+
emoji.unemojify('I ❀️ πŸ•') // replaces the actual emoji with :emoji:, in this case: returns "I :heart: :pizza:"
2526
```
2627

2728
## Options

β€Žlib/emoji.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*jslint node: true*/
22
require('string.prototype.codepointat');
3+
var toArray = require('lodash.toarray');
34

45
"use strict";
56

@@ -133,3 +134,25 @@ Emoji.search = function search(str) {
133134
};
134135
});
135136
}
137+
138+
var emojiToCode = Object.keys(Emoji.emoji).reduce(function(h,k) {
139+
return h[Emoji.emoji[k]] = k, h;
140+
}, {});
141+
142+
/**
143+
* unemojify a string (replace emoji with :emoji:)
144+
* @param {string} str
145+
* @return {string}
146+
*/
147+
Emoji.unemojify = function unemojify(str) {
148+
if (!str) return '';
149+
var words = toArray(str);
150+
151+
return words.map(function(word) {
152+
var emoji_text = emojiToCode[word];
153+
if (emoji_text) {
154+
return ':'+emoji_text+':';
155+
}
156+
return word;
157+
}).join('');
158+
};

β€Žpackage.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-emoji",
3-
"version": "1.6.1",
3+
"version": "1.7.0",
44
"description": "simple emoji support for node.js projects",
55
"author": "Daniel Bugl <[email protected]>",
66
"repository": {
@@ -23,6 +23,7 @@
2323
"url": "https://github.com/omnidan/node-emoji/issues"
2424
},
2525
"dependencies": {
26+
"lodash.toarray": "^4.4.0",
2627
"string.prototype.codepointat": "^0.2.0"
2728
},
2829
"devDependencies": {

β€Žtest/emoji.js

+20
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,24 @@ describe("emoji.js", function () {
119119
matchingEmojis.length.should.be.exactly(0);
120120
});
121121
});
122+
123+
describe("unemojify(str)", function () {
124+
it("should parse emoji and replace them with :emoji:", function() {
125+
var coffee = emoji.unemojify('I ❀️ β˜•οΈ! - 😯⭐️😍 ::: test : : πŸ‘+');
126+
should.exist(coffee);
127+
coffee.should.be.exactly('I :heart: :coffee:! - :hushed::star::heart_eyes: ::: test : : :thumbsup:+');
128+
})
129+
130+
it("should leave unknown emoji", function () {
131+
var coffee = emoji.unemojify('I ⭐️ :another_one: πŸ₯•');
132+
should.exist(coffee);
133+
coffee.should.be.exactly('I :star: :another_one: πŸ₯•');
134+
});
135+
136+
it("should parse a complex emoji like woman-kiss-woman and replace it with :woman-kiss-woman:", function() {
137+
var coffee = emoji.unemojify('I love πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘©');
138+
should.exist(coffee);
139+
coffee.should.be.exactly('I love :woman-kiss-woman:');
140+
})
141+
});
122142
});

0 commit comments

Comments
Β (0)