From 5ebf4464ea17d2467fe10ba6bf2ad592946bc66e Mon Sep 17 00:00:00 2001 From: Etienne Lemay Date: Mon, 11 Jul 2016 11:28:44 -0400 Subject: [PATCH] Move search index logic out of search component --- src/components/search.js | 58 +++------------------------------------- src/utils/emoji-index.js | 52 +++++++++++++++++++++++++++++++++++ src/utils/index.js | 1 + 3 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 src/utils/emoji-index.js diff --git a/src/components/search.js b/src/components/search.js index 9c45819..e4f96e6 100644 --- a/src/components/search.js +++ b/src/components/search.js @@ -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() { diff --git a/src/utils/emoji-index.js b/src/utils/emoji-index.js new file mode 100644 index 0000000..bbf10bc --- /dev/null +++ b/src/utils/emoji-index.js @@ -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 } diff --git a/src/utils/index.js b/src/utils/index.js index 89fdb0c..4ee45a1 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,2 +1,3 @@ export {default as store} from './store' +export {default as emojiIndex} from './emoji-index' export {default as frequently} from './frequently'