1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: user search by text instead of regex (#924)

* fix: user search by text instead of regex

* refactor: better clarity

Co-authored-by: Fredrik Strand Oseberg <fredrik.no@gmail.com>
This commit is contained in:
Nuno Góis 2022-04-29 12:14:17 +01:00 committed by GitHub
parent a255ccb458
commit 860656a95e

View File

@ -43,20 +43,14 @@ const filterUsersByQuery = (users: IUser[], filter: IUsersFilter): IUser[] => {
return users;
}
// Try to parse the search query as a RegExp.
// Return all users if it can't be parsed.
try {
const regExp = new RegExp(filter.query, 'i');
return users.filter(user => filterUserByRegExp(user, regExp));
} catch (err) {
if (err instanceof SyntaxError) {
return users;
} else {
throw err;
}
}
return users.filter(user => {
return filterUserByText(user, filter.query);
});
};
const filterUserByRegExp = (user: IUser, regExp: RegExp): boolean =>
regExp.test(user.name ?? '') ||
regExp.test(user.username ?? user.email ?? '');
const filterUserByText = (user: IUser, search: string = ''): boolean => {
const fieldsToSearch = [user.name ?? '', user.username ?? user.email ?? ''];
return fieldsToSearch.some(field =>
field.toLowerCase().includes(search.toLowerCase())
);
};