Allow emoji suggestions based on a match anywhere in the emoji name, but improve sorting

This commit is contained in:
xenofem 2020-02-09 17:25:24 -05:00 committed by Shpuld Shpuldson
parent eae0bce320
commit aef03d53b2
1 changed files with 10 additions and 6 deletions

View File

@ -29,17 +29,21 @@ export default data => input => {
export const suggestEmoji = emojis => input => {
const noPrefix = input.toLowerCase().substr(1)
return emojis
.filter(({ displayText }) => displayText.toLowerCase().startsWith(noPrefix))
.filter(({ displayText }) => displayText.toLowerCase().match(noPrefix))
.sort((a, b) => {
let aScore = 0
let bScore = 0
// Make custom emojis a priority
aScore += a.imageUrl ? 10 : 0
bScore += b.imageUrl ? 10 : 0
// Prioritize emoji that start with the input string
aScore += a.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0
bScore += b.displayText.toLowerCase().startsWith(noPrefix) ? 10 : 0
// Sort alphabetically
const alphabetically = a.displayText > b.displayText ? 1 : -1
// Sort by length
aScore -= a.displayText.length
bScore -= b.displayText.length
// Break ties alphabetically
const alphabetically = a.displayText > b.displayText ? 0.5 : -0.5
return bScore - aScore + alphabetically
})