From 334284cbd6f7972c50059dbc2ab2c115d0a9d111 Mon Sep 17 00:00:00 2001 From: Iuri Wollmann Date: Wed, 2 Dec 2020 23:15:20 -0300 Subject: [PATCH] Possibility to strip the keys from string --- README.md | 1 + lib/emoji.js | 10 +++++++++- test/emoji.js | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba29824..d72b1af 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ emoji.find('pizza') // Find the `pizza` emoji, and returns `({ emoji: '🍕', ke 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.strip('I :heart: coffee!', true) // Strips the keys from string, in this case returns: "I coffee!". emoji.replace('⚠️ 〰️ 〰️ low disk space', (emoji) => `${emoji.key}:`) // Replace emoji's by callback method: "warning: low disk space" ``` diff --git a/lib/emoji.js b/lib/emoji.js index 751dc25..05fe03c 100644 --- a/lib/emoji.js +++ b/lib/emoji.js @@ -9,6 +9,11 @@ var emojiByName = require('./emoji.json'); */ var emojiNameRegex = /:([a-zA-Z0-9_\-\+]+):/g; +/** + * regex to remove emoji key strings, e.g. "":coffee: coffee" -> coffee + */ +var removeNameRegex = /:.*?\:/; + /** * regex to trim whitespace * use instead of String.prototype.trim() for IE8 support @@ -294,9 +299,12 @@ Emoji.replace = function replace (str, replacement, cleanSpaces) { /** * remove all emojis from a string * @param {string} str + * @param {boolean} onlyKeys strips only the keys on string, e.g. ":coffee: coffe" -> " coffe" * @return {string} */ -Emoji.strip = function strip (str) { +Emoji.strip = function strip (str, onlyKeys = false) { + if (onlyKeys) return str.split(removeNameRegex).join(''); + return Emoji.replace(str, '', true); }; diff --git a/test/emoji.js b/test/emoji.js index 121beb1..621beff 100644 --- a/test/emoji.js +++ b/test/emoji.js @@ -303,5 +303,15 @@ describe("emoji.js", 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'); }); + + it('Should be able to strip only emojis names', function() { + var result = emoji.strip('I :love: cleaning', true); + result.should.equal('I cleaning'); + }); + + it('Should be able to strip all emojis names', function() { + var result = emoji.strip('I :heart: cleaning and :coffee:', true); + result.should.equal('I cleaning and '); + }); }); });