38 lines
930 B
Go
38 lines
930 B
Go
package client
|
|
|
|
// Paginate returns the correct starting and ending index for a paginated query,
|
|
// given that client provides a desired page and limit of objects and the handler
|
|
// provides the total number of objects. The start page is assumed to be 1-indexed.
|
|
// If the start page is invalid, non-positive values are returned signaling the
|
|
// request is invalid; it returns non-positive values if limit is non-positive and
|
|
// defLimit is negative.
|
|
func Paginate(numObjs, page, limit, defLimit int) (start, end int) {
|
|
if page <= 0 {
|
|
// invalid start page
|
|
return -1, -1
|
|
}
|
|
|
|
// fallback to default limit if supplied limit is invalid
|
|
if limit <= 0 {
|
|
if defLimit < 0 {
|
|
// invalid default limit
|
|
return -1, -1
|
|
}
|
|
limit = defLimit
|
|
}
|
|
|
|
start = (page - 1) * limit
|
|
end = limit + start
|
|
|
|
if end >= numObjs {
|
|
end = numObjs
|
|
}
|
|
|
|
if start >= numObjs {
|
|
// page is out of bounds
|
|
return -1, -1
|
|
}
|
|
|
|
return start, end
|
|
}
|