Skip to content

Commit 0862eb0

Browse files
authored
Merge pull request #56 from smeijer/feature/emoji-strip
Feature/emoji strip
2 parents def1f2c + b102b26 commit 0862eb0

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ emoji.find('πŸ•'); // Find the `pizza` emoji, and returns `({ emoji: 'πŸ•', ke
2727
emoji.find('pizza'); // Find the `pizza` emoji, and returns `({ emoji: 'πŸ•', key: 'pizza' })`;
2828
emoji.hasEmoji('πŸ•'); // Validate if this library knows an emoji like `πŸ•`
2929
emoji.hasEmoji('pizza'); // Validate if this library knowns a emoji with the name `pizza`
30+
emoji.strip('⚠️ 〰️ 〰️ low disk space'); // Strips the string from emoji's, in this case returns: "low disk space".
31+
emoji.replace('⚠️ 〰️ 〰️ low disk space', (emoji) => `${emoji.key}:`); // Replace emoji's by callback method: "warning: low disk space"
3032
```
3133

3234
## Options

β€Žlib/emoji.jsβ€Ž

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ var emojiByName = require('./emoji.json');
99
*/
1010
var emojiNameRegex = /:([a-zA-Z0-9_\-\+]+):/g;
1111

12+
/**
13+
* regex to trim whitespace
14+
* use instead of String.prototype.trim() for IE8 supprt
15+
*/
16+
var trimSpaceRegex = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
17+
1218
/**
1319
* Removes colons on either side
1420
* of the string if present
@@ -198,7 +204,7 @@ Emoji.emojify = function emojify (str, on_missing, format) {
198204
var isMissing = emoji.indexOf(':') > -1;
199205

200206
if (isMissing && typeof on_missing === 'function') {
201-
return on_missing(emoji.substr(1, emoji.length-2));
207+
return on_missing(s);
202208
}
203209

204210
if (!isMissing && typeof format === 'function') {
@@ -256,4 +262,40 @@ Emoji.unemojify = function unemojify (str) {
256262
}).join('');
257263
};
258264

265+
/**
266+
* replace emojis with replacement value
267+
* @param {string} str
268+
* @param {function|string} the string or callback function to replace the emoji with
269+
* @param {boolean} should trailing whitespaces be cleaned? Defaults false
270+
* @return {string}
271+
*/
272+
Emoji.replace = function replace (str, replacement, cleanSpaces) {
273+
if (!str) return '';
274+
275+
var replace = typeof replacement === 'function' ? replacement : function() { return replacement; };
276+
var words = toArray(str);
277+
278+
var replaced = words.map(function(word, idx) {
279+
var emoji = Emoji.findByCode(word);
280+
281+
if (emoji && cleanSpaces && words[idx + 1] === ' ') {
282+
words[idx + 1] = '';
283+
}
284+
285+
return emoji ? replace(emoji) : word;
286+
}).join('');
287+
288+
return cleanSpaces ? replaced.replace(trimSpaceRegex, '') : replaced;
289+
};
290+
291+
292+
/**
293+
* remove all emojis from a string
294+
* @param {string} str
295+
* @return {string}
296+
*/
297+
Emoji.strip = function strip (str) {
298+
return Emoji.replace(str, '', true);
299+
};
300+
259301
module.exports = Emoji;

β€Žtest/emoji.jsβ€Ž

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,55 @@ describe("emoji.js", function () {
247247
result.should.equal(false);
248248
});
249249
});
250+
251+
describe('replace', function() {
252+
it('Should be able to strip emojis', function() {
253+
var result = emoji.replace('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space', '', true);
254+
result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space');
255+
});
256+
257+
it('Should keep the trailing spaces when not explicitly told to clean', function() {
258+
var result = emoji.replace('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space', '');
259+
result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space');
260+
});
261+
262+
it('Should be able to strip a emoji by code text form', function() {
263+
var result = emoji.replace('I ❀ coffee', '', true);
264+
result.should.equal('I coffee');
265+
});
266+
267+
it('Should be able to strip a emoji by code in variant form', function() {
268+
var result = emoji.replace('I ❀️ cleaning', '', true);
269+
result.should.equal('I cleaning');
270+
});
271+
272+
it('Should be able to strip complex emojis', function() {
273+
var result = emoji.replace('Where did this πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘© happen?', '', true);
274+
result.should.equal('Where did this happen?');
275+
});
276+
277+
it('Should be able to strip flag emojis', function() {
278+
var result = emoji.replace('There is no flag πŸ‡²πŸ‡½', '', true);
279+
result.should.equal('There is no flag');
280+
});
281+
282+
it('Should be able to replace by callback function', function() {
283+
var result = emoji.replace('There is no ⚠ on my hard drive', function (emoji) {
284+
return emoji.key;
285+
});
286+
result.should.equal('There is no warning on my hard drive');
287+
});
288+
289+
it('Non existing complex emojis are known to be ignored', function() {
290+
var result = emoji.replace('Some πŸ•β€οΈβ€πŸ’‹β€β˜• emoji', '');
291+
result.should.not.equal('Some emoji');
292+
});
293+
});
294+
295+
describe('strip', function() {
296+
it('Should be able to strip emojis', function() {
297+
var result = emoji.strip('Host: eseaps001 Addr: 10.XX.XX.XX: - ⚠️ 〰️ 〰️ low disk space');
298+
result.should.equal('Host: eseaps001 Addr: 10.XX.XX.XX: - low disk space');
299+
});
300+
});
250301
});

0 commit comments

Comments
Β (0)