From fa021da7cfb108ee118757bb59f95d20c620bac4 Mon Sep 17 00:00:00 2001 From: Yash Srivastav Date: Thu, 10 Aug 2017 10:02:43 +0530 Subject: [PATCH] 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);