diff --git a/src/snek/static/chat-input.js b/src/snek/static/chat-input.js index 9b45b35..d91b51d 100644 --- a/src/snek/static/chat-input.js +++ b/src/snek/static/chat-input.js @@ -70,9 +70,14 @@ class ChatInputComponent extends HTMLElement { return mentions.map(mention => { let closestAuthor = null; let minDistance = Infinity; - + const lowerMention = mention.toLowerCase(); + authors.forEach(author => { - const distance = this.levenshteinDistance(mention.toLowerCase(), author.toLowerCase()); + const lowerAuthor = author.toLowerCase(); + const distance = this.levenshteinDistance(lowerMention, lowerAuthor); + if(!this.isSubSequence(lowerMention,lowerAuthor)) { + distance += 1 + } if (distance < minDistance) { minDistance = distance; closestAuthor = author; @@ -252,6 +257,25 @@ levenshteinDistance(a, b) { const millisecondsDifference = event2Time.getTime() - event1Time.getTime(); return millisecondsDifference / 1000; } + isSubsequence(input, array) { + for (const str of array) { + let i = 0; + let j = 0; + + while (i < input.length && j < str.length) { + if (input[i] === str[j]) { + i++; + } + j++; + } + + if (i === input.length) { + return true; + } + } + return false; + } + newMessage() { if (!this.messageUid) {