<commit_before>'use strict'

const _ = require('lodash');

module.exports = function (Model, options) {
  const defaultOptions = {
    limit: 25
  };

  const resultOptions = Object.assign({}, defaultOptions, options);

  Model.beforeRemote('find', (ctx, instance, next) => {
    const page = _.get(ctx.args, 'filter.page', 1);
    const limit = _.get(ctx.args, 'filter.limit', resultOptions.limit);
    const skip = limit * (page - 1)

    _.set(ctx.args, 'filter.skip', skip);
    _.set(ctx.args, 'filter.limit', limit);

    process.nextTick(next);
  });

  Model.afterRemote('find', (ctx, instance, next) => {
    const page = parseInt(_.get(ctx.args, 'filter.page', 1));
    const limit = parseInt(_.get(ctx.args, 'filter.limit', false));

    const where = _.get(ctx.args, 'filter.where', {});

    Model.count(where)
      .then(countResources => {
        const pages = limit ? Math.ceil(countResources / limit) : 1;
        ctx.result = {
          docs: ctx.result,
          pages: {
            current: page,
            prev: page - 1,
            hasPrev: (page - 1) > 0,
            next: page + 1,
            hasNext: (page < pages),
            total: pages
          },
          items: {
            limit,
            total: countResources
          }
        };

        process.nextTick(next);
      })
      .catch(err => next(err));
  });
}<commit_msg>Add support to custom methods<commit_after>'use strict'

const _ = require('lodash');

module.exports = function (Model, options) {
  const defaultOptions = {
    limit: 25
  };

  const resultOptions = Object.assign({}, defaultOptions, options);
  const methods = resultOptions.methods || [];

  const beforeRemote = (ctx, instance, next) => {
    const page = _.get(ctx.args, 'filter.page', 1);
    const limit = _.get(ctx.args, 'filter.limit', resultOptions.limit);
    const skip = limit * (page - 1)

    _.set(ctx.args, 'filter.skip', skip);
    _.set(ctx.args, 'filter.limit', limit);

    process.nextTick(next);
  };

  const afterRemote = (ctx, instance, next) => {
    const page = parseInt(_.get(ctx.args, 'filter.page', 1));
    const limit = parseInt(_.get(ctx.args, 'filter.limit', false));
    const where = _.get(ctx.args, 'filter.where', {});

    Model.count(where)
      .then(countResources => {
        const pages = limit ? Math.ceil(countResources / limit) : 1;
        ctx.result = {
          docs: ctx.result,
          pages: {
            current: page,
            prev: page - 1,
            hasPrev: (page - 1) > 0,
            next: page + 1,
            hasNext: (page < pages),
            total: pages
          },
          items: {
            limit,
            total: countResources
          }
        };

        process.nextTick(next);
      })
      .catch(err => next(err));
  };

  Model.beforeRemote('find', beforeRemote);
  Model.afterRemote('find', afterRemote);

  methods.forEach((method) => {
    Model.beforeRemote(method, beforeRemote);
    Model.afterRemote(method, afterRemote);
  });
}
{'fraction_non_alphanumeric': 0.15596026490066225, 'fraction_numerical': 0.006622516556291391, 'mean_word_length': 2.2344753747323343, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, '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': '7032470', 'n_tokens_mistral': 946, 'n_tokens_neox': 907, 'n_words': 212}