Sometimes, when you work with emojis πππ€πβ they can be a real pain in the ass. One physical symbol can equal up to four Unicode characters, so when counting strings & encoding emojis into a valid format for your favourite legacy database, these fun little icons can physically make you π‘
This article was originally published to my blog, then to Medium, and back again.
But then you look at Slack, and that handles emoji with styleβ¦
I love how Slack handles emoji. Transforming :tada:
into π and :sunglasses:
into π and so on. So, the engineer within me decided to find a NPM module to do this for me.
And after several hours, the closest I could find was Mojier, published two years ago, which mapped emojis to their shortcodes and back again. But being two years out of date means the library was half-complete, and the lookups were slow (for(var p in emoji)
), so ten minutes later I had emoji-shorts online β
Using this to translate strings containing emoji is easy:
var emoji = require('emoji-shorts');
console.log(emoji.toPlain('These violent delights have violent ends π'));
// These violent delights have violent ends :smiling_imp:
console.log(emoji.toRich('The most elegant parts of me weren\'t written by you :thinking:'));
// The most elegant parts of me weren't written by you π€
The emojis & relevant short-codes came from Github themselves, with a little post-processing to make the lookups a little faster. Ideally this would be used when saving user-input & when fetching user-input, so your ORM / database engine of choice is none the wiser π
If you have any questions, or you wish to submit a PR (of which all are welcome, we all β€οΈ emojis!) then please feel free to get involved: https://github.com/car-throttle/emoji-shorts
In the meantime, Merry Christmas πππ