Move search index logic out of search component

release
Etienne Lemay 2016-07-11 11:28:44 -04:00
parent 510d4b30fd
commit 5ebf4464ea
3 changed files with 56 additions and 55 deletions

View File

@ -1,64 +1,12 @@
import React from 'react'
import lunr from 'lunr'
import data from '../../data'
import {emojiIndex} from '../utils'
export default class Search extends React.Component {
constructor(props) {
super(props)
this.buildIndex()
}
buildIndex() {
this.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
this.index.add({
id: short_name,
emoticons: emoticons,
short_name: this.tokenize(short_name),
name: this.tokenize(name),
})
}
}
tokenize (string) {
if (['-', '-1', '+', '+1'].indexOf(string) == 0) {
return string.split('')
}
if (/(:|;|=)-/.test(string)) {
return [string]
}
return string.split(/[-|_|\s]+/)
}
handleChange() {
var { input } = this.refs,
value = input.value,
results = null
value = input.value
if (value.length) {
results = this.index.search(this.tokenize(value)).map((result) =>
result.ref
)
results = results.slice(0, this.props.maxResults)
}
this.props.onSearch(results)
this.props.onSearch(emojiIndex.search(value))
}
clear() {

52
src/utils/emoji-index.js Normal file
View File

@ -0,0 +1,52 @@
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
}
function tokenize (string) {
if (['-', '-1', '+', '+1'].indexOf(string) == 0) {
return string.split('')
}
if (/(:|;|=)-/.test(string)) {
return [string]
}
return string.split(/[-|_|\s]+/)
}
export default { search }

View File

@ -1,2 +1,3 @@
export {default as store} from './store'
export {default as emojiIndex} from './emoji-index'
export {default as frequently} from './frequently'