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

66 lines
1.2 KiB
JavaScript
Raw Normal View History

import lunr from 'lunr'
import data from '../../data'
2016-07-19 16:27:24 +00:00
import {getSanitizedData} from '.'
var emojisList = {}
var emoticonsList = {}
var index = lunr(function() {
this.pipeline.reset()
this.field('short_name', { boost: 2 })
this.field('emoticons')
this.field('name')
this.ref('id')
})
for (let emoji in data.emojis) {
let emojiData = data.emojis[emoji],
{ short_name, name, emoticons } = emojiData
for (let emoticon of emoticons) {
if (!emoticonsList[emoticon]) {
emoticonsList[emoticon] = short_name
}
}
emojisList[short_name] = getSanitizedData(short_name)
index.add({
id: short_name,
emoticons: emoticons,
short_name: tokenize(short_name),
name: tokenize(name),
})
}
function search(value, maxResults = 75) {
var results = null
if (value.length) {
results = index.search(tokenize(value)).map((result) =>
emojisList[result.ref]
)
results = results.slice(0, maxResults)
}
return results
}
2016-07-18 18:23:28 +00:00
function tokenize (string = '') {
if (string[0] == '-' || string[0] == '+') {
return string.split('')
}
if (/(:|;|=)-/.test(string)) {
return [string]
}
return string.split(/[-|_|\s]+/)
}
export default { search, emojis: emojisList, emoticons: emoticonsList }