The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

178 lines
4.3 KiB

  1. // This code is largely borrowed from:
  2. // https://github.com/missive/emoji-mart/blob/5f2ffcc/src/utils/emoji-index.js
  3. import data from './emoji_mart_data_light';
  4. import { getData, getSanitizedData, intersect } from './emoji_utils';
  5. let originalPool = {};
  6. let index = {};
  7. let emojisList = {};
  8. let emoticonsList = {};
  9. let customEmojisList = [];
  10. for (let emoji in data.emojis) {
  11. let emojiData = data.emojis[emoji];
  12. let { short_names, emoticons } = emojiData;
  13. let id = short_names[0];
  14. if (emoticons) {
  15. emoticons.forEach(emoticon => {
  16. if (emoticonsList[emoticon]) {
  17. return;
  18. }
  19. emoticonsList[emoticon] = id;
  20. });
  21. }
  22. emojisList[id] = getSanitizedData(id);
  23. originalPool[id] = emojiData;
  24. }
  25. function clearCustomEmojis(pool) {
  26. customEmojisList.forEach((emoji) => {
  27. let emojiId = emoji.id || emoji.short_names[0];
  28. delete pool[emojiId];
  29. delete emojisList[emojiId];
  30. });
  31. }
  32. function addCustomToPool(custom, pool) {
  33. if (customEmojisList.length) clearCustomEmojis(pool);
  34. custom.forEach((emoji) => {
  35. let emojiId = emoji.id || emoji.short_names[0];
  36. if (emojiId && !pool[emojiId]) {
  37. pool[emojiId] = getData(emoji);
  38. emojisList[emojiId] = getSanitizedData(emoji);
  39. }
  40. });
  41. customEmojisList = custom;
  42. index = {};
  43. }
  44. function search(value, { emojisToShowFilter, maxResults, include, exclude, custom } = {}) {
  45. if (custom !== undefined) {
  46. if (customEmojisList !== custom)
  47. addCustomToPool(custom, originalPool);
  48. } else {
  49. custom = [];
  50. }
  51. maxResults = maxResults || 75;
  52. include = include || [];
  53. exclude = exclude || [];
  54. let results = null,
  55. pool = originalPool;
  56. if (value.length) {
  57. if (value === '-' || value === '-1') {
  58. return [emojisList['-1']];
  59. }
  60. let values = value.toLowerCase().split(/[\s|,|\-|_]+/),
  61. allResults = [];
  62. if (values.length > 2) {
  63. values = [values[0], values[1]];
  64. }
  65. if (include.length || exclude.length) {
  66. pool = {};
  67. data.categories.forEach(category => {
  68. let isIncluded = include && include.length ? include.indexOf(category.name.toLowerCase()) > -1 : true;
  69. let isExcluded = exclude && exclude.length ? exclude.indexOf(category.name.toLowerCase()) > -1 : false;
  70. if (!isIncluded || isExcluded) {
  71. return;
  72. }
  73. category.emojis.forEach(emojiId => pool[emojiId] = data.emojis[emojiId]);
  74. });
  75. if (custom.length) {
  76. let customIsIncluded = include && include.length ? include.indexOf('custom') > -1 : true;
  77. let customIsExcluded = exclude && exclude.length ? exclude.indexOf('custom') > -1 : false;
  78. if (customIsIncluded && !customIsExcluded) {
  79. addCustomToPool(custom, pool);
  80. }
  81. }
  82. }
  83. allResults = values.map((value) => {
  84. let aPool = pool,
  85. aIndex = index,
  86. length = 0;
  87. for (let charIndex = 0; charIndex < value.length; charIndex++) {
  88. const char = value[charIndex];
  89. length++;
  90. aIndex[char] = aIndex[char] || {};
  91. aIndex = aIndex[char];
  92. if (!aIndex.results) {
  93. let scores = {};
  94. aIndex.results = [];
  95. aIndex.pool = {};
  96. for (let id in aPool) {
  97. let emoji = aPool[id],
  98. { search } = emoji,
  99. sub = value.substr(0, length),
  100. subIndex = search.indexOf(sub);
  101. if (subIndex !== -1) {
  102. let score = subIndex + 1;
  103. if (sub === id) score = 0;
  104. aIndex.results.push(emojisList[id]);
  105. aIndex.pool[id] = emoji;
  106. scores[id] = score;
  107. }
  108. }
  109. aIndex.results.sort((a, b) => {
  110. let aScore = scores[a.id],
  111. bScore = scores[b.id];
  112. return aScore - bScore;
  113. });
  114. }
  115. aPool = aIndex.pool;
  116. }
  117. return aIndex.results;
  118. }).filter(a => a);
  119. if (allResults.length > 1) {
  120. results = intersect.apply(null, allResults);
  121. } else if (allResults.length) {
  122. results = allResults[0];
  123. } else {
  124. results = [];
  125. }
  126. }
  127. if (results) {
  128. if (emojisToShowFilter) {
  129. results = results.filter((result) => emojisToShowFilter(data.emojis[result.id]));
  130. }
  131. if (results && results.length > maxResults) {
  132. results = results.slice(0, maxResults);
  133. }
  134. }
  135. return results;
  136. }
  137. export { search };