node-emoji
Advanced tools
Comparing version 1.6.1 to 1.7.0
/*jslint node: true*/ | ||
require('string.prototype.codepointat'); | ||
var toArray = require('lodash.toarray'); | ||
@@ -80,5 +81,6 @@ "use strict"; | ||
* @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 ''; | ||
@@ -91,5 +93,11 @@ | ||
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; | ||
@@ -131,1 +139,23 @@ }) | ||
} | ||
var emojiToCode = Object.keys(Emoji.emoji).reduce(function(h,k) { | ||
return h[Emoji.emoji[k]] = k, h; | ||
}, {}); | ||
/** | ||
* unemojify a string (replace emoji with :emoji:) | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
Emoji.unemojify = function unemojify(str) { | ||
if (!str) return ''; | ||
var words = toArray(str); | ||
return words.map(function(word) { | ||
var emoji_text = emojiToCode[word]; | ||
if (emoji_text) { | ||
return ':'+emoji_text+':'; | ||
} | ||
return word; | ||
}).join(''); | ||
}; |
{ | ||
"name": "node-emoji", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "simple emoji support for node.js projects", | ||
@@ -26,2 +26,3 @@ "author": "Daniel Bugl <daniel.bugl@touchlay.com>", | ||
"dependencies": { | ||
"lodash.toarray": "^4.4.0", | ||
"string.prototype.codepointat": "^0.2.0" | ||
@@ -28,0 +29,0 @@ }, |
@@ -25,4 +25,35 @@ # node-emoji | ||
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:" | ||
``` | ||
## 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 | ||
@@ -29,0 +60,0 @@ 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 |
@@ -73,2 +73,11 @@ /*jslint node: true*/ | ||
}); | ||
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" />'); | ||
}); | ||
}); | ||
@@ -114,2 +123,22 @@ | ||
}); | ||
describe("unemojify(str)", function () { | ||
it("should parse emoji and replace them with :emoji:", function() { | ||
var coffee = emoji.unemojify('I ❤️ ☕️! - 😯⭐️😍 ::: test : : 👍+'); | ||
should.exist(coffee); | ||
coffee.should.be.exactly('I :heart: :coffee:! - :hushed::star::heart_eyes: ::: test : : :thumbsup:+'); | ||
}) | ||
it("should leave unknown emoji", function () { | ||
var coffee = emoji.unemojify('I ⭐️ :another_one: 🥕'); | ||
should.exist(coffee); | ||
coffee.should.be.exactly('I :star: :another_one: 🥕'); | ||
}); | ||
it("should parse a complex emoji like woman-kiss-woman and replace it with :woman-kiss-woman:", function() { | ||
var coffee = emoji.unemojify('I love 👩❤️💋👩'); | ||
should.exist(coffee); | ||
coffee.should.be.exactly('I love :woman-kiss-woman:'); | ||
}) | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150188
1593
74
2
+ Addedlodash.toarray@^4.4.0
+ Addedlodash.toarray@4.4.0(transitive)