30 lines
632 B
Go
30 lines
632 B
Go
package store
|
|
|
|
import "strconv"
|
|
|
|
// PaginateOffset returns a page from a pre-sorted slice using numeric string cursors (same scheme as ListRevisions).
|
|
func PaginateOffset[T any](all []T, cursor string, limit int) (page []T, nextCursor string, hasMore bool) {
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
off := 0
|
|
if cursor != "" {
|
|
if n, err := strconv.Atoi(cursor); err == nil && n >= 0 {
|
|
off = n
|
|
}
|
|
}
|
|
if off > len(all) {
|
|
off = len(all)
|
|
}
|
|
end := off + limit
|
|
if end > len(all) {
|
|
end = len(all)
|
|
}
|
|
page = all[off:end]
|
|
if end < len(all) {
|
|
nextCursor = strconv.Itoa(end)
|
|
hasMore = true
|
|
}
|
|
return page, nextCursor, hasMore
|
|
}
|