diff --git a/README.md b/README.md index 57c47fd..5fd0ea8 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ emoji.find('🍕'); // Find the `pizza` emoji, and returns `({ emoji: '🍕', ke 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" ``` ## Options diff --git a/lib/emoji.js b/lib/emoji.js index dcf724f..0aac29b 100644 --- a/lib/emoji.js +++ b/lib/emoji.js @@ -9,6 +9,12 @@ var emojiByName = require('./emoji.json'); */ var emojiNameRegex = /:([a-zA-Z0-9_\-\+]+):/g; +/** + * 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 * of the string if present @@ -198,7 +204,7 @@ Emoji.emojify = function emojify (str, on_missing, format) { var isMissing = emoji.indexOf(':') > -1; if (isMissing && typeof on_missing === 'function') { - return on_missing(emoji.substr(1, emoji.length-2)); + return on_missing(s); } if (!isMissing && typeof format === 'function') { @@ -256,4 +262,40 @@ Emoji.unemojify = function unemojify (str) { }).join(''); }; +/** + * 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; diff --git a/test/emoji.js b/test/emoji.js index 846f76e..2ead227 100644 --- a/test/emoji.js +++ b/test/emoji.js @@ -247,4 +247,55 @@ describe("emoji.js", function () { result.should.equal(false); }); }); + + 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'); + }); + }); });