fix: prevent negative slice index when last exceeds items count #62
@@ -64,6 +64,9 @@ func GetPage[T any](items []T, first *int, after *string, last *int, before *str
|
|||||||
} else if last != nil {
|
} else if last != nil {
|
||||||
tmp = *last
|
tmp = *last
|
||||||
sIx = len(items) - tmp
|
sIx = len(items) - tmp
|
||||||
|
if sIx < 0 {
|
||||||
|
sIx = 0
|
||||||
|
}
|
||||||
eIx = len(items)
|
eIx = len(items)
|
||||||
}
|
}
|
||||||
if cursor, err := DecodeCursor(after); err == nil && cursor != "" {
|
if cursor, err := DecodeCursor(after); err == nil && cursor != "" {
|
||||||
|
|||||||
@@ -116,6 +116,17 @@ func TestGetPage(t *testing.T) {
|
|||||||
EndCursor: ptr("MQ=="),
|
EndCursor: ptr("MQ=="),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "last exceeds items count",
|
||||||
|
args: args[string]{items: []string{"1", "2", "3"}, last: ptr(10), max: 10, fn: func(s string) string { return s }},
|
||||||
|
wantItems: []string{"1", "2", "3"},
|
||||||
|
wantPageInfo: PageInfo{
|
||||||
|
StartCursor: ptr("MQ=="),
|
||||||
|
HasNextPage: false,
|
||||||
|
HasPreviousPage: false,
|
||||||
|
EndCursor: ptr("Mw=="),
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user