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 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 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); + }); + }); });