fix for paginate (again) and lastIndexExclusive

This commit is contained in:
InsanusMokrassar 2020-11-03 19:43:45 +06:00
parent 35913b95be
commit 2f42b30f87
3 changed files with 12 additions and 2 deletions

View File

@ -4,6 +4,7 @@
* `Pagination`
* Fixes in function `List#paginate`
* Extension property `Pagination#lastIndexExclusive`
## 0.2.5

View File

@ -28,6 +28,15 @@ interface Pagination {
val Pagination.firstIndex: Int
get() = page * size
/**
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
*
* [[firstIndex], [lastIndex]]; That means, that for [Pagination] with [Pagination.size] == 10 and [Pagination.page] == 1
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19. Here [Pagination.lastIndexExclusive] == 20
*/
val Pagination.lastIndexExclusive: Int
get() = firstIndex + size
/**
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
*
@ -35,7 +44,7 @@ val Pagination.firstIndex: Int
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19.
*/
val Pagination.lastIndex: Int
get() = firstIndex + size - 1
get() = lastIndexExclusive - 1
/**
* Calculates pages count for given [datasetSize]

View File

@ -21,7 +21,7 @@ fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
}
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
return subList(maxOf(with.firstIndex, 0), (minOf(with.lastIndex, lastIndex)) + 1).createPaginationResult(
return subList(maxOf(with.firstIndex, 0), minOf(with.lastIndexExclusive, size)).createPaginationResult(
with,
size.toLong()
)