emoji-mart-lazyload/src/utils/emoji-index.js

181 lines
4.2 KiB
JavaScript
Raw Normal View History

import data from '../data'
2017-10-06 21:23:04 +00:00
import { getData, getSanitizedData, intersect } from '.'
2016-07-19 16:27:24 +00:00
var originalPool = {}
var index = {}
var emojisList = {}
var emoticonsList = {}
var customEmojisList = []
for (let emoji in data.emojis) {
let emojiData = data.emojis[emoji],
2017-10-07 04:02:02 +00:00
{ short_names, emoticons } = emojiData,
id = short_names[0]
2017-09-17 08:54:22 +00:00
if (emoticons) {
2018-03-27 18:51:26 +00:00
emoticons.forEach((emoticon) => {
2017-09-17 08:54:22 +00:00
if (emoticonsList[emoticon]) {
return
}
2016-07-22 16:23:47 +00:00
emoticonsList[emoticon] = id
2017-09-17 08:54:22 +00:00
})
}
2016-07-22 16:23:47 +00:00
emojisList[id] = getSanitizedData(id)
originalPool[id] = emojiData
}
function clearCustomEmojis(pool) {
2018-03-27 18:51:26 +00:00
customEmojisList.forEach((emoji) => {
let emojiId = emoji.id || emoji.short_names[0]
delete pool[emojiId]
delete emojisList[emojiId]
})
}
function addCustomToPool(custom, pool) {
if (customEmojisList.length) clearCustomEmojis(pool)
2018-03-27 18:51:26 +00:00
custom.forEach((emoji) => {
let emojiId = emoji.id || emoji.short_names[0]
if (emojiId && !pool[emojiId]) {
pool[emojiId] = getData(emoji)
emojisList[emojiId] = getSanitizedData(emoji)
}
})
customEmojisList = custom
index = {}
}
2017-10-07 04:02:02 +00:00
function search(
value,
2018-03-27 18:51:26 +00:00
{ emojisToShowFilter, maxResults, include, exclude, custom = [] } = {},
2017-10-07 04:02:02 +00:00
) {
if (customEmojisList != custom) addCustomToPool(custom, originalPool)
maxResults || (maxResults = 75)
include || (include = [])
exclude || (exclude = [])
var results = null,
2017-10-07 04:02:02 +00:00
pool = originalPool
if (value.length) {
if (value == '-' || value == '-1') {
return [emojisList['-1']]
}
var values = value.toLowerCase().split(/[\s|,|\-|_]+/),
2017-10-07 04:02:02 +00:00
allResults = []
if (values.length > 2) {
values = [values[0], values[1]]
}
if (include.length || exclude.length) {
pool = {}
2018-03-27 18:51:26 +00:00
data.categories.forEach((category) => {
2017-10-07 04:02:02 +00:00
let isIncluded =
2017-12-15 20:31:49 +00:00
include && include.length ? include.indexOf(category.id) > -1 : true
2017-10-07 04:02:02 +00:00
let isExcluded =
2017-12-15 20:31:49 +00:00
exclude && exclude.length ? exclude.indexOf(category.id) > -1 : false
2017-09-17 08:54:22 +00:00
if (!isIncluded || isExcluded) {
return
}
2017-09-17 08:54:22 +00:00
2017-10-07 04:02:02 +00:00
category.emojis.forEach(
2018-03-27 18:51:26 +00:00
(emojiId) => (pool[emojiId] = data.emojis[emojiId]),
2017-10-07 04:02:02 +00:00
)
2017-09-17 08:54:22 +00:00
})
if (custom.length) {
2017-10-07 04:02:02 +00:00
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-07 04:02:02 +00:00
allResults = values
2018-03-27 18:51:26 +00:00
.map((value) => {
2017-10-07 04:02:02 +00:00
var aPool = pool,
aIndex = index,
length = 0
2017-10-07 04:02:02 +00:00
for (let charIndex = 0; charIndex < value.length; charIndex++) {
const char = value[charIndex]
length++
2017-10-07 04:02:02 +00:00
aIndex[char] || (aIndex[char] = {})
aIndex = aIndex[char]
2017-10-07 04:02:02 +00:00
if (!aIndex.results) {
let scores = {}
2017-10-07 04:02:02 +00:00
aIndex.results = []
aIndex.pool = {}
2017-10-07 04:02:02 +00:00
for (let id in aPool) {
let emoji = aPool[id],
{ search } = emoji,
sub = value.substr(0, length),
subIndex = search.indexOf(sub)
2017-10-07 04:02:02 +00:00
if (subIndex != -1) {
let score = subIndex + 1
if (sub == id) score = 0
2017-10-07 04:02:02 +00:00
aIndex.results.push(emojisList[id])
aIndex.pool[id] = emoji
2017-10-07 04:02:02 +00:00
scores[id] = score
}
}
2017-10-07 04:02:02 +00:00
aIndex.results.sort((a, b) => {
var aScore = scores[a.id],
bScore = scores[b.id]
2017-10-07 04:02:02 +00:00
return aScore - bScore
})
}
2017-10-07 04:02:02 +00:00
aPool = aIndex.pool
}
2017-10-07 04:02:02 +00:00
return aIndex.results
})
2018-03-27 18:51:26 +00:00
.filter((a) => a)
if (allResults.length > 1) {
2017-10-06 21:23:04 +00:00
results = intersect.apply(null, allResults)
} else if (allResults.length) {
results = allResults[0]
} else {
results = []
}
}
if (results) {
if (emojisToShowFilter) {
2018-03-27 18:51:26 +00:00
results = results.filter((result) => emojisToShowFilter(pool[result.id]))
}
if (results && results.length > maxResults) {
results = results.slice(0, maxResults)
2017-04-04 16:00:32 +00:00
}
}
return results
}
export default { search, emojis: emojisList, emoticons: emoticonsList }