Skip to content

Commit

Permalink
Prevent duplicates in emoji suggestions
Browse files Browse the repository at this point in the history
Use `Set()` instead of `Array.concat()` to merge results.
  • Loading branch information
mejo- committed Nov 24, 2021
1 parent b5feddf commit b736773
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/extensions/Emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,19 @@ export default class Emoji extends Extension {
// First search for exact matches
let results = searchItems.filter(item => item.short_names
.filter(name => name.toLowerCase() === query.toLowerCase()).length > 0)
console.debug('results', results)

// TODO: prevent duplicated results
// Second search for matches that start with query string
results = results.concat(searchItems.filter(item => item.short_names
.filter(name => name.toLowerCase().startsWith(query.toLowerCase())).length > 0))
results = [...new Set([...results, ...searchItems.filter(item => item.short_names
.filter(name => name.toLowerCase().startsWith(query.toLowerCase())).length > 0)])]
console.debug('results', results)

// If we still don't have enough, search for general matches
if (results.length < 5) {
results = results.concat(searchItems.filter(item => item.short_names
.filter(name => name.toLowerCase().includes(query.toLowerCase())).length > 0))
results = [...new Set([...results, ...searchItems.filter(item => item.short_names
.filter(name => name.toLowerCase().includes(query.toLowerCase())).length > 0)])]
}
console.debug('results', results)

return results.slice(0, 5)
},
Expand Down

0 comments on commit b736773

Please sign in to comment.