Allow fuzzy searching in Quick Switch modal (#204)

This commit is contained in:
Donald Little 2017-07-17 11:22:05 -07:00 committed by Gregory Schier
parent 2c5ce7999e
commit da90cc3d1d

View File

@ -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;