From d4d92f35754b5363a831d77153324e68488a21db Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Sun, 16 Jul 2017 17:44:23 +0200 Subject: [PATCH 1/3] Add failing tests for `emoji.hasEmoji` --- test/emoji.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/emoji.js b/test/emoji.js index 3c9ded4..846f76e 100644 --- a/test/emoji.js +++ b/test/emoji.js @@ -215,4 +215,36 @@ describe("emoji.js", function () { should.not.exists(result); }) }); + + describe('hasEmoji', function() { + it('Should be able to check a emoji by :name:', function() { + var result = emoji.hasEmoji(':heart:'); + result.should.equal(true) + }); + + it('Should be able to check a emoji by name', function() { + var result = emoji.hasEmoji('heart'); + result.should.equal(true); + }); + + it('Should be able to check a emoji by code text form)', function() { + var result = emoji.hasEmoji('❤'); + result.should.equal(true); + }); + + it('Should be able to check a emoji by code in variant form', function() { + var result = emoji.hasEmoji('❤️'); + result.should.equal(true); + }); + + it('Should return false for unknown emoji names', function() { + var result = emoji.hasEmoji(':pizza-kiss-coffee:'); + result.should.equal(false); + }); + + it('Should return false for unknown emoji codes', function() { + var result = emoji.hasEmoji('🍕❤️‍💋‍☕'); + result.should.equal(false); + }); + }); }); From e22be7fd4c2fd0dda64e2894ee1422b3ae244e52 Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Sun, 16 Jul 2017 17:45:04 +0200 Subject: [PATCH 2/3] Add Emoji.hasEmoji methods --- lib/emoji.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/emoji.js b/lib/emoji.js index b705107..e0cc577 100644 --- a/lib/emoji.js +++ b/lib/emoji.js @@ -137,6 +137,36 @@ Emoji.findByCode = function get(code) { return name ? ({ emoji: emojiByName[name], key: name }) : undefined; }; + +/** + * Check if an emoji is known by this library + * @param {string} nameOrCode The emoji to validate, either `coffee`, `:coffee:` or `☕`; + * @return {object} + */ +Emoji.hasEmoji = function get(nameOrCode) { + return Emoji.hasEmojiByName(nameOrCode) || Emoji.hasEmojiByCode(nameOrCode); +}; + +/** + * Check if an emoji with given name is known by this library + * @param {string} name The emoji to validate either `coffee` or `:coffee:`; + * @return {object} + */ +Emoji.hasEmojiByName = function get(name) { + var result = Emoji.findByName(name); + return !!result && result.key === stripColons(name); +}; + +/** + * Check if a given emoji is known by this library + * @param {string} code The emoji to validate; for example `☕` or `☔` + * @return {object} + */ +Emoji.hasEmojiByCode = function get(code) { + var result = Emoji.findByCode(code); + return !!result && stripNSB(result.emoji) === stripNSB(code); +}; + /** * get emoji name from code * @param {string} emoji From 7d0d165c5e1aa261d7209f910dbd0dcbd3420acf Mon Sep 17 00:00:00 2001 From: Stephan Meijer Date: Sun, 16 Jul 2017 17:46:43 +0200 Subject: [PATCH 3/3] Update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index fd9af06..57c47fd 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ emoji.search('cof') // returns an array of objects with matching emoji's. `[{ em emoji.unemojify('I ❤️ 🍕') // replaces the actual emoji with :emoji:, in this case: returns "I :heart: :pizza:" emoji.find('🍕'); // Find the `pizza` emoji, and returns `({ emoji: '🍕', key: 'pizza' })`; emoji.find('pizza'); // Find the `pizza` emoji, and returns `({ emoji: '🍕', key: 'pizza' })`; +emoji.hasEmoji('🍕'); // Validate if this library knows an emoji like `🍕` +emoji.hasEmoji('pizza'); // Validate if this library knowns a emoji with the name `pizza` ``` ## Options