<commit_before>'use strict'
const winston = require( 'winston' )
const chalk = require( 'chalk' )

const User = require( '../../../../models/User' )

module.exports = function( req, res ) {
  if ( !req.body.email ) {
    res.status( 400 )
    return res.send( { success: false, message: "Malformed request." } )
  }
  if ( req.body.email.length < 2 ) {
    res.status( 400 )
    return res.send( { success: false, message: "Please provide more than two letters." } )
  }
  User.find( { email: { "$regex": req.body.email, "$options": "i" } }, '_id name surname company' ).limit( 5 )<commit_msg>Only search for users by substrings of name, surname, and company, see issue #92<commit_after>'use strict'
const winston = require( 'winston' )
const chalk = require( 'chalk' )

const User = require( '../../../../models/User' )

// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
const escapeRegExp = ( string ) => string.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ) // $& means the whole matched string

module.exports = function( req, res ) {
  let conditions = {}
  if ( req.body.name ) conditions.name = { '$regex': escapeRegExp( req.body.name ), '$options': 'i' }
  if ( req.body.surname ) conditions.surname = { '$regex': escapeRegExp( req.body.surname ), '$options': 'i' }
  if ( req.body.company ) conditions.company = { '$regex': escapeRegExp( req.body.company ), '$options': 'i' }

  User.find( conditions, '_id name surname company' ).limit( 5 )
{'fraction_non_alphanumeric': 0.192722371967655, 'fraction_numerical': 0.007412398921832884, 'mean_word_length': 3.513677811550152, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3607634', 'n_tokens_mistral': 504, 'n_tokens_neox': 480, 'n_words': 134}