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