improved algorithm, possibly speed too

This commit is contained in:
Dym Sohin 2020-09-21 18:10:55 +02:00
parent f1e1f20a8d
commit cff202241b
2 changed files with 12 additions and 6 deletions

View file

@ -11,14 +11,17 @@ const filterByKeyword = (list, keyword = '') => {
if (keyword === '') return list if (keyword === '') return list
const keywordLowercase = keyword.toLowerCase() const keywordLowercase = keyword.toLowerCase()
const orderedEmojiList = [] let orderedEmojiList = []
for (const emoji of list) { for (const emoji of list) {
const indexOfKeyword = emoji.displayText.toLowerCase().indexOf(keywordLowercase) const indexOfKeyword = emoji.displayText.toLowerCase().indexOf(keywordLowercase)
if (indexOfKeyword > -1) { if (indexOfKeyword > -1) {
orderedEmojiList.splice(indexOfKeyword, 0, emoji) if (!Array.isArray(orderedEmojiList[keywordLowercase])) {
orderedEmojiList[keywordLowercase] = []
}
orderedEmojiList[keywordLowercase].push(emoji)
} }
} }
return orderedEmojiList return orderedEmojiList.flat()
} }
const EmojiPicker = { const EmojiPicker = {

View file

@ -29,14 +29,17 @@ const ReactButton = {
emojis () { emojis () {
if (this.filterWord !== '') { if (this.filterWord !== '') {
const filterWordLowercase = this.filterWord.toLowerCase() const filterWordLowercase = this.filterWord.toLowerCase()
const orderedEmojiList = [] let orderedEmojiList = []
for (const emoji of this.$store.state.instance.emoji) { for (const emoji of this.$store.state.instance.emoji) {
const indexOfFilterWord = emoji.displayText.toLowerCase().indexOf(filterWordLowercase) const indexOfFilterWord = emoji.displayText.toLowerCase().indexOf(filterWordLowercase)
if (indexOfFilterWord > -1) { if (indexOfFilterWord > -1) {
orderedEmojiList.splice(indexOfFilterWord, 0, emoji) if (!Array.isArray(orderedEmojiList[indexOfFilterWord])) {
orderedEmojiList[indexOfFilterWord] = []
}
orderedEmojiList[indexOfFilterWord].push(emoji)
} }
} }
return orderedEmojiList return orderedEmojiList.flat()
} }
return this.$store.state.instance.emoji || [] return this.$store.state.instance.emoji || []
}, },