From c540058e6a7a7a2d1996a35d2813b95007990277 Mon Sep 17 00:00:00 2001 From: Patrick Nelson Date: Sat, 18 Feb 2017 03:10:03 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8E=20Now=20able=20to=20search=20using?= =?UTF-8?q?=20multiple=20words=20separated=20by=20space=20(issue=20#89).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/search.js | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/app/search.js b/app/search.js index 211e16e..aee20ee 100644 --- a/app/search.js +++ b/app/search.js @@ -112,17 +112,38 @@ function search (query) { if (query.length === 0 || (query.length === 1 && query.charCodeAt() <= 255)) { results = emojikeys.slice(0) } else { - var resultsDict = {} - indexKeys.forEach(function matchQuery (keyword) { - if (stringIncludes(keyword, query)) { - index[keyword].forEach(function addMatchingEmoji (emoji) { - resultsDict[emoji] = true - }) + var resultsDict = null + let words = query.split(/\s+/) + for(let i in words) { + let word = words[i] + if (word === '') continue + + let wordResultsDict = {} + indexKeys.forEach(function matchQuery (keyword) { + if (stringIncludes(keyword, word)) { + index[keyword].forEach(function addMatchingEmoji (emoji) { + wordResultsDict[emoji] = true + }) + } + }) + + if (resultsDict === null) { + // Just initialize it. + resultsDict = wordResultsDict + } else { + // Intersect with existing results. + for(let emoji in resultsDict) { + if (!wordResultsDict.hasOwnProperty(emoji)) { + delete resultsDict[emoji] + } + } } - }) + } + results = Object.keys(resultsDict).sort(function sortResults (a, b) { return emojikeyIndexTable[a] - emojikeyIndexTable[b] }) + } // Put exact match first