From bba9779d699db76d017800e163568aa88dd5ce9b Mon Sep 17 00:00:00 2001 From: Anders Borud Date: Wed, 15 Apr 2015 13:26:52 +0000 Subject: [PATCH 1/3] Added emoji convertion for irc. Fixes #10 in source. --- lib/bot.js | 8 ++++++++ lib/emoji.js | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 lib/emoji.js diff --git a/lib/bot.js b/lib/bot.js index 7db02a6..0933814 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -4,6 +4,7 @@ var logger = require('winston'); var Slack = require('slack-client'); var errors = require('./errors'); var validateChannelMapping = require('./validators').validateChannelMapping; +var emojis = require('./emoji').emojis; var REQUIRED_FIELDS = ['server', 'nickname', 'channelMapping', 'token']; @@ -118,6 +119,13 @@ Bot.prototype.parseText = function(text) { return '<' + label + '>'; } return '<' + command + '>'; + }) + .replace(/\:(\w+)\:/g, function(match) { + var clean = match.replace(/\:/g,''); + if (clean in emojis) { + return emojis[clean]; + } + return match; }); }; diff --git a/lib/emoji.js b/lib/emoji.js new file mode 100644 index 0000000..5f17a71 --- /dev/null +++ b/lib/emoji.js @@ -0,0 +1,22 @@ +var emojis = { +smile: ":)", +simple_smile: ":)", +smiley: ":-)", +grin: ":D", +wink: ";)", +smirk: ";)", +blush: ":$", +stuck_out_tongue: ":P", +stuck_out_tongue_winking_eye: ";P", +stuck_out_tongue_closed_eyes: "", +disappointed: ":(", +astonished: ":O", +open_mouth: ":O", +heart: "<3", +broken_heart: " Date: Thu, 16 Apr 2015 16:23:58 +0000 Subject: [PATCH 2/3] Changed emoji file to json. --- lib/emoji.js | 22 ---------------------- lib/emoji.json | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 22 deletions(-) delete mode 100644 lib/emoji.js create mode 100644 lib/emoji.json diff --git a/lib/emoji.js b/lib/emoji.js deleted file mode 100644 index 5f17a71..0000000 --- a/lib/emoji.js +++ /dev/null @@ -1,22 +0,0 @@ -var emojis = { -smile: ":)", -simple_smile: ":)", -smiley: ":-)", -grin: ":D", -wink: ";)", -smirk: ";)", -blush: ":$", -stuck_out_tongue: ":P", -stuck_out_tongue_winking_eye: ";P", -stuck_out_tongue_closed_eyes: "", -disappointed: ":(", -astonished: ":O", -open_mouth: ":O", -heart: "<3", -broken_heart: " Date: Thu, 16 Apr 2015 16:25:14 +0000 Subject: [PATCH 3/3] Cleaned up some code and added test for emoji parsing. --- lib/bot.js | 9 ++++----- test/bot.test.js | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/bot.js b/lib/bot.js index 0933814..86d445b 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -4,7 +4,7 @@ var logger = require('winston'); var Slack = require('slack-client'); var errors = require('./errors'); var validateChannelMapping = require('./validators').validateChannelMapping; -var emojis = require('./emoji').emojis; +var emojis = require('./emoji'); var REQUIRED_FIELDS = ['server', 'nickname', 'channelMapping', 'token']; @@ -120,10 +120,9 @@ Bot.prototype.parseText = function(text) { } return '<' + command + '>'; }) - .replace(/\:(\w+)\:/g, function(match) { - var clean = match.replace(/\:/g,''); - if (clean in emojis) { - return emojis[clean]; + .replace(/\:(\w+)\:/g, function(match, emoji) { + if (emoji in emojis) { + return emojis[emoji]; } return match; }); diff --git a/test/bot.test.js b/test/bot.test.js index d61a7bf..fffc779 100644 --- a/test/bot.test.js +++ b/test/bot.test.js @@ -92,4 +92,9 @@ describe('Bot', function() { this.bot.parseText(' ') .should.equal(' '); }); + + it('should parse emojis correctly', function() { + this.bot.parseText(':smile:').should.equal(':)'); + this.bot.parseText(':train:').should.equal(':train:'); + }); });