<commit_before>module.exports = {
  checkErrors: function (fields, req) {
    for(var name in fields) {
      var field = fields[name];
      var value = req.param(name);
      if(field.type === 'numeric') {
        if(
          !/^\d+$/.test(value) ||
          (field.optional == true && typeof value !== 'undefined')
        ) {
          var message = field.label || field.name;
          message += 'is not valid, must be integer';
          return message;
        }
      }
      // Add more types here.
    }

    return false;
  },

  /**
   * index() - list all documents.
   */
  index: function(req, res) {

    if(!req.session || !req.session.user || !req.session.user.id) {
      return res.badRequest('Unauthorized.');
    }

    var isNumeric = /^\d+$/;
    if(!isNumeric.test(req.session.user.id) && !isNumeric.test(req.param('id'))) {
      return res.badRequest('id is not valid, must be integer.');
    }

    var errors = module.exports.checkErrors({
      'page': {
        'type': 'numeric',
        'optional': true
      },
      'limit': {
        'type': 'numeric',
        'optional': true
      },
      'is_template': {
        'type': 'numeric',
        'optional': true
      }
    }, req);

    // TODO: Send bad request on errors.

    var page = req.param('page', 1);

    var limit = req.param('limit', null);

    var filters = {
      'is_template': req.param('is_template', null)
    };

    // Use a promise to get both queries in parallel.
    // We return the promise so we can test this.
    return Promise.all([
      Document.findByUser(req.session.user.id, page, limit, filters),
      Document.countByUser(req.session.user.id, filters)
    ]).spread(function(documents, count) {
      // Get all of our results.
      return res.json({
        page: page,
        limit: limit,
        count: count.count,
        documents: documents
      });
    }).catch(function(error) {
      // Handle any errors - this occurs on
      // the *first* error.
      return res.badRequest(error);
    });
  },
  /**
   * find() - find a single document by its slug.<commit_msg>Cleanup comments.<commit_after>module.exports = {
  checkErrors: function (fields, req) {
    for(var name in fields) {
      var field = fields[name];
      var value = req.param(name);
      if(field.type === 'numeric') {
        if(
          !/^\d+$/.test(value) ||
          (field.optional == true && typeof value !== 'undefined')
        ) {
          var message = field.label || field.name;
          message += 'is not valid, must be integer';
          return message;
        }
      }
      // Add more types here.
    }

    return false;
  },

  /**
   * index() - list all documents.
   */
  index: function(req, res) {

    if(!req.session || !req.session.user || !req.session.user.id) {
      return res.badRequest('Unauthorized.');
    }

    var isNumeric = /^\d+$/;
    if(!isNumeric.test(req.session.user.id) && !isNumeric.test(req.param('id'))) {
      return res.badRequest('id is not valid, must be integer.');
    }

    var errors = module.exports.checkErrors({
      'page': {
        'type': 'numeric',
        'optional': true
      },
      'limit': {
        'type': 'numeric',
        'optional': true
      },
      'is_template': {
        'type': 'numeric',
        'optional': true
      }
    }, req);

    // TODO: Send bad request on errors.

    var page = req.param('page', 1);

    var limit = req.param('limit', null);

    var filters = {
      'is_template': req.param('is_template', null)
    };

    // Use a promise to get both queries in parallel.
    // We return the promise so we can test this.
    return Promise.all([
      Document.findByUser(req.session.user.id, page, limit, filters),
      Document.countByUser(req.session.user.id, filters)
    ]).spread(function(documents, count) {
      // Get all of our results.
      return res.json({
        page: page,
        limit: limit,
        count: count.count,
        documents: documents
      });
    }).catch(function(error) {
      // Handle any errors - this occurs on
      // the *first* error.
      return res.badRequest(error);
    });
  },
  /**
   * find() - find a single document by its id.
{'fraction_non_alphanumeric': 0.15499290108849975, 'fraction_numerical': 0.000473260766682442, 'mean_word_length': 1.9788583509513742, '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': '2999484', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1271, 'n_words': 338}