Skip to content

Instantly share code, notes, and snippets.

@cooperka
Last active April 11, 2017 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cooperka/bde4a559e15efe30919249442a145374 to your computer and use it in GitHub Desktop.
Save cooperka/bde4a559e15efe30919249442a145374 to your computer and use it in GitHub Desktop.
"Pure emoji string" detection
import emojiRegexCreator from 'emoji-regex';
const emojiRegex = emojiRegexCreator();
export default {
isPureEmojiString(text) {
if (!text || !text.trim()) return false;
return text.replace(emojiRegex, '').trim() === '';
},
};
import { forOwn } from 'lodash';
import emoji from 'node-emoji';
import utils from '../utils';
const testStrings = {
null: false,
undefined: false,
'': false,
' ': false,
'\t': false,
'hi': false,
'Not emoji': false,
'Not pure emoji :blush:': false,
':wink:': true,
':wink:!': false,
':notarealemoji:': false,
':sunglasses: :cactus:': true,
' \t :sunglasses: \t :cactus: \t ': true,
'\u{1F609}': true, // :wink:
'\u{1F60E} \u{1F335}': true, // :sunglasses: :cactus:
'\u{0041}': false, // 'A'
};
describe('emoji utils', () => {
forOwn(testStrings, (isPure, str) => {
it(`recognizes '${str}' as ${isPure ? 'a' : 'NOT a'} pure emoji string`, () => {
const emojiString = emoji.emojify(str);
expect(utils.isPureEmojiString(emojiString)).toBe(isPure);
});
});
});
@cooperka
Copy link
Author

Benchmarked using http://jsben.ch/#/qpvoO to compare a few different regexes, this is about as fast as I could get it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment