node-emoji
Advanced tools
Comparing version
@@ -13,2 +13,8 @@ /*jslint node: true*/ | ||
| /** | ||
| * regex to trim whitespace | ||
| * use instead of String.prototype.trim() for IE8 supprt | ||
| */ | ||
| var trimSpaceRegex = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; | ||
| /** | ||
| * Removes colons on either side | ||
@@ -202,3 +208,3 @@ * of the string if present | ||
| if (isMissing && typeof on_missing === 'function') { | ||
| return on_missing(emoji.substr(1, emoji.length-2)); | ||
| return on_missing(s); | ||
| } | ||
@@ -261,2 +267,38 @@ | ||
| /** | ||
| * replace emojis with replacement value | ||
| * @param {string} str | ||
| * @param {function|string} the string or callback function to replace the emoji with | ||
| * @param {boolean} should trailing whitespaces be cleaned? Defaults false | ||
| * @return {string} | ||
| */ | ||
| Emoji.replace = function replace (str, replacement, cleanSpaces) { | ||
| if (!str) return ''; | ||
| var replace = typeof replacement === 'function' ? replacement : function() { return replacement; }; | ||
| var words = toArray(str); | ||
| var replaced = words.map(function(word, idx) { | ||
| var emoji = Emoji.findByCode(word); | ||
| if (emoji && cleanSpaces && words[idx + 1] === ' ') { | ||
| words[idx + 1] = ''; | ||
| } | ||
| return emoji ? replace(emoji) : word; | ||
| }).join(''); | ||
| return cleanSpaces ? replaced.replace(trimSpaceRegex, '') : replaced; | ||
| }; | ||
| /** | ||
| * remove all emojis from a string | ||
| * @param {string} str | ||
| * @return {string} | ||
| */ | ||
| Emoji.strip = function strip (str) { | ||
| return Emoji.replace(str, '', true); | ||
| }; | ||
| module.exports = Emoji; |
| { | ||
| "name": "node-emoji", | ||
| "version": "1.8.0", | ||
| "version": "1.8.1", | ||
| "description": "simple emoji support for node.js projects", | ||
@@ -5,0 +5,0 @@ "author": "Daniel Bugl <daniel.bugl@touchlay.com>", |
@@ -26,6 +26,8 @@ # node-emoji | ||
| 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` | ||
| 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` | ||
| emoji.strip('⚠️ 〰️ 〰️ low disk space') // Strips the string from emoji's, in this case returns: "low disk space". | ||
| emoji.replace('⚠️ 〰️ 〰️ low disk space', (emoji) => `${emoji.key}:`) // Replace emoji's by callback method: "warning: low disk space" | ||
| ``` | ||
@@ -36,3 +38,3 @@ | ||
| ### onMissing | ||
| `emoji.emojify(str, onMissing)`; | ||
| `emoji.emojify(str, onMissing)` | ||
@@ -51,3 +53,3 @@ As second argument, `emojify` takes an handler to parse unknown emojis. Provide a function to add your own handler: | ||
| ### format | ||
| `emoji.emojify(str, onMissing, format)`; | ||
| `emoji.emojify(str, onMissing, format)` | ||
@@ -54,0 +56,0 @@ 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: |
@@ -250,2 +250,53 @@ /*jslint node: true*/ | ||
| }); | ||
| describe('replace', function() { | ||
| it('Should be able to strip emojis', function() { | ||
| var result = emoji.replace('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space', '', true); | ||
| result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space'); | ||
| }); | ||
| it('Should keep the trailing spaces when not explicitly told to clean', function() { | ||
| var result = emoji.replace('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space', ''); | ||
| result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space'); | ||
| }); | ||
| it('Should be able to strip a emoji by code text form', function() { | ||
| var result = emoji.replace('I ❤ coffee', '', true); | ||
| result.should.equal('I coffee'); | ||
| }); | ||
| it('Should be able to strip a emoji by code in variant form', function() { | ||
| var result = emoji.replace('I ❤️ cleaning', '', true); | ||
| result.should.equal('I cleaning'); | ||
| }); | ||
| it('Should be able to strip complex emojis', function() { | ||
| var result = emoji.replace('Where did this 👩❤️💋👩 happen?', '', true); | ||
| result.should.equal('Where did this happen?'); | ||
| }); | ||
| it('Should be able to strip flag emojis', function() { | ||
| var result = emoji.replace('There is no flag 🇲🇽', '', true); | ||
| result.should.equal('There is no flag'); | ||
| }); | ||
| it('Should be able to replace by callback function', function() { | ||
| var result = emoji.replace('There is no ⚠ on my hard drive', function (emoji) { | ||
| return emoji.key; | ||
| }); | ||
| result.should.equal('There is no warning on my hard drive'); | ||
| }); | ||
| it('Non existing complex emojis are known to be ignored', function() { | ||
| var result = emoji.replace('Some 🍕❤️💋☕ emoji', ''); | ||
| result.should.not.equal('Some emoji'); | ||
| }); | ||
| }); | ||
| describe('strip', function() { | ||
| it('Should be able to strip emojis', function() { | ||
| var result = emoji.strip('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space'); | ||
| result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space'); | ||
| }); | ||
| }); | ||
| }); |
160832
2.23%1837
4.26%78
2.63%