From 1468cf5cdc2f9bb0f4eb034edd8c76b26922a18c Mon Sep 17 00:00:00 2001 From: devnoname120 Date: Thu, 27 Jan 2022 19:45:19 +0100 Subject: [PATCH 1/2] Show emojis that match on name on top --- src/search.js | 38 ++++++++++++++++++++++++++++++++------ test/search.test.js | 6 ------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/search.js b/src/search.js index d451d1d..4cdab17 100644 --- a/src/search.js +++ b/src/search.js @@ -45,8 +45,9 @@ const getIconName = (emoji, name) => { const alfredItem = (emoji, name) => { const modifiedEmoji = addModifier(emoji, modifier) const icon = getIconName(emoji, name) + + // No `uid` property otherwise Alfred ignores the ordering of the list and uses its own return { - uid: name, title: name, subtitle: `${verb} "${modifiedEmoji}" (${name}) ${preposition}`, arg: modifiedEmoji, @@ -84,11 +85,36 @@ const libHasEmoji = (name, term) => { emojilib.lib[name].keywords.some((keyword) => keyword.includes(term)) } const matches = (terms) => { - return emojiNames.filter((name) => { - return terms.every((term) => { - return name.includes(term) || libHasEmoji(name, term) - }) - }) + const emojiNameResults = []; + const emojiKeywordResults = []; + + for (const emojiName of emojiNames) { + let hasNameMatch = false; + let hasMatch = true; + + for (const term of terms) { + if (emojiName.includes(term)) { + hasNameMatch = true; + continue; + } else if (!libHasEmoji(emojiName, term)) { + hasMatch = false; + break; + } + } + + if (!hasMatch) { + continue; + } + + if (hasNameMatch) { + emojiNameResults.push(emojiName); + } else { + emojiKeywordResults.push(emojiName); + } + } + + // Prioritize emojis that were matched on the name rather than on keywords + return [...emojiNameResults, ...emojiKeywordResults] } // :thumbs up: => ['thumbs', 'up'] diff --git a/test/search.test.js b/test/search.test.js index 1b9a573..41dc72b 100644 --- a/test/search.test.js +++ b/test/search.test.js @@ -72,12 +72,6 @@ test('applies modifier if possible', (t) => { t.ok(Object.keys(found.items).length > 0) }) -test('enables uid', (t) => { - t.plan(1) - const found = search('grimacing') - t.ok(found.items[0].uid === 'grimacing') -}) - test('enables autocomplete', (t) => { t.plan(1) const found = search('think') From 713250ef59d98282616203e752d75485cfb20381 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 2 Apr 2022 23:38:23 +0200 Subject: [PATCH 2/2] Add two missing files --- lib/genpack.js | 1 + src/search.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/genpack.js b/lib/genpack.js index 42b6303..895a186 100644 --- a/lib/genpack.js +++ b/lib/genpack.js @@ -29,6 +29,7 @@ const packedEmoji = { emojiNames: emojiNames, searchTerms: Object.keys(keywordsMap), emoji: emmojiInfo, + emojiKeywords, orderedEmoji, emojiComponents } diff --git a/src/search.js b/src/search.js index 89ee552..529df1b 100644 --- a/src/search.js +++ b/src/search.js @@ -3,7 +3,7 @@ const emojiData = require('./emoji.pack.json') const { emoji: emojiInfo, - searchTerms, + emojiKeywords, orderedEmoji, emojiComponents } = emojiData @@ -114,7 +114,7 @@ const matches = (terms) => { if (emojiName.includes(term)) { hasNameMatch = true continue - } else if (!searchTerms.some(searchTerm => searchTerm.includes(term))) { + } else if (!emojiKeywords[emojiText].some(keyword => keyword.includes(term))) { hasMatch = false // And operator on the terms. break