Modified user search to include whole word search

This commit is contained in:
Wyatt Johnson
2017-10-11 16:41:09 -06:00
parent 1fffa1427d
commit 86f48cea96
5 changed files with 66 additions and 12 deletions
+1 -1
View File
@@ -116,7 +116,7 @@ module.exports = class AssetsService {
static search({value, limit, open, sortOrder, cursor} = {}) {
let assets = AssetModel.find({});
if (value) {
if (value && value.length > 0) {
assets.merge({
$text: {
$search: value
+10
View File
@@ -0,0 +1,10 @@
/**
* Escape string for special regular expression characters.
*/
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
module.exports = {
escapeRegExp,
};
+10 -4
View File
@@ -24,6 +24,7 @@ const ActionsService = require('./actions');
const MailerService = require('./mailer');
const Wordlist = require('./wordlist');
const Domainlist = require('./domainlist');
const {escapeRegExp} = require('./regex');
const EMAIL_CONFIRM_JWT_SUBJECT = 'email_confirm';
const PASSWORD_RESET_JWT_SUBJECT = 'password_reset';
@@ -630,14 +631,19 @@ module.exports = class UsersService {
* @return {Promise}
*/
static search(value) {
if (!value || typeof value !== 'string' || value.length === 0) {
return UserModel.find({});
}
value = escapeRegExp(value);
return UserModel.find({
$or: [
// Search by a prefix match on the username.
{
'username': {
$regex: new RegExp(`^${value}`),
$options: 'i'
'lowercaseUsername': {
$regex: new RegExp(value.toLowerCase())
}
},
@@ -646,7 +652,7 @@ module.exports = class UsersService {
'profiles': {
$elemMatch: {
id: {
$regex: new RegExp(`^${value}`),
$regex: new RegExp(value),
$options: 'i'
},
provider: 'local'
+1 -7
View File
@@ -3,13 +3,7 @@ const _ = require('lodash');
const SettingsService = require('./settings');
const Errors = require('../errors');
const memoize = require('lodash/memoize');
/**
* Escape string for special regular expression characters.
*/
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
const {escapeRegExp} = require('./regex');
/**
* Generate a regulare expression that catches the `phrases`.