<commit_before>const errors = require('feathers-errors').errors;

export function lowerCase(... fields) {
  const lowerCaseFields = data => {
    for(let field of fields) {
      if(data[field]) {
        if(typeof data[field] !== 'string') {
          throw new errors.BadRequest('Expected string');
        } else {
          data[field] = data[field].toLowerCase();
        }
      }
    }
  };

  const callback = typeof fields[fields.length - 1] === 'function' ?
    fields.pop() : () => true;

  return function(hook) {
    const result = hook.type === 'before' ? hook.data : hook.result;
    const next = condition => {
      if(result && condition) {
        if(hook.method === 'find' || Array.isArray(result)) {
          // data.data if the find method is paginated
          (result.data || result).forEach(lowerCaseFields);
        } else {
          lowerCaseFields(result);
        }
      }
      return hook;
    };

    const check = callback(hook);

    return check && typeof check.then === 'function' ?
      check.then(next) : next(check);
  };
}

export function removeQuery(... fields) {
  const removeQueries = data => {
    for(let field of fields) {
      data[field] = undefined;
      delete data[field];
    }
  };
  
  const callback = typeof fields[fields.length - 1] === 'function' ?
    fields.pop() : () => true;

  return function(hook) {
    if (hook.type === 'after') {
      throw new errors.GeneralError(`Provider '${hook.params.provider}' can not remove query params on after hook.`);
    }
    const result = hook.params.query; 
    const next = condition => {
      if(result && condition) {
        removeQueries(result);
      }
      return hook;
    };

    const check = callback(hook);

    return check && typeof check.then === 'function' ?
      check.then(next) : next(check);
  };
}

export function pluckQuery(... fields) {
  const pluckQueries = data => {
    // admin, name
    for(let key of Object.keys(data)) {
      if(fields.indexOf(key) === -1) {
        console.log(key);
        data[key] = undefined;
        delete data[key];
      }
    }
  };
  
  const callback = typeof fields[fields.length - 1] === 'function' ?
    fields.pop() : () => true;

  return function(hook) {
    if (hook.type === 'after') {
      throw new errors.GeneralError(`Provider '${hook.params.provider}' can not pluck query params on after hook.`);
    }
    const result = hook.params.query;
    const next = condition => {
      if(result && condition) {
        pluckQueries(result);
      }
      return hook;
    };

    const check = callback(hook);

    return check && typeof check.then === 'function' ?
      check.then(next) : next(check);
  };
}

export function remove(... fields) {
  const removeFields = data => {<commit_msg>Removed garbage<commit_after>const errors = require('feathers-errors').errors;

export function lowerCase(... fields) {
  const lowerCaseFields = data => {
    for(let field of fields) {
      if(data[field]) {
        if(typeof data[field] !== 'string') {
          throw new errors.BadRequest('Expected string');
        } else {
          data[field] = data[field].toLowerCase();
        }
      }
    }
  };

  const callback = typeof fields[fields.length - 1] === 'function' ?
    fields.pop() : () => true;

  return function(hook) {
    const result = hook.type === 'before' ? hook.data : hook.result;
    const next = condition => {
      if(result && condition) {
        if(hook.method === 'find' || Array.isArray(result)) {
          // data.data if the find method is paginated
          (result.data || result).forEach(lowerCaseFields);
        } else {
          lowerCaseFields(result);
        }
      }
      return hook;
    };

    const check = callback(hook);

    return check && typeof check.then === 'function' ?
      check.then(next) : next(check);
  };
}

export function removeQuery(... fields) {
  const removeQueries = data => {
    for(let field of fields) {
      data[field] = undefined;
      delete data[field];
    }
  };
  
  const callback = typeof fields[fields.length - 1] === 'function' ?
    fields.pop() : () => true;

  return function(hook) {
    if (hook.type === 'after') {
      throw new errors.GeneralError(`Provider '${hook.params.provider}' can not remove query params on after hook.`);
    }
    const result = hook.params.query; 
    const next = condition => {
      if(result && condition) {
        removeQueries(result);
      }
      return hook;
    };

    const check = callback(hook);

    return check && typeof check.then === 'function' ?
      check.then(next) : next(check);
  };
}

export function pluckQuery(... fields) {
  const pluckQueries = data => {
    for(let key of Object.keys(data)) {
      if(fields.indexOf(key) === -1) {
        data[key] = undefined;
        delete data[key];
      }
    }
  };
  
  const callback = typeof fields[fields.length - 1] === 'function' ?
    fields.pop() : () => true;

  return function(hook) {
    if (hook.type === 'after') {
      throw new errors.GeneralError(`Provider '${hook.params.provider}' can not pluck query params on after hook.`);
    }
    const result = hook.params.query;
    const next = condition => {
      if(result && condition) {
        pluckQueries(result);
      }
      return hook;
    };

    const check = callback(hook);

    return check && typeof check.then === 'function' ?
      check.then(next) : next(check);
  };
}

export function remove(... fields) {
  const removeFields = data => {
{'fraction_non_alphanumeric': 0.15135037157875658, 'fraction_numerical': 0.0014500634402755121, 'mean_word_length': 2.40407156076496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '4689084', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1628, 'n_words': 410}