Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to strip the keys from string #101

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand Down
10 changes: 9 additions & 1 deletion lib/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /:.*?\:/;
Copy link
Collaborator

@JoshuaKGoldberg JoshuaKGoldberg May 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Style] The ? is unnecessary. .*? says:

  • .: any character
  • *: any number of times (including 0)
  • ?: optionally

I'm betting the regular expression shouldn't match ::. So it can be simplified to:

Suggested change
var removeNameRegex = /:.*?\:/;
var removeNameRegex = /:.+:/;


/**
* regex to trim whitespace
* use instead of String.prototype.trim() for IE8 support
Expand Down Expand Up @@ -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);
};

Expand Down
10 changes: 10 additions & 0 deletions test/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ');
});
Copy link
Collaborator

@JoshuaKGoldberg JoshuaKGoldberg May 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Testing] Thinking about the test cases, there are some that should probably be added:

  • A string with :: somewhere in it
  • Colons that shouldn't be matched, like I: the great lover of coffee and I: coffee. You: tea.
  • A string that has emojis and :name:s

});
});