You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8 KiB

  1. // @preval
  2. // http://www.unicode.org/Public/emoji/5.0/emoji-test.txt
  3. // This file contains the compressed version of the emoji data from
  4. // both emoji_map.json and from emoji-mart's emojiIndex and data objects.
  5. // It's designed to be emitted in an array format to take up less space
  6. // over the wire.
  7. const { unicodeToFilename } = require('./unicode_to_filename');
  8. const { unicodeToUnifiedName } = require('./unicode_to_unified_name');
  9. const emojiMap = require('./emoji_map.json');
  10. const { emojiIndex } = require('emoji-mart');
  11. const { default: emojiMartData } = require('emoji-mart/dist/data');
  12. const excluded = ['®', '©', '™'];
  13. const skins = ['🏻', '🏼', '🏽', '🏾', '🏿'];
  14. const shortcodeMap = {};
  15. const shortCodesToEmojiData = {};
  16. const emojisWithoutShortCodes = [];
  17. Object.keys(emojiIndex.emojis).forEach(key => {
  18. shortcodeMap[emojiIndex.emojis[key].native] = emojiIndex.emojis[key].id;
  19. });
  20. const stripModifiers = unicode => {
  21. skins.forEach(tone => {
  22. unicode = unicode.replace(tone, '');
  23. });
  24. return unicode;
  25. };
  26. Object.keys(emojiMap).forEach(key => {
  27. if (excluded.includes(key)) {
  28. delete emojiMap[key];
  29. return;
  30. }
  31. const normalizedKey = stripModifiers(key);
  32. let shortcode = shortcodeMap[normalizedKey];
  33. if (!shortcode) {
  34. shortcode = shortcodeMap[normalizedKey + '\uFE0F'];
  35. }
  36. const filename = emojiMap[key];
  37. const filenameData = [key];
  38. if (unicodeToFilename(key) !== filename) {
  39. // filename can't be derived using unicodeToFilename
  40. filenameData.push(filename);
  41. }
  42. if (typeof shortcode === 'undefined') {
  43. emojisWithoutShortCodes.push(filenameData);
  44. } else {
  45. if (!Array.isArray(shortCodesToEmojiData[shortcode])) {
  46. shortCodesToEmojiData[shortcode] = [[]];
  47. }
  48. shortCodesToEmojiData[shortcode][0].push(filenameData);
  49. }
  50. });
  51. Object.keys(emojiIndex.emojis).forEach(key => {
  52. const { native } = emojiIndex.emojis[key];
  53. let { short_names, search, unified } = emojiMartData.emojis[key];
  54. if (short_names[0] !== key) {
  55. throw new Error('The compresser expects the first short_code to be the ' +
  56. 'key. It may need to be rewritten if the emoji change such that this ' +
  57. 'is no longer the case.');
  58. }
  59. short_names = short_names.slice(1); // first short name can be inferred from the key
  60. const searchData = [native, short_names, search];
  61. if (unicodeToUnifiedName(native) !== unified) {
  62. // unified name can't be derived from unicodeToUnifiedName
  63. searchData.push(unified);
  64. }
  65. shortCodesToEmojiData[key].push(searchData);
  66. });
  67. // JSON.parse/stringify is to emulate what @preval is doing and avoid any
  68. // inconsistent behavior in dev mode
  69. module.exports = JSON.parse(JSON.stringify([
  70. shortCodesToEmojiData,
  71. emojiMartData.skins,
  72. emojiMartData.categories,
  73. emojiMartData.short_names,
  74. emojisWithoutShortCodes,
  75. ]));