From 5721f5f6949a8a72339de0b26be5a9cccc553fc2 Mon Sep 17 00:00:00 2001 From: Etienne Lemay Date: Fri, 22 Jul 2016 16:05:14 -0400 Subject: [PATCH] Support searching for multiple (2) terms --- src/utils/emoji-index.js | 60 ++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/utils/emoji-index.js b/src/utils/emoji-index.js index 2f37e3002..2c78264fd 100644 --- a/src/utils/emoji-index.js +++ b/src/utils/emoji-index.js @@ -1,6 +1,6 @@ import data from '../../data' -import {getSanitizedData} from '.' +import {getSanitizedData, intersect} from '.' var index = {} var emojisList = {} @@ -24,38 +24,58 @@ function search(value, maxResults = 75) { var results = null if (value.length) { - var aPool = data.emojis, - aIndex = index, - i = 0 + var values = value.toLowerCase().split(/[\s|,]+/), + allResults = [] - value = value.toLowerCase() + if (values.length > 2) { + values = [values[0], values[1]] + } + + allResults = values.map((value) => { + var aPool = data.emojis, + aIndex = index, + length = 0 - for (let char of value.split('')) { - i++ + for (let char of value.split('')) { + length++ - aIndex[char] || (aIndex[char] = {}) - aIndex = aIndex[char] + aIndex[char] || (aIndex[char] = {}) + aIndex = aIndex[char] - if (!aIndex.results) { - aIndex.results = [] - aIndex.pool = {} + if (!aIndex.results) { + aIndex.results = [] + aIndex.pool = {} - for (let id in aPool) { - let emoji = aPool[id], - { search } = emoji + 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 + if (search.indexOf(value.substr(0, length)) != -1) { + aIndex.results.push(emojisList[id]) + aIndex.pool[id] = emoji + } } } + + aPool = aIndex.pool } - aPool = aIndex.pool - results = aIndex.results.slice(0, maxResults) + return aIndex.results + }).filter(a => a) + + if (allResults.length > 1) { + results = intersect(...allResults) + } else if (allResults.length) { + results = allResults[0] + } else { + results = [] } } + if (results) { + results = results.slice(0, maxResults) + } + return results }