From 862d75b8ab0a562d86d32ec835f53b1addae74f1 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 19 Oct 2017 12:36:53 +0300 Subject: [PATCH] Add script to generate emoji map from emojione data --- scripts/generate-emoji.js | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 scripts/generate-emoji.js diff --git a/scripts/generate-emoji.js b/scripts/generate-emoji.js new file mode 100644 index 00000000..de02eed1 --- /dev/null +++ b/scripts/generate-emoji.js @@ -0,0 +1,70 @@ +"use strict"; + +const request = require("request"); +const path = require("path"); +const fs = require("fs"); +const fuzzy = require("fuzzy"); + +request.get({ + url: "https://raw.githubusercontent.com/emojione/emojione/master/emoji_strategy.json", + json: true, +}, (error, response, emojiStrategy) => { + const emojiMap = {}; + + for (const key in emojiStrategy) { + if (emojiStrategy.hasOwnProperty(key)) { + const shortname = prepareShortName(emojiStrategy[key].shortname); + + // Skip tones, at least for now + if (shortname.indexOf("tone") > -1) { + continue; + } + + const unicode = stringToUnicode(key); + + emojiMap[shortname] = unicode; + + for (let alternative of emojiStrategy[key].shortname_alternates) { + alternative = prepareShortName(alternative); + + if (fuzzy.test(shortname, alternative) || fuzzy.test(alternative, shortname)) { + continue; + } + + emojiMap[alternative] = unicode; + } + } + } + + const output = JSON.stringify(emojiMap, null, 2) + "\n"; + + fs.writeFileSync(path.resolve(path.join( + __dirname, + "..", + "client", + "js", + "libs", + "simplemap.json" + )), output); +}); + +function stringToUnicode(key) { + return key + .split("-") + .map((c) => String.fromCodePoint(`0x${c}`)) + .join(""); +} + +function prepareShortName(shortname) { + if (shortname === ":-1:") { + // We replace dashes, but should keep :-1: working + return "-1"; + } else if (shortname === ":e-mail:") { + // :email: exists as an alternative, should figure out how to use it instead + return "email"; + } + + return shortname + .slice(1, -1) + .replace("-", "_"); +}