<commit_before>
//  (c) 2012 <NAME>
//  MIT Licensed
//  For all details and documentation:
//  https://github.com/tblobaum/mongoose-troop

// Pagination
// ----------

// Plugin
function pagination (schema, options) {
  options = options || {}

  // Options
  var defaultLimit = options.defaultLimit || 10
    , defaultQuery = options.defaultQuery || {}
    , defaultFields = options.defaultFields || {}
    , defaultSort = options.defaultSort || {}
    , defaultPopulate = options.defaultPopulate || null
    , remember = options.remember || false
    , defaultPage = 1
    , currentLimit = defaultLimit
    , currentQuery = defaultQuery
    , currentFields = defaultFields
    , currentSort = defaultSort
    , currentPopulate = defaultPopulate

  // Main pagination method, can pass `query`, `page`, `limit`
  // and `fields` into the options to pass directly into the 
  // mongoose ORM, returns the standard `err` and `docs`, along with
  // the total object count, total pages, and current page
  schema.static('paginate', function (opt, fn) {
    if (!fn) { fn = opt; opt = {} }
    var self = this
      , query = opt.query || currentQuery
      , page = opt.page || defaultPage
      , limit = opt.limit || currentLimit
      , fields = opt.fields || currentFields
      , sort = opt.sort || currentSort
      , populate = opt.populate || currentPopulate

    if (remember) {
      opt.query && (currentQuery = opt.query)
      opt.limit && (currentLimit = opt.limit)
      opt.fields && (currentFields = opt.fields)
      opt.sort && (currentSort = opt.sort)
      opt.populate && (currentPopulate = opt.populate)
    }

    // default query
    var q = this.find(query, fields, {
      skip: (limit * (page - 1))
    , limit: limit
    });
    // sorting
    if (sort)
      q.sort(sort);
    // population
    if (populate)
      q.populate(populate);
    // execute query
    q.exec(function(err, docs) {
      if (err) return (fn && fn(err))
      self.count(query, function (err, count) {
        if (err) return (fn && fn(err))
        fn && fn(err, docs, count, Math.ceil(count / limit), page)
      })
    })
    return this
  })
  
  // Shortcut method for retrieving the first page
  schema.static('firstPage', function (opt, fn) {
    if (!fn) { fn = opt; opt = {} }
    var query = opt.query || currentQuery
      , limit = opt.limit || currentLimit
      , fields = opt.fields || currentFields
      , sort = opt.sort || currentSort
      , populate = opt.populate || currentPopulate
    
    this.paginate({
      query: query
    , page: 1
    , limit: limit
    , fields: fields
    , sort: sort
    , populate: populate
    }, fn)
    return this
  })

  // Shortcut method for retrieving the last page
  schema.static('lastPage', function (opt, fn) {
    if (!fn) { fn = opt; opt = {} }<commit_msg>updated structure<commit_after>
//  (c) 2012 <NAME>
//  MIT Licensed
//  For all details and documentation:
//  https://github.com/tblobaum/mongoose-troop

// Pagination
// ----------

// Plugin
function pagination (schema, options) {
  options = options || {}

  // Options
  var defaultLimit = options.defaultLimit || 10
    , defaultQuery = options.defaultQuery || {}
    , defaultFields = options.defaultFields || {}
    , defaultSort = options.defaultSort || {}
    , defaultPopulate = options.defaultPopulate || null
    , remember = options.remember || false
    , defaultPage = 1
    , currentLimit = defaultLimit
    , currentQuery = defaultQuery
    , currentFields = defaultFields
    , currentSort = defaultSort
    , currentPopulate = defaultPopulate

  // Main pagination method, can pass `query`, `page`, `limit`
  // and `fields` into the options to pass directly into the 
  // mongoose ORM, returns the standard `err` and `docs`, along with
  // the total object count, total pages, and current page
  schema.static('paginate', function (opt, fn) {
    if (!fn) { fn = opt; opt = {} }
    var self = this
      , query = opt.query || currentQuery
      , page = opt.page || defaultPage
      , limit = opt.limit || currentLimit
      , fields = opt.fields || currentFields
      , sort = opt.sort || currentSort
      , populate = opt.populate || currentPopulate

    if (remember) {
      opt.query && (currentQuery = opt.query)
      opt.limit && (currentLimit = opt.limit)
      opt.fields && (currentFields = opt.fields)
      opt.sort && (currentSort = opt.sort)
      opt.populate && (currentPopulate = opt.populate)
    }

    // default query
    var q = this.find(query, fields, {
      skip: (limit * (page - 1))
    , limit: limit
    });
    // sorting
    if (sort)
      q.sort(sort);
    // population
    if (populate)
      q.populate(populate);
    // execute query
    q.exec(function(err, docs) {
      if (err) return (fn && fn(err))
      self.count(query, function (err, count) {
        if (err) return (fn && fn(err))
        fn && fn(err, { docs: docs, count: count, pages: Math.ceil(count / limit), page: page, limit: limit })
      })
    })
    return this
  })
  
  // Shortcut method for retrieving the first page
  schema.static('firstPage', function (opt, fn) {
    if (!fn) { fn = opt; opt = {} }
    var query = opt.query || currentQuery
      , limit = opt.limit || currentLimit
      , fields = opt.fields || currentFields
      , sort = opt.sort || currentSort
      , populate = opt.populate || currentPopulate
    
    this.paginate({
      query: query
    , page: 1
    , limit: limit
    , fields: fields
    , sort: sort
    , populate: populate
    }, fn)
    return this
  })

  // Shortcut method for retrieving the last page
  schema.static('lastPage', function (opt, fn) {
    if (!fn) { fn = opt; opt = {} }
{'fraction_non_alphanumeric': 0.13574184496667835, 'fraction_numerical': 0.003156787092248334, 'mean_word_length': 2.448004836759371, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '3577272', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1686, 'n_words': 495}