Skip to content

Commit

Permalink
Merge pull request #48 from smeijer/feature/add-format-option
Browse files Browse the repository at this point in the history
Add format argument to support wrapping of emojis
  • Loading branch information
omnidan authored Jul 10, 2017
2 parents bfb2e78 + 5e919bb commit 767cd90
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ emoji.random() // returns a random emoji + key, e.g. `{ emoji: '❤️', key: 'h
emoji.search('cof') // returns an array of objects with matching emoji's. `[{ emoji: '☕️', key: 'coffee' }, { emoji: ⚰', key: 'coffin'}]`
```

## Options

### onMissing
`emoji.emojify(str, onMissing)`;

As second argument, `emojify` takes an handler to parse unknown emojis. Provide a function to add your own handler:

```js
var onMissing = function (name) {
return name;
});

var emojified = emoji.emojify('I :unknown_emoji: :star: :another_one:', onMissing);
// emojified: I unknown_emoji ⭐️ another_one
```

### format
`emoji.emojify(str, onMissing, format)`;

As third argument, `emojify` takes an handler to wrap parsed emojis. Provide a function to place emojis in custom elements, and to apply your custom styling:

```js
var format = function (code, name) {
return '<img alt="' + code + '" src="' + name + '.png" />';
});

var emojified = emoji.emojify('I :unknown_emoji: :star: :another_one:', null, format);
// emojified: I <img alt="❤️" src="heart.png" /> <img alt="☕️" src="coffee.png" />
```

## Adding new emoji
Emoji come from js-emoji (Thanks a lot :thumbsup:). You can get a JSON file with all emoji here: https://raw.githubusercontent.com/omnidan/node-emoji/master/lib/emoji.json

Expand Down
9 changes: 8 additions & 1 deletion lib/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,26 @@ Emoji.which = function which(emoji_code) {
* emojify a string (replace :emoji: with an emoji)
* @param {string} str
* @param {function} on_missing (gets emoji name without :: and returns a proper emoji if no emoji was found)
* @param {function} format (wrap the returned emoji in a custom element)
* @return {string}
*/
Emoji.emojify = function emojify(str, on_missing) {
Emoji.emojify = function emojify(str, on_missing, format) {
if (!str) return '';

return str.split(parser) // parse emoji via regex
.map(function parseEmoji(s, i) {
// every second element is an emoji, e.g. "test :fast_forward:" -> [ "test ", "fast_forward" ]
if (i % 2 === 0) return s;
var emoji = Emoji._get(s);

if (emoji.indexOf(':') > -1 && typeof on_missing === 'function') {
return on_missing(emoji.substr(1, emoji.length-2));
}

if (typeof format === 'function') {
return format(emoji, s);
}

return emoji;
})
.join('') // convert back to string
Expand Down
9 changes: 9 additions & 0 deletions test/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ describe("emoji.js", function () {
should.exist(coffee);
coffee.should.be.exactly('I unknown_emoji ⭐️ another_one');
});

it("should wrap emoji using provided cb function", function () {
var coffee = emoji.emojify('I :heart: :coffee:', null, function(code, name) {
return '<img alt="' + code + '" src="' + name + '.png" />';
});

should.exist(coffee);
coffee.should.be.exactly('I <img alt="❤️" src="heart.png" /> <img alt="☕️" src="coffee.png" />');
});
});

it("should return an emoji code", function () {
Expand Down

0 comments on commit 767cd90

Please sign in to comment.