<commit_before>var _ = require('lodash')
var http = require('http')

module.exports = function (model, filter, options) {
  var queryOptions = {
    protected: ['skip', 'limit', 'sort', 'distinct', 'populate', 'select', 'lean', '$and', '$or', 'query', 'projection'],
    current: {}
  }

  // H+ - JSON query param parser, exposes OR, AND and WHERE methods
  // TODO - improve to serve recursive logical operators
  function jsonQueryParser (key, value) {
    if (_.isString(value)) {
      if (value[0] === '~') { // parse RegExp
        return new RegExp(value.substring(1), 'i')
      } else if (value[0] === '>') {
        if (value[1] === '=') {
          return {$gte: value.substr(2)}
        } else {
          return {$gt: value.substr(1)}
        }
      } else if (value[0] === '<') {
        if (value[1] === '=') {
          return {$lte: value.substr(2)}
        } else {
          return {$lt: value.substr(1)}
        }
      } else if (value[0] === '!' && value[1] === '=') {
        return {$ne: value.substr(2)}
      } else if (value[0] === '=') {
        return {$eq: value.substring(1)}
      }
    } else if (_.isArray(value)) {
      if (model.schema.paths.hasOwnProperty(key)) {
        return {$in: value}
      }
    }

    return value
  }

  function build (query, req) {
    var excludedarr = filter.getExcluded(req.access)
    var arr, i, re

    for (var key in queryOptions.clean) {
      if (excludedarr.indexOf(key) !== -1) {
        // caller tries to query for excluded keys. for security
        // reasons, we will skip the first -1 objects (to provoke
        // an error) and immediately return
        return query.skip(-1)
      }

      query.where(key)
      var value = queryOptions.clean[key]

      if (value[0] === '~') {
        re = new RegExp(value.substring(1), 'i')
        query.where(key).regex(re)
      } else if (value[0] === '>') {
        if (value[1] === '=') {
          query.gte(value.substr(2))
        } else {
          query.gt(value.substr(1))
        }
      } else if (value[0] === '<') {
        if (value[1] === '=') {
          query.lte(value.substr(2))
        } else {
          query.lt(value.substr(1))
        }
      } else if (value[0] === '!' && value[1] === '=') { // H+ for !=
        query.ne(value.substr(2))
      } else if (value[0] === '[' && value[value.length - 1] === ']') {
        query.in(value.substr(1, value.length - 2).split(','))
      } else if (value[0] === '=') {
        query.equals(value.substring(1))
      } else {
        query.equals(value)
      }
    }

    // H+ exposes Query AND, OR and WHERE methods
    if (queryOptions.current.query) {
      query.where(JSON.parse(queryOptions.current.query, jsonQueryParser))
    }

    // TODO - as introduction of QUERY param obsoletes need of $and
    if (queryOptions.current.$and) {
      query.and(JSON.parse(queryOptions.current.$and, jsonQueryParser))
    }

    // TODO - as introduction of QUERY param obsoletes need of $or
    if (queryOptions.current.$or) {
      query.or(JSON.parse(queryOptions.current.$or, jsonQueryParser))
    }

    if (queryOptions.current.skip) {
      query.skip(queryOptions.current.skip)
    }
<commit_msg>refactor: remove $and and $or parameters<commit_after>var _ = require('lodash')
var http = require('http')

module.exports = function (model, filter, options) {
  var queryOptions = {
    protected: ['skip', 'limit', 'sort', 'distinct', 'populate', 'select', 'lean', 'query', 'projection'],
    current: {}
  }

  // H+ - JSON query param parser, exposes OR, AND and WHERE methods
  // TODO - improve to serve recursive logical operators
  function jsonQueryParser (key, value) {
    if (_.isString(value)) {
      if (value[0] === '~') { // parse RegExp
        return new RegExp(value.substring(1), 'i')
      } else if (value[0] === '>') {
        if (value[1] === '=') {
          return {$gte: value.substr(2)}
        } else {
          return {$gt: value.substr(1)}
        }
      } else if (value[0] === '<') {
        if (value[1] === '=') {
          return {$lte: value.substr(2)}
        } else {
          return {$lt: value.substr(1)}
        }
      } else if (value[0] === '!' && value[1] === '=') {
        return {$ne: value.substr(2)}
      } else if (value[0] === '=') {
        return {$eq: value.substring(1)}
      }
    } else if (_.isArray(value)) {
      if (model.schema.paths.hasOwnProperty(key)) {
        return {$in: value}
      }
    }

    return value
  }

  function build (query, req) {
    var excludedarr = filter.getExcluded(req.access)
    var arr, i, re

    for (var key in queryOptions.clean) {
      if (excludedarr.indexOf(key) !== -1) {
        // caller tries to query for excluded keys. for security
        // reasons, we will skip the first -1 objects (to provoke
        // an error) and immediately return
        return query.skip(-1)
      }

      query.where(key)
      var value = queryOptions.clean[key]

      if (value[0] === '~') {
        re = new RegExp(value.substring(1), 'i')
        query.where(key).regex(re)
      } else if (value[0] === '>') {
        if (value[1] === '=') {
          query.gte(value.substr(2))
        } else {
          query.gt(value.substr(1))
        }
      } else if (value[0] === '<') {
        if (value[1] === '=') {
          query.lte(value.substr(2))
        } else {
          query.lt(value.substr(1))
        }
      } else if (value[0] === '!' && value[1] === '=') { // H+ for !=
        query.ne(value.substr(2))
      } else if (value[0] === '[' && value[value.length - 1] === ']') {
        query.in(value.substr(1, value.length - 2).split(','))
      } else if (value[0] === '=') {
        query.equals(value.substring(1))
      } else {
        query.equals(value)
      }
    }

    // H+ exposes Query AND, OR and WHERE methods
    if (queryOptions.current.query) {
      query.where(JSON.parse(queryOptions.current.query, jsonQueryParser))
    }

    if (queryOptions.current.skip) {
      query.skip(queryOptions.current.skip)
    }

{'fraction_non_alphanumeric': 0.18198347107438018, 'fraction_numerical': 0.012231404958677685, 'mean_word_length': 2.1482830385015608, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '3981284', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1965, 'n_words': 455}