diff --git a/lib/genpack.js b/lib/genpack.js index 52bbc6e..895a186 100644 --- a/lib/genpack.js +++ b/lib/genpack.js @@ -7,6 +7,8 @@ const emojiData = require('unicode-emoji-json') const orderedEmoji = require('unicode-emoji-json/data-ordered-emoji') const emojiComponents = require('unicode-emoji-json/data-emoji-components') +const emojiNames = Object.values(emojiData).map(e => e.name) + const keywordsMap = {} const emmojiInfo = {} @@ -19,15 +21,15 @@ for (const emojiSymbol of emojiSymbols) { keywordsMap[keyword].push(emojiSymbol) }) emmojiInfo[emojiSymbol] = { - ...emojiData[emojiSymbol], - keywords: emojiKeywords[emojiSymbol] + ...emojiData[emojiSymbol] } } const packedEmoji = { - keywords: keywordsMap, + emojiNames: emojiNames, searchTerms: Object.keys(keywordsMap), emoji: emmojiInfo, + emojiKeywords, orderedEmoji, emojiComponents } diff --git a/src/search.js b/src/search.js index 82bc7db..529df1b 100644 --- a/src/search.js +++ b/src/search.js @@ -2,9 +2,8 @@ const emojiData = require('./emoji.pack.json') const { - keywords: emojiKeywords, emoji: emojiInfo, - searchTerms, + emojiKeywords, orderedEmoji, emojiComponents } = emojiData @@ -66,8 +65,9 @@ const alfredItem = (emojiDetails, emojiSymbol) => { const modifiedEmoji = addModifier(emojiDetails, emojiSymbol, modifier) const icon = getIconName(emojiDetails) const name = emojiDetails.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, @@ -101,14 +101,39 @@ const alfredItems = (chars) => { const all = () => alfredItems(orderedEmoji) const matches = (terms) => { - const result = [] - for (const term of terms) { - const foundTerms = searchTerms.filter(searchTerm => searchTerm.includes(term)) - foundTerms.forEach(foundTerm => { - Array.prototype.push.apply(result, emojiKeywords[foundTerm]) - }) + const emojiNameResults = new Set() + const emojiKeywordResults = new Set() + + for (const [emojiText, emoji] of Object.entries(emojiInfo)) { + const emojiName = emoji.name + + let hasNameMatch = false + let hasMatch = true + + for (const term of terms) { + if (emojiName.includes(term)) { + hasNameMatch = true + continue + } else if (!emojiKeywords[emojiText].some(keyword => keyword.includes(term))) { + hasMatch = false + // And operator on the terms. + break + } + } + + if (!hasMatch) { + continue + } + + if (hasNameMatch) { + emojiNameResults.add(emojiText) + } else { + emojiKeywordResults.add(emojiText) + } } - return new Set(result) + + // 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 eb8c7fb..920e276 100644 --- a/test/search.test.js +++ b/test/search.test.js @@ -65,11 +65,11 @@ 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 face') -}) +// test('enables uid', (t) => { +// t.plan(1) +// const found = search('grimacing') +// t.ok(found.items[0].uid === 'grimacing face') +// }) test('enables autocomplete', (t) => { t.plan(1)