From 1c8aa7a88f25a361938cc00e8bef8ebcbe198607 Mon Sep 17 00:00:00 2001 From: Yash Srivastav Date: Tue, 1 Aug 2017 00:59:52 +0530 Subject: [PATCH 1/3] 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", () => { From fa021da7cfb108ee118757bb59f95d20c620bac4 Mon Sep 17 00:00:00 2001 From: Yash Srivastav Date: Thu, 10 Aug 2017 10:02:43 +0530 Subject: [PATCH 2/3] Add fuzzy searching for autocompleting colors --- client/js/lounge.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index ebbbe7c6..002f7dd7 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -120,8 +120,18 @@ $(function() { match: /\x03(\d{0,2}|[A-Za-z ]{0,10})$/, search(term, callback) { term = term.toLowerCase(); + const matchingColorCodes = constants.colorCodeMap - .filter((i) => i[0].startsWith(term) || i[1].toLowerCase().startsWith(term)); + .filter((i) => fuzzy.test(term, i[0]) || fuzzy.test(term, i[1])) + .map((i) => { + if (fuzzy.test(term, i[1])) { + return [i[0], fuzzy.match(term, i[1], { + pre: "", + post: "" + }).rendered]; + } + return i; + }); callback(matchingColorCodes); }, @@ -140,7 +150,16 @@ $(function() { search(term, callback, match) { term = term.toLowerCase(); const matchingColorCodes = constants.colorCodeMap - .filter((i) => i[0].startsWith(term) || i[1].toLowerCase().startsWith(term)) + .filter((i) => fuzzy.test(term, i[0]) || fuzzy.test(term, i[1])) + .map((pair) => { + if (fuzzy.test(term, pair[1])) { + return [pair[0], fuzzy.match(term, pair[1], { + pre: "", + post: "" + }).rendered]; + } + return pair; + }) .map((pair) => pair.concat(match[1])); // Needed to pass fg color to `template`... callback(matchingColorCodes); From 0e332ec19d6d68c6b4aef58eb3753aa711a89e3f Mon Sep 17 00:00:00 2001 From: Yash Srivastav Date: Sat, 19 Aug 2017 21:07:22 +0530 Subject: [PATCH 3/3] Merge redundant functions into one --- client/js/lounge.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 002f7dd7..9fcf8cb1 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -71,9 +71,10 @@ $(function() { search(term, callback) { term = term.slice(1); if (term[0] === "@") { - callback(completeNicks(term.slice(1)).map((val) => "@" + val)); + callback(completeNicks(term.slice(1), true) + .map((val) => ["@" + val[0], "@" + val[1]])); } else { - callback(completeNicks(term)); + callback(completeNicks(term, true)); } }, template([string, ]) { @@ -290,7 +291,7 @@ $(function() { chat.find(".chan.active .chat").trigger("msg.sticky"); // fix growing }) - .tab(tabCompleteNicks, {hint: false}) + .tab((word) => completeNicks(word, false), {hint: false}) .on("autocomplete:on", function() { enableAutocomplete(); }); @@ -943,8 +944,9 @@ $(function() { return results.map((el) => [el.string, el.original]); } - function tabCompleteNicks(word) { + function completeNicks(word, isFuzzy) { const users = chat.find(".active .users"); + word = word.toLowerCase(); // Lobbies and private chats do not have an user list if (!users.length) { @@ -952,26 +954,15 @@ $(function() { } const words = users.data("nicks"); - + if (isFuzzy) { + return fuzzyGrep(word, words); + } return $.grep( words, - (w) => !w.toLowerCase().indexOf(word.toLowerCase()) + (w) => !w.toLowerCase().indexOf(word) ); } - 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();