Skip to content

Commit

Permalink
Merge pull request #53 from smeijer/feature/emoji-find
Browse files Browse the repository at this point in the history
Feature/emoji-find
  • Loading branch information
omnidan authored Jul 21, 2017
2 parents 62e7126 + b714fdd commit 6961d5e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ emoji.emojify('I :heart: :coffee:!') // replaces all :emoji: with the actual emo
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:"
emoji.find('🍕'); // Find the `pizza` emoji, and returns `({ emoji: '🍕', key: 'pizza' })`;
emoji.find('pizza'); // Find the `pizza` emoji, and returns `({ emoji: '🍕', key: 'pizza' })`;
```

## Options
Expand Down
34 changes: 34 additions & 0 deletions lib/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,40 @@ Emoji.get = function get(emoji) {
return Emoji._get(emoji);
};

/**
* find the emoji by either code or name
* @param {string} nameOrCode The emoji to find, either `coffee`, `:coffee:` or `☕`;
* @return {object}
*/
Emoji.find = function get(nameOrCode) {
return Emoji.findByName(nameOrCode) || Emoji.findByCode(nameOrCode);
};

/**
* find the emoji by name
* @param {string} name The emoji to find either `coffee` or `:coffee:`;
* @return {object}
*/
Emoji.findByName = function get(name) {
var stripped = stripColons(name);
var emoji = emojiByName[stripped];

return emoji ? ({ emoji: emoji, key: stripped }) : undefined;
};

/**
* find the emoji by code (emoji)
* @param {string} code The emoji to find; for example `☕` or `☔`
* @return {object}
*/
Emoji.findByCode = function get(code) {
var stripped = stripNSB(code);
var name = emojiByCode[stripped];

// lookup emoji to ensure the Variant Form is returned
return name ? ({ emoji: emojiByName[name], key: name }) : undefined;
};

/**
* get emoji name from code
* @param {string} emoji
Expand Down
25 changes: 25 additions & 0 deletions test/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,29 @@ describe("emoji.js", function () {
flags.should.be.exactly('The flags of :flag-mx: and :flag-ma: are not the same');
});
});

describe('find emoji', function() {
it('Should be able to find a emoji by :name:', function() {
var result = emoji.find(':heart:')
should.exists(result);
result.should.eql({ emoji: '❤️', key: 'heart' });
});

it('Should be able to find an emoji by name', function() {
var result = emoji.find('heart');
should.exists(result);
result.should.eql({ emoji: '❤️', key: 'heart' });
});

it('Should be able to find an emoji by code', function() {
var result = emoji.find('❤');
should.exists(result);
result.should.eql({ emoji: '❤️', key: 'heart' });
});

it('Should return `undefined` for unknown emojis', function() {
var result = emoji.find('unknown_emoji');
should.not.exists(result);
})
});
});

0 comments on commit 6961d5e

Please sign in to comment.