From 1c8aa7a88f25a361938cc00e8bef8ebcbe198607 Mon Sep 17 00:00:00 2001 From: Yash Srivastav Date: Tue, 1 Aug 2017 00:59:52 +0530 Subject: [PATCH] Extend fuzzy search in autocomplete to all strategies Fixes #1086 Just building upon the work already done in #1334 --- client/js/lounge.js | 78 ++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 25e67f4b..ebbbe7c6 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -51,17 +51,10 @@ $(function() { id: "emoji", match: /\B:([-+\w:?]{2,}):?$/, search(term, callback) { - const results = fuzzy.filter( - // Trim colon from the matched term, - // as we are unable to get a clean string from match regex - term.replace(/:$/, ""), - emojiSearchTerms, - { - pre: "", - post: "" - } - ); - callback(results.map((el) => [el.string, el.original])); + // Trim colon from the matched term, + // as we are unable to get a clean string from match regex + term = term.replace(/:$/, ""), + callback(fuzzyGrep(term, emojiSearchTerms)); }, template([string, original]) { return `${emojiMap[original]} ${string}`; @@ -83,11 +76,11 @@ $(function() { callback(completeNicks(term)); } }, - template(value) { - return value; + template([string, ]) { + return string; }, - replace(value) { - return value; + replace([, original]) { + return original; }, index: 1 }; @@ -98,11 +91,11 @@ $(function() { search(term, callback, match) { callback(completeChans(match[0])); }, - template(value) { - return value; + template([string,]) { + return string; }, - replace(value) { - return value; + replace([, original]) { + return original; }, index: 1 }; @@ -113,11 +106,11 @@ $(function() { search(term, callback) { callback(completeCommands("/" + term)); }, - template(value) { - return value; + template([string, ]) { + return string; }, - replace(value) { - return value; + replace([, original]) { + return original; }, index: 1 }; @@ -278,7 +271,7 @@ $(function() { chat.find(".chan.active .chat").trigger("msg.sticky"); // fix growing }) - .tab(completeNicks, {hint: false}) + .tab(tabCompleteNicks, {hint: false}) .on("autocomplete:on", function() { enableAutocomplete(); }); @@ -919,7 +912,19 @@ $(function() { } }()); - function completeNicks(word) { + function fuzzyGrep(term, array) { + const results = fuzzy.filter( + term, + array, + { + pre: "", + post: "" + } + ); + return results.map((el) => [el.string, el.original]); + } + + function tabCompleteNicks(word) { const users = chat.find(".active .users"); // Lobbies and private chats do not have an user list @@ -935,13 +940,23 @@ $(function() { ); } + function completeNicks(word) { + const users = chat.find(".active .users"); + + // Lobbies and private chats do not have an user list + if (!users.length) { + return []; + } + + const words = users.data("nicks"); + + return fuzzyGrep(word, words); + } + function completeCommands(word) { const words = constants.commands.slice(); - return $.grep( - words, - (w) => !w.toLowerCase().indexOf(word.toLowerCase()) - ); + return fuzzyGrep(word, words); } function completeChans(word) { @@ -955,10 +970,7 @@ $(function() { } }); - return $.grep( - words, - (w) => !w.toLowerCase().indexOf(word.toLowerCase()) - ); + return fuzzyGrep(word, words); } $(document).on("visibilitychange focus click", () => {