cosmos-sdk/client/utils.go

31 lines
729 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. If the start page is invalid, non-positive
// values are returned signaling the request is invalid.
//
// NOTE: The start page is assumed to be 1-indexed.
func Paginate(numObjs, page, limit, defLimit int) (start, end int) {
if page == 0 {
// invalid start page
return -1, -1
} else if limit == 0 {
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
}