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.

100 lines
3.0 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 skins = ['🏻', '🏼', '🏽', '🏾', '🏿'];
  19. const shortcodeMap = {};
  20. const shortCodesToEmojiData = {};
  21. const emojisWithoutShortCodes = [];
  22. Object.keys(emojiIndex.emojis).forEach(key => {
  23. shortcodeMap[emojiIndex.emojis[key].native] = emojiIndex.emojis[key].id;
  24. });
  25. const stripModifiers = unicode => {
  26. skins.forEach(tone => {
  27. unicode = unicode.replace(tone, '');
  28. });
  29. return unicode;
  30. };
  31. Object.keys(emojiMap).forEach(key => {
  32. if (excluded.includes(key)) {
  33. delete emojiMap[key];
  34. return;
  35. }
  36. const normalizedKey = stripModifiers(key);
  37. let shortcode = shortcodeMap[normalizedKey];
  38. if (!shortcode) {
  39. shortcode = shortcodeMap[normalizedKey + '\uFE0F'];
  40. }
  41. const filename = emojiMap[key];
  42. const filenameData = [key];
  43. if (unicodeToFilename(key) !== filename) {
  44. // filename can't be derived using unicodeToFilename
  45. filenameData.push(filename);
  46. }
  47. if (typeof shortcode === 'undefined') {
  48. emojisWithoutShortCodes.push(filenameData);
  49. } else {
  50. if (!Array.isArray(shortCodesToEmojiData[shortcode])) {
  51. shortCodesToEmojiData[shortcode] = [[]];
  52. }
  53. shortCodesToEmojiData[shortcode][0].push(filenameData);
  54. }
  55. });
  56. Object.keys(emojiIndex.emojis).forEach(key => {
  57. const { native } = emojiIndex.emojis[key];
  58. let { short_names, search, unified } = emojiMartData.emojis[key];
  59. if (short_names[0] !== key) {
  60. throw new Error('The compresser expects the first short_code to be the ' +
  61. 'key. It may need to be rewritten if the emoji change such that this ' +
  62. 'is no longer the case.');
  63. }
  64. short_names = short_names.slice(1); // first short name can be inferred from the key
  65. const searchData = [native, short_names, search];
  66. if (unicodeToUnifiedName(native) !== unified) {
  67. // unified name can't be derived from unicodeToUnifiedName
  68. searchData.push(unified);
  69. }
  70. shortCodesToEmojiData[key].push(searchData);
  71. });
  72. // JSON.parse/stringify is to emulate what @preval is doing and avoid any
  73. // inconsistent behavior in dev mode
  74. module.exports = JSON.parse(JSON.stringify([
  75. shortCodesToEmojiData,
  76. emojiMartData.skins,
  77. emojiMartData.categories,
  78. emojiMartData.aliases,
  79. emojisWithoutShortCodes,
  80. ]));