From f7fd6e7d05bf88169bd944455d0b45432de0f45f Mon Sep 17 00:00:00 2001 From: Etienne Lemay Date: Tue, 26 Jul 2016 15:31:27 -0400 Subject: [PATCH] Sort search results by score [Close #1] --- src/utils/emoji-index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/utils/emoji-index.js b/src/utils/emoji-index.js index f3697c6..107e96e 100644 --- a/src/utils/emoji-index.js +++ b/src/utils/emoji-index.js @@ -47,18 +47,34 @@ function search(value, maxResults = 75) { aIndex = aIndex[char] if (!aIndex.results) { + let scores = {} + aIndex.results = [] aIndex.pool = {} for (let id in aPool) { let emoji = aPool[id], - { search } = emoji + { search } = emoji, + sub = value.substr(0, length), + subIndex = search.indexOf(sub) + + if (subIndex != -1) { + let score = subIndex + 1 + if (sub == id) score = 0 - if (search.indexOf(value.substr(0, length)) != -1) { aIndex.results.push(emojisList[id]) aIndex.pool[id] = emoji + + scores[id] = score } } + + aIndex.results.sort((a, b) => { + var aScore = scores[a.id], + bScore = scores[b.id] + + return aScore - bScore + }) } aPool = aIndex.pool @@ -76,7 +92,7 @@ function search(value, maxResults = 75) { } } - if (results) { + if (results && results.length) { results = results.slice(0, maxResults) }