2017-10-02 08:22:24 +03:00
|
|
|
// This code is largely borrowed from:
|
2017-10-07 13:17:02 +03:00
|
|
|
// https://github.com/missive/emoji-mart/blob/5f2ffcc/src/utils/emoji-index.js
|
2017-10-02 08:22:24 +03:00
|
|
|
|
2017-10-06 04:42:34 +03:00
|
|
|
import data from './emoji_mart_data_light';
|
2017-10-02 08:22:24 +03:00
|
|
|
import { getData, getSanitizedData, intersect } from './emoji_utils';
|
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
let originalPool = {};
|
2017-10-02 08:22:24 +03:00
|
|
|
let index = {};
|
|
|
|
let emojisList = {};
|
|
|
|
let emoticonsList = {};
|
2018-06-07 20:40:23 +03:00
|
|
|
let customEmojisList = [];
|
2017-10-02 08:22:24 +03:00
|
|
|
|
|
|
|
for (let emoji in data.emojis) {
|
2017-10-07 13:17:02 +03:00
|
|
|
let emojiData = data.emojis[emoji];
|
|
|
|
let { short_names, emoticons } = emojiData;
|
|
|
|
let id = short_names[0];
|
|
|
|
|
|
|
|
if (emoticons) {
|
|
|
|
emoticons.forEach(emoticon => {
|
|
|
|
if (emoticonsList[emoticon]) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-02 08:22:24 +03:00
|
|
|
|
|
|
|
emoticonsList[emoticon] = id;
|
2017-10-07 13:17:02 +03:00
|
|
|
});
|
2017-10-02 08:22:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
emojisList[id] = getSanitizedData(id);
|
2017-10-07 13:17:02 +03:00
|
|
|
originalPool[id] = emojiData;
|
|
|
|
}
|
|
|
|
|
2018-06-07 20:40:23 +03:00
|
|
|
function clearCustomEmojis(pool) {
|
|
|
|
customEmojisList.forEach((emoji) => {
|
|
|
|
let emojiId = emoji.id || emoji.short_names[0];
|
|
|
|
|
|
|
|
delete pool[emojiId];
|
|
|
|
delete emojisList[emojiId];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
function addCustomToPool(custom, pool) {
|
2018-06-07 20:40:23 +03:00
|
|
|
if (customEmojisList.length) clearCustomEmojis(pool);
|
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
custom.forEach((emoji) => {
|
|
|
|
let emojiId = emoji.id || emoji.short_names[0];
|
|
|
|
|
|
|
|
if (emojiId && !pool[emojiId]) {
|
|
|
|
pool[emojiId] = getData(emoji);
|
|
|
|
emojisList[emojiId] = getSanitizedData(emoji);
|
|
|
|
}
|
|
|
|
});
|
2018-06-07 20:40:23 +03:00
|
|
|
|
|
|
|
customEmojisList = custom;
|
|
|
|
index = {};
|
2017-10-02 08:22:24 +03:00
|
|
|
}
|
|
|
|
|
2018-06-08 14:28:04 +03:00
|
|
|
function search(value, { emojisToShowFilter, maxResults, include, exclude, custom } = {}) {
|
|
|
|
if (custom !== undefined) {
|
|
|
|
if (customEmojisList !== custom)
|
|
|
|
addCustomToPool(custom, originalPool);
|
|
|
|
} else {
|
|
|
|
custom = [];
|
|
|
|
}
|
2017-10-07 13:17:02 +03:00
|
|
|
|
2017-10-02 08:22:24 +03:00
|
|
|
maxResults = maxResults || 75;
|
|
|
|
include = include || [];
|
|
|
|
exclude = exclude || [];
|
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
let results = null,
|
|
|
|
pool = originalPool;
|
2017-10-02 08:22:24 +03:00
|
|
|
|
|
|
|
if (value.length) {
|
|
|
|
if (value === '-' || value === '-1') {
|
|
|
|
return [emojisList['-1']];
|
|
|
|
}
|
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
let values = value.toLowerCase().split(/[\s|,|\-|_]+/),
|
|
|
|
allResults = [];
|
2017-10-02 08:22:24 +03:00
|
|
|
|
|
|
|
if (values.length > 2) {
|
|
|
|
values = [values[0], values[1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (include.length || exclude.length) {
|
|
|
|
pool = {};
|
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
data.categories.forEach(category => {
|
2017-10-02 08:22:24 +03:00
|
|
|
let isIncluded = include && include.length ? include.indexOf(category.name.toLowerCase()) > -1 : true;
|
|
|
|
let isExcluded = exclude && exclude.length ? exclude.indexOf(category.name.toLowerCase()) > -1 : false;
|
|
|
|
if (!isIncluded || isExcluded) {
|
2017-10-07 13:17:02 +03:00
|
|
|
return;
|
2017-10-02 08:22:24 +03:00
|
|
|
}
|
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
category.emojis.forEach(emojiId => pool[emojiId] = data.emojis[emojiId]);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (custom.length) {
|
|
|
|
let customIsIncluded = include && include.length ? include.indexOf('custom') > -1 : true;
|
|
|
|
let customIsExcluded = exclude && exclude.length ? exclude.indexOf('custom') > -1 : false;
|
|
|
|
if (customIsIncluded && !customIsExcluded) {
|
|
|
|
addCustomToPool(custom, pool);
|
2017-10-02 08:22:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
allResults = values.map((value) => {
|
|
|
|
let aPool = pool,
|
|
|
|
aIndex = index,
|
|
|
|
length = 0;
|
2017-10-02 08:22:24 +03:00
|
|
|
|
2017-10-07 13:17:02 +03:00
|
|
|
for (let charIndex = 0; charIndex < value.length; charIndex++) {
|
|
|
|
const char = value[charIndex];
|
2017-10-02 08:22:24 +03:00
|
|
|
length++;
|
|
|
|
|
|
|
|
aIndex[char] = aIndex[char] || {};
|
|
|
|
aIndex = aIndex[char];
|
|
|
|
|
|
|
|
if (!aIndex.results) {
|
|
|
|
let scores = {};
|
|
|
|
|
|
|
|
aIndex.results = [];
|
|
|
|
aIndex.pool = {};
|
|
|
|
|
|
|
|
for (let id in aPool) {
|
|
|
|
let emoji = aPool[id],
|
|
|
|
{ search } = emoji,
|
|
|
|
sub = value.substr(0, length),
|
|
|
|
subIndex = search.indexOf(sub);
|
|
|
|
|
|
|
|
if (subIndex !== -1) {
|
|
|
|
let score = subIndex + 1;
|
2017-10-07 13:17:02 +03:00
|
|
|
if (sub === id) score = 0;
|
2017-10-02 08:22:24 +03:00
|
|
|
|
|
|
|
aIndex.results.push(emojisList[id]);
|
|
|
|
aIndex.pool[id] = emoji;
|
|
|
|
|
|
|
|
scores[id] = score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aIndex.results.sort((a, b) => {
|
|
|
|
let aScore = scores[a.id],
|
|
|
|
bScore = scores[b.id];
|
|
|
|
|
|
|
|
return aScore - bScore;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
aPool = aIndex.pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aIndex.results;
|
|
|
|
}).filter(a => a);
|
|
|
|
|
|
|
|
if (allResults.length > 1) {
|
2017-10-07 13:17:02 +03:00
|
|
|
results = intersect.apply(null, allResults);
|
2017-10-02 08:22:24 +03:00
|
|
|
} else if (allResults.length) {
|
|
|
|
results = allResults[0];
|
|
|
|
} else {
|
|
|
|
results = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (results) {
|
|
|
|
if (emojisToShowFilter) {
|
2018-06-07 20:40:23 +03:00
|
|
|
results = results.filter((result) => emojisToShowFilter(data.emojis[result.id]));
|
2017-10-02 08:22:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (results && results.length > maxResults) {
|
|
|
|
results = results.slice(0, maxResults);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { search };
|