2016-07-11 15:28:44 +00:00
|
|
|
import lunr from 'lunr'
|
|
|
|
import data from '../../data'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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) =>
|
|
|
|
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] == '+') {
|
2016-07-11 15:28:44 +00:00
|
|
|
return string.split('')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/(:|;|=)-/.test(string)) {
|
|
|
|
return [string]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.split(/[-|_|\s]+/)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default { search }
|