From f8fad797abb86583dab7de3accb14617f3b95be8 Mon Sep 17 00:00:00 2001 From: Tim Ruffles Date: Fri, 7 Jul 2017 15:54:21 +0100 Subject: [PATCH 1/3] Revert "Revert "Merge pull request #41 from shivkanthb/unemojify"" This reverts commit bfb2e78b5b4652d45e6f6b936d930da829023da1. --- .gitignore | 2 +- README.md | 1 + lib/emoji.js | 28 ++++++++++++++++++++++++++++ package.json | 3 ++- test/emoji.js | 20 ++++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 8262c2d..335e141 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ node_modules *.sublime-workspace *.txt test.js -coverage +coverage \ No newline at end of file diff --git a/README.md b/README.md index 53aa787..fc1adbd 100644 --- a/README.md +++ b/README.md @@ -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:" ``` ## Adding new emoji diff --git a/lib/emoji.js b/lib/emoji.js index 8141f07..1046eea 100644 --- a/lib/emoji.js +++ b/lib/emoji.js @@ -1,5 +1,6 @@ /*jslint node: true*/ require('string.prototype.codepointat'); +var toArray = require('lodash.toarray'); "use strict"; @@ -126,3 +127,30 @@ Emoji.search = function search(str) { }; }); } + +Object.prototype.getKeyByValue = function(value) { + for (var prop in this) { + if (this.hasOwnProperty(prop)) { + if (this[prop] === value) { + return prop; + } + } + } +} + +/** + * 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 = Emoji.emoji.getKeyByValue(word); + if (emoji_text) { + return ':'+emoji_text+':'; + } + return word; + }).join(''); +}; diff --git a/package.json b/package.json index 19df9e6..7162b49 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-emoji", - "version": "1.6.1", + "version": "1.6.0", "description": "simple emoji support for node.js projects", "author": "Daniel Bugl ", "repository": { @@ -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": { diff --git a/test/emoji.js b/test/emoji.js index a77687a..4210dba 100644 --- a/test/emoji.js +++ b/test/emoji.js @@ -110,4 +110,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 : : :+1:+'); + }) + + 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:'); + }) + }); }); From 42dc2b55de97c25a8f809124e7954de7f79e0ad3 Mon Sep 17 00:00:00 2001 From: Tim Ruffles Date: Fri, 7 Jul 2017 15:56:05 +0100 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20remove=20unnecessary=20monkey=20punc?= =?UTF-8?q?h=20=F0=9F=99=88=F0=9F=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - also bumped the version a minor, as new functionality is added --- lib/emoji.js | 10 +++++----- package.json | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/emoji.js b/lib/emoji.js index 1046eea..459a606 100644 --- a/lib/emoji.js +++ b/lib/emoji.js @@ -128,10 +128,10 @@ Emoji.search = function search(str) { }); } -Object.prototype.getKeyByValue = function(value) { - for (var prop in this) { - if (this.hasOwnProperty(prop)) { - if (this[prop] === value) { +function getKeyByValue(object, value) { + for (var prop in object) { + if (object.hasOwnProperty(prop)) { + if (object[prop] === value) { return prop; } } @@ -147,7 +147,7 @@ Emoji.unemojify = function unemojify(str) { if (!str) return ''; var words = toArray(str); return words.map(function(word) { - var emoji_text = Emoji.emoji.getKeyByValue(word); + var emoji_text = getKeyByValue(Emoji.emoji, word); if (emoji_text) { return ':'+emoji_text+':'; } diff --git a/package.json b/package.json index 7162b49..02f393f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-emoji", - "version": "1.6.0", + "version": "1.7.0", "description": "simple emoji support for node.js projects", "author": "Daniel Bugl ", "repository": { From bddce1e075ca735e98d8b14d8e1a4dea02a32e7b Mon Sep 17 00:00:00 2001 From: Tim Ruffles Date: Fri, 7 Jul 2017 16:13:45 +0100 Subject: [PATCH 3/3] refactor: change O(N*M) performance to O(N) --- lib/emoji.js | 15 +++++---------- test/emoji.js | 2 +- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/emoji.js b/lib/emoji.js index 459a606..d6bc19f 100644 --- a/lib/emoji.js +++ b/lib/emoji.js @@ -128,15 +128,9 @@ Emoji.search = function search(str) { }); } -function getKeyByValue(object, value) { - for (var prop in object) { - if (object.hasOwnProperty(prop)) { - if (object[prop] === value) { - return prop; - } - } - } -} +var emojiToCode = Object.keys(Emoji.emoji).reduce(function(h,k) { + return h[Emoji.emoji[k]] = k, h; +}, {}); /** * unemojify a string (replace emoji with :emoji:) @@ -146,8 +140,9 @@ function getKeyByValue(object, value) { Emoji.unemojify = function unemojify(str) { if (!str) return ''; var words = toArray(str); + return words.map(function(word) { - var emoji_text = getKeyByValue(Emoji.emoji, word); + var emoji_text = emojiToCode[word]; if (emoji_text) { return ':'+emoji_text+':'; } diff --git a/test/emoji.js b/test/emoji.js index 4210dba..b955dad 100644 --- a/test/emoji.js +++ b/test/emoji.js @@ -115,7 +115,7 @@ describe("emoji.js", 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 : : :+1:+'); + coffee.should.be.exactly('I :heart: :coffee:! - :hushed::star::heart_eyes: ::: test : : :thumbsup:+'); }) it("should leave unknown emoji", function () {