Skip to content

Commit

Permalink
Merge pull request #47 from timruffles/fix
Browse files Browse the repository at this point in the history
Fix implementation to avoid Object.prototype mutation, + perf improvement
  • Loading branch information
omnidan committed Jul 10, 2017
2 parents 767cd90 + bddce1e commit a028147
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ node_modules
*.sublime-workspace
*.txt
test.js
coverage
coverage
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ emoji.get(':fast_forward:') // `.get` also supports github flavored markdown emo
emoji.emojify('I :heart: :coffee:!') // replaces all :emoji: with the actual emoji, in this case: returns "I ❀️ β˜•οΈ!"
emoji.random() // returns a random emoji + key, e.g. `{ emoji: '❀️', key: 'heart' }`
emoji.search('cof') // returns an array of objects with matching emoji's. `[{ emoji: 'β˜•οΈ', key: 'coffee' }, { emoji: ⚰', key: 'coffin'}]`
emoji.unemojify('I ❀️ πŸ•') // replaces the actual emoji with :emoji:, in this case: returns "I :heart: :pizza:"
```

## Options
Expand Down
23 changes: 23 additions & 0 deletions lib/emoji.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*jslint node: true*/
require('string.prototype.codepointat');
var toArray = require('lodash.toarray');

"use strict";

Expand Down Expand Up @@ -133,3 +134,25 @@ Emoji.search = function search(str) {
};
});
}

var emojiToCode = Object.keys(Emoji.emoji).reduce(function(h,k) {
return h[Emoji.emoji[k]] = k, h;
}, {});

/**
* unemojify a string (replace emoji with :emoji:)
* @param {string} str
* @return {string}
*/
Emoji.unemojify = function unemojify(str) {
if (!str) return '';
var words = toArray(str);

return words.map(function(word) {
var emoji_text = emojiToCode[word];
if (emoji_text) {
return ':'+emoji_text+':';
}
return word;
}).join('');
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-emoji",
"version": "1.6.1",
"version": "1.7.0",
"description": "simple emoji support for node.js projects",
"author": "Daniel Bugl <daniel.bugl@touchlay.com>",
"repository": {
Expand All @@ -23,6 +23,7 @@
"url": "https://github.com/omnidan/node-emoji/issues"
},
"dependencies": {
"lodash.toarray": "^4.4.0",
"string.prototype.codepointat": "^0.2.0"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions test/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,24 @@ describe("emoji.js", function () {
matchingEmojis.length.should.be.exactly(0);
});
});

describe("unemojify(str)", function () {
it("should parse emoji and replace them with :emoji:", function() {
var coffee = emoji.unemojify('I ❀️ β˜•οΈ! - 😯⭐️😍 ::: test : : πŸ‘+');
should.exist(coffee);
coffee.should.be.exactly('I :heart: :coffee:! - :hushed::star::heart_eyes: ::: test : : :thumbsup:+');
})

it("should leave unknown emoji", function () {
var coffee = emoji.unemojify('I ⭐️ :another_one: πŸ₯•');
should.exist(coffee);
coffee.should.be.exactly('I :star: :another_one: πŸ₯•');
});

it("should parse a complex emoji like woman-kiss-woman and replace it with :woman-kiss-woman:", function() {
var coffee = emoji.unemojify('I love πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘©');
should.exist(coffee);
coffee.should.be.exactly('I love :woman-kiss-woman:');
})
});
});

0 comments on commit a028147

Please sign in to comment.