Skip to content

Commit

Permalink
Merge pull request #54 from smeijer/feature/emoji-hasEmoji
Browse files Browse the repository at this point in the history
Feature/emoji-hasEmoji
  • Loading branch information
omnidan authored Jul 21, 2017
2 parents 6961d5e + 7d0d165 commit 6eeaecd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions lib/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions test/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 6eeaecd

Please sign in to comment.