From da90cc3d1d85a3451623788b00c9f94f29fcbb01 Mon Sep 17 00:00:00 2001 From: Donald Little Date: Mon, 17 Jul 2017 11:22:05 -0700 Subject: [PATCH] Allow fuzzy searching in Quick Switch modal (#204) --- .../components/modals/request-switcher-modal.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/ui/components/modals/request-switcher-modal.js b/app/ui/components/modals/request-switcher-modal.js index fc2f3fa3a..5cbaecb86 100644 --- a/app/ui/components/modals/request-switcher-modal.js +++ b/app/ui/components/modals/request-switcher-modal.js @@ -109,13 +109,22 @@ class RequestSwitcherModal extends PureComponent { // OPTIMIZATION: This only filters if we have a filter let matchedRequests = workspaceChildren.filter(d => d.type === models.request.type); if (searchString) { + const specialCharacters = ['.', '^', '$', '*', '+', '-', '?', '(', ')', '[', ']', '{', '}', '\\', '|']; + + const regexSearchString = searchString + .toLowerCase() + .split('') + .map((c) => specialCharacters.includes(c) ? `\\${c}` : c) + .join('.*'); + + const toMatch = new RegExp(regexSearchString); + matchedRequests = matchedRequests.filter(r => { const name = r.name.toLowerCase(); const id = r._id.toLowerCase(); - const toMatch = searchString.toLowerCase(); - // Match substring of name - const matchesName = name.indexOf(toMatch) >= 0; + // Fuzzy match searchString to name + const matchesName = toMatch.test(name); // Match exact Id const matchesId = id === toMatch;