summaryrefslogtreecommitdiffstats
path: root/modules/util/paginate.go
blob: 87f31b76ed268f51ce02376aaacc1f9bdd848bc9 (plain)
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
// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package util

import "reflect"

// PaginateSlice cut a slice as per pagination options
// if page = 0 it do not paginate
func PaginateSlice(list any, page, pageSize int) any {
	if page <= 0 || pageSize <= 0 {
		return list
	}
	if reflect.TypeOf(list).Kind() != reflect.Slice {
		return list
	}

	listValue := reflect.ValueOf(list)

	page--

	if page*pageSize >= listValue.Len() {
		return listValue.Slice(listValue.Len(), listValue.Len()).Interface()
	}

	listValue = listValue.Slice(page*pageSize, listValue.Len())

	if listValue.Len() > pageSize {
		return listValue.Slice(0, pageSize).Interface()
	}

	return listValue.Interface()
}