<commit_before>import _ from 'lodash'
import {boolMerge} from './utils'

/**
 * QueryBuilder class to build query clauses.
 *
 * @private
 */
export default class QueryBuilder {

  constructor() {
    this._queries = {}
    this._filters = {}
    this._aggs = {}
  }

  get queries() {
    return _.cloneDeep(this._queries)
  }

  /**
   * Apply a query of a given type providing all the necessary arguments,
   * passing these arguments directly to the specified query builder. Merges
   * existing query(s) with the new query.
   *
   * @param  {String}  boolType Name of the query type.
   * @param  {...args} args     Arguments passed to query builder.
   * @returns {QueryBuilder}    Builder class.
   */
  _makeQuery(boolType, ...args) {
    let nested = _.last(args)

    let newQuery = this._buildQuery(...args)

    if (_.isFunction(nested)) {
      let clause = newQuery[_.findKey(newQuery)]
      let builder = new QueryBuilder()
      let recursiveResult = nested(builder)
      if (!_.isEmpty(recursiveResult._aggs)) {clause.aggs = recursiveResult._aggs}
      if (!_.isEmpty(recursiveResult._queries)) {clause.query = recursiveResult._queries}
      if (!_.isEmpty(recursiveResult._filters)) {clause.filter = recursiveResult._filters}

      // return newQuery
    }

    return boolMerge(newQuery, this._queries, boolType)
  }

  _buildQuery(type, field, value, opts) {
    let clause = {}

    if (field && value && opts) {
      clause = Object.assign({}, opts, {[field]: value})
    } else if (field && value) {
      clause = {[field]: value}
    } else if (field) {
      clause = {field}
    }

    return {[type]: clause}
  }

  _makeFilter(boolType, ...args) {
    let nested = _.last(args)

    let newFilter = this._buildFilter(...args)

    if (_.isFunction(nested)) {
      let clause = newFilter[_.findKey(newFilter)]
      let builder = new QueryBuilder()
      let recursiveResult = nested(builder)

      if (!_.isEmpty(recursiveResult._aggs)) {clause.aggs = recursiveResult._aggs}
      if (!_.isEmpty(recursiveResult._queries)) {clause.query = recursiveResult._queries}
      if (!_.isEmpty(recursiveResult._filters)) {clause.filter = recursiveResult._filters}

      // return newFilter
    }

    return boolMerge(newFilter, this._filters, boolType)
  }

  _buildFilter(type, field, value, opts) {
    let clause = {}

    if (field && value && opts) {
      clause = Object.assign({}, opts, {[field]: value})
    } else if (field && value) {
      clause = {[field]: value}
    } else if (field) {
      clause = {field}
    }

    return {[type]: clause}
  }

  _makeAggregation(...args) {
    let nested = _.last(args)

    let newAggregation = this._buildAggregation(...args)

    if (_.isFunction(nested)) {
      let clause = newAggregation[_.findKey(newAggregation)]
      let builder = new QueryBuilder()
      let recursiveResult = nested(builder)
      if (!_.isEmpty(recursiveResult._aggs)) {clause.aggs = recursiveResult._aggs}
      if (!_.isEmpty(recursiveResult._queries)) {clause.query = recursiveResult._queries}
      if (!_.isEmpty(recursiveResult._filters)) {clause.filter = recursiveResult._filters}
      // return newAggregation
    }

    return newAggregation
  }
<commit_msg>Remove commented lines<commit_after>import _ from 'lodash'
import {boolMerge} from './utils'

/**
 * QueryBuilder class to build query clauses.
 *
 * @private
 */
export default class QueryBuilder {

  constructor() {
    this._queries = {}
    this._filters = {}
    this._aggs = {}
  }

  get queries() {
    return _.cloneDeep(this._queries)
  }

  /**
   * Apply a query of a given type providing all the necessary arguments,
   * passing these arguments directly to the specified query builder. Merges
   * existing query(s) with the new query.
   *
   * @param  {String}  boolType Name of the query type.
   * @param  {...args} args     Arguments passed to query builder.
   * @returns {QueryBuilder}    Builder class.
   */
  _makeQuery(boolType, ...args) {
    let nested = _.last(args)

    let newQuery = this._buildQuery(...args)

    if (_.isFunction(nested)) {
      let clause = newQuery[_.findKey(newQuery)]
      let builder = new QueryBuilder()
      let recursiveResult = nested(builder)
      if (!_.isEmpty(recursiveResult._aggs)) {clause.aggs = recursiveResult._aggs}
      if (!_.isEmpty(recursiveResult._queries)) {clause.query = recursiveResult._queries}
      if (!_.isEmpty(recursiveResult._filters)) {clause.filter = recursiveResult._filters}
    }

    return boolMerge(newQuery, this._queries, boolType)
  }

  _buildQuery(type, field, value, opts) {
    let clause = {}

    if (field && value && opts) {
      clause = Object.assign({}, opts, {[field]: value})
    } else if (field && value) {
      clause = {[field]: value}
    } else if (field) {
      clause = {field}
    }

    return {[type]: clause}
  }

  _makeFilter(boolType, ...args) {
    let nested = _.last(args)

    let newFilter = this._buildFilter(...args)

    if (_.isFunction(nested)) {
      let clause = newFilter[_.findKey(newFilter)]
      let builder = new QueryBuilder()
      let recursiveResult = nested(builder)

      if (!_.isEmpty(recursiveResult._aggs)) {clause.aggs = recursiveResult._aggs}
      if (!_.isEmpty(recursiveResult._queries)) {clause.query = recursiveResult._queries}
      if (!_.isEmpty(recursiveResult._filters)) {clause.filter = recursiveResult._filters}
    }

    return boolMerge(newFilter, this._filters, boolType)
  }

  _buildFilter(type, field, value, opts) {
    let clause = {}

    if (field && value && opts) {
      clause = Object.assign({}, opts, {[field]: value})
    } else if (field && value) {
      clause = {[field]: value}
    } else if (field) {
      clause = {field}
    }

    return {[type]: clause}
  }

  _makeAggregation(...args) {
    let nested = _.last(args)

    let newAggregation = this._buildAggregation(...args)

    if (_.isFunction(nested)) {
      let clause = newAggregation[_.findKey(newAggregation)]
      let builder = new QueryBuilder()
      let recursiveResult = nested(builder)
      if (!_.isEmpty(recursiveResult._aggs)) {clause.aggs = recursiveResult._aggs}
      if (!_.isEmpty(recursiveResult._queries)) {clause.query = recursiveResult._queries}
      if (!_.isEmpty(recursiveResult._filters)) {clause.filter = recursiveResult._filters}
    }

    return newAggregation
  }

{'fraction_non_alphanumeric': 0.1319368453962795, 'fraction_numerical': 0.0, 'mean_word_length': 2.99875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7279939', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1927, 'n_words': 486}