All files / util apiPaginate.js

36.36% Statements 8/22
0% Branches 0/8
0% Functions 0/4
36.36% Lines 8/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45    15x 15x   15x   15x   15x                             15x                       15x             15x  
'use strict'
 
const LIMIT_DEFAULT = 50
const LIMIT_MAX = 1000
 
const OFFSET_DEFAULT = 0
 
const DEFAULT_MAX_RESULT_WINDOW = 10000
 
const parseIntValue = function(value, defaultValue, maxLimit) {
  if (!value) {
    return defaultValue
  }
 
  var v = parseInt(value)
  if (!maxLimit) {
    return v
  }
 
  return v <= maxLimit ? v : maxLimit
}
 
function ApiPaginate() {}
 
ApiPaginate.parseOptionsForElasticSearch = function(params) {
  var limit = parseIntValue(params.limit, LIMIT_DEFAULT, LIMIT_MAX)
  var offset = parseIntValue(params.offset, OFFSET_DEFAULT)
 
  // See https://github.com/crowi/crowi/pull/293
  if (limit + offset > DEFAULT_MAX_RESULT_WINDOW) {
    throw new Error(`(limit + offset) must be less than or equal to ${DEFAULT_MAX_RESULT_WINDOW}`)
  }
 
  return { limit: limit, offset: offset }
}
 
ApiPaginate.parseOptions = function(params) {
  var limit = parseIntValue(params.limit, LIMIT_DEFAULT, LIMIT_MAX)
  var offset = parseIntValue(params.offset, OFFSET_DEFAULT)
 
  return { limit: limit, offset: offset }
}
 
module.exports = ApiPaginate