From be1e6705c3bf18f254768eaf541134b882618d15 Mon Sep 17 00:00:00 2001 From: Etienne Lemay Date: Fri, 22 Jul 2016 14:23:16 -0400 Subject: [PATCH] Use custom search index instead of lunr Builds the index as the user search Supports substring search --- package.json | 1 - src/utils/emoji-index.js | 66 +++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 8d28aa9..c88c0ed 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "emoji-data": "git://github.com/iamcal/emoji-data.git#6daffc10d8e8fd06b80ec24c9bdcb65218f71563", "emojilib": "2.0.2", "inflection": "1.10.0", - "lunr": "0.7.1", "mkdirp": "0.5.1", "react": "15.2.0", "react-addons-test-utils": "15.2.0", diff --git a/src/utils/emoji-index.js b/src/utils/emoji-index.js index b2641d6..2f37e30 100644 --- a/src/utils/emoji-index.js +++ b/src/utils/emoji-index.js @@ -1,24 +1,14 @@ -import lunr from 'lunr' import data from '../../data' import {getSanitizedData} from '.' +var index = {} 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_names, name, emoticons } = emojiData, + { short_names, emoticons } = emojiData, id = short_names[0] for (let emoticon of emoticons) { @@ -28,39 +18,45 @@ for (let emoji in data.emojis) { } emojisList[id] = getSanitizedData(id) - - index.add({ - id: id, - emoticons: emoticons, - short_name: tokenize(id), - name: tokenize(name), - }) } function search(value, maxResults = 75) { var results = null if (value.length) { - results = index.search(tokenize(value)).map((result) => - emojisList[result.ref] - ) + var aPool = data.emojis, + aIndex = index, + i = 0 - results = results.slice(0, maxResults) + value = value.toLowerCase() + + for (let char of value.split('')) { + i++ + + aIndex[char] || (aIndex[char] = {}) + aIndex = aIndex[char] + + if (!aIndex.results) { + aIndex.results = [] + aIndex.pool = {} + + for (let id in aPool) { + let emoji = aPool[id], + { search } = emoji + + if (search.indexOf(value.substr(0, i)) != -1) { + aIndex.results.push(emojisList[id]) + aIndex.pool[id] = emoji + } + } + } + + aPool = aIndex.pool + results = aIndex.results.slice(0, maxResults) + } } return results } -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 }