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.

122 lines
3.4 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 { uncompress: emojiMartUncompress } = require('emoji-mart/dist/utils/data');
  12. let data = require('emoji-mart/data/all.json');
  13. if(data.compressed) {
  14. data = emojiMartUncompress(data);
  15. }
  16. const emojiMartData = data;
  17. const excluded = ['®', '©', '™'];
  18. const skinTones = ['🏻', '🏼', '🏽', '🏾', '🏿'];
  19. const shortcodeMap = {};
  20. const shortCodesToEmojiData = {};
  21. const emojisWithoutShortCodes = [];
  22. Object.keys(emojiIndex.emojis).forEach(key => {
  23. let emoji = emojiIndex.emojis[key];
  24. // Emojis with skin tone modifiers are stored like this
  25. if (Object.prototype.hasOwnProperty.call(emoji, '1')) {
  26. emoji = emoji['1'];
  27. }
  28. shortcodeMap[emoji.native] = emoji.id;
  29. });
  30. const stripModifiers = unicode => {
  31. skinTones.forEach(tone => {
  32. unicode = unicode.replace(tone, '');
  33. });
  34. return unicode;
  35. };
  36. Object.keys(emojiMap).forEach(key => {
  37. if (excluded.includes(key)) {
  38. delete emojiMap[key];
  39. return;
  40. }
  41. const normalizedKey = stripModifiers(key);
  42. let shortcode = shortcodeMap[normalizedKey];
  43. if (!shortcode) {
  44. shortcode = shortcodeMap[normalizedKey + '\uFE0F'];
  45. }
  46. const filename = emojiMap[key];
  47. const filenameData = [key];
  48. if (unicodeToFilename(key) !== filename) {
  49. // filename can't be derived using unicodeToFilename
  50. filenameData.push(filename);
  51. }
  52. if (typeof shortcode === 'undefined') {
  53. emojisWithoutShortCodes.push(filenameData);
  54. } else {
  55. if (!Array.isArray(shortCodesToEmojiData[shortcode])) {
  56. shortCodesToEmojiData[shortcode] = [[]];
  57. }
  58. shortCodesToEmojiData[shortcode][0].push(filenameData);
  59. }
  60. });
  61. Object.keys(emojiIndex.emojis).forEach(key => {
  62. let emoji = emojiIndex.emojis[key];
  63. // Emojis with skin tone modifiers are stored like this
  64. if (Object.prototype.hasOwnProperty.call(emoji, '1')) {
  65. emoji = emoji['1'];
  66. }
  67. const { native } = emoji;
  68. let { short_names, search, unified } = emojiMartData.emojis[key];
  69. if (short_names[0] !== key) {
  70. throw new Error('The compresser expects the first short_code to be the ' +
  71. 'key. It may need to be rewritten if the emoji change such that this ' +
  72. 'is no longer the case.');
  73. }
  74. short_names = short_names.slice(1); // first short name can be inferred from the key
  75. const searchData = [native, short_names, search];
  76. if (unicodeToUnifiedName(native) !== unified) {
  77. // unified name can't be derived from unicodeToUnifiedName
  78. searchData.push(unified);
  79. }
  80. if (!Array.isArray(shortCodesToEmojiData[key])) {
  81. shortCodesToEmojiData[key] = [[]];
  82. }
  83. shortCodesToEmojiData[key].push(searchData);
  84. });
  85. // JSON.parse/stringify is to emulate what @preval is doing and avoid any
  86. // inconsistent behavior in dev mode
  87. module.exports = JSON.parse(JSON.stringify([
  88. shortCodesToEmojiData,
  89. emojiMartData.skins,
  90. emojiMartData.categories,
  91. emojiMartData.aliases,
  92. emojisWithoutShortCodes,
  93. ]));