4 Commits

Author SHA1 Message Date
Unbound Release e18b226356 chore(release): prepare for v0.0.4 2025-06-16 10:09:08 +00:00
argoyle a8a1c613a4 fix(pagination): handle empty page return case
Adds a check for empty pages in the pagination logic to return a 
nil slice and empty PageInfo when there are no items to display. 
Updates tests to verify the behavior when no more items are 
available, ensuring robustness in pagination functionality.
2025-06-16 12:04:52 +02:00
Unbound Release e60459ddd4 chore(release): prepare for v0.0.3 2025-06-16 07:13:07 +00:00
argoyle 8e8bdac22c fix: update GetPage to check for empty items slice
Refactor the GetPage function to check for an empty slice of items 
instead of a nil pointer. Update tests to cover cases for nil and 
empty slices, ensuring consistent behavior and improved clarity.
2025-06-16 09:09:04 +02:00
3 changed files with 29 additions and 2 deletions
+12
View File
@@ -2,6 +2,18 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [0.0.4] - 2025-06-16
### 🐛 Bug Fixes
- *(pagination)* Handle empty page return case
## [0.0.3] - 2025-06-16
### 🐛 Bug Fixes
- Update GetPage to check for empty items slice
## [0.0.2] - 2025-06-15 ## [0.0.2] - 2025-06-15
### 🐛 Bug Fixes ### 🐛 Bug Fixes
+4 -1
View File
@@ -52,7 +52,7 @@ func EncodeCursor(cursor string) string {
} }
func GetPage[T any](items []T, first *int, after *string, last *int, before *string, max int, fn func(T) string) ([]T, PageInfo) { func GetPage[T any](items []T, first *int, after *string, last *int, before *string, max int, fn func(T) string) ([]T, PageInfo) {
if items == nil { if len(items) == 0 {
return nil, PageInfo{} return nil, PageInfo{}
} }
tmp := min(max, len(items)) tmp := min(max, len(items))
@@ -88,6 +88,9 @@ func GetPage[T any](items []T, first *int, after *string, last *int, before *str
eIx = idx eIx = idx
} }
page := items[sIx:min(eIx, len(items))] page := items[sIx:min(eIx, len(items))]
if len(page) == 0 {
return nil, PageInfo{}
}
return page, PageInfo{ return page, PageInfo{
StartCursor: ptr(EncodeCursor(fn(page[0]))), StartCursor: ptr(EncodeCursor(fn(page[0]))),
HasNextPage: eIx < len(items), HasNextPage: eIx < len(items),
+13 -1
View File
@@ -24,11 +24,17 @@ func TestGetPage(t *testing.T) {
} }
tests := []testCase[string]{ tests := []testCase[string]{
{ {
name: "empty", name: "nil items",
args: args[string]{max: 10}, args: args[string]{max: 10},
wantItems: nil, wantItems: nil,
wantPageInfo: PageInfo{}, wantPageInfo: PageInfo{},
}, },
{
name: "empty items",
args: args[string]{items: []string{}, max: 10},
wantItems: nil,
wantPageInfo: PageInfo{},
},
{ {
name: "first 2", name: "first 2",
args: args[string]{items: []string{"1", "2", "3"}, first: ptr(2), max: 10, fn: func(s string) string { return s }}, args: args[string]{items: []string{"1", "2", "3"}, first: ptr(2), max: 10, fn: func(s string) string { return s }},
@@ -39,6 +45,12 @@ func TestGetPage(t *testing.T) {
EndCursor: ptr("Mg=="), EndCursor: ptr("Mg=="),
}, },
}, },
{
name: "no more items",
args: args[string]{items: []string{"1", "2", "3"}, first: ptr(2), after: ptr("Mw=="), max: 10, fn: func(s string) string { return s }},
wantItems: nil,
wantPageInfo: PageInfo{},
},
{ {
name: "first 10", name: "first 10",
args: args[string]{items: []string{"1", "2", "3"}, first: ptr(10), max: 10, fn: func(s string) string { return s }}, args: args[string]{items: []string{"1", "2", "3"}, first: ptr(10), max: 10, fn: func(s string) string { return s }},