Last active
April 11, 2017 15:16
-
-
Save cooperka/bde4a559e15efe30919249442a145374 to your computer and use it in GitHub Desktop.
"Pure emoji string" detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import emojiRegexCreator from 'emoji-regex'; | |
const emojiRegex = emojiRegexCreator(); | |
export default { | |
isPureEmojiString(text) { | |
if (!text || !text.trim()) return false; | |
return text.replace(emojiRegex, '').trim() === ''; | |
}, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Benchmarked using http://jsben.ch/#/qpvoO to compare a few different regexes, this is about as fast as I could get it.