diff --git a/CHANGELOG.md b/CHANGELOG.md index 5073a7fa383..823ea1b34f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * `Pagination` * Fixes in function `List#paginate` + * Extension property `Pagination#lastIndexExclusive` ## 0.2.5 diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt index 2ea52f141ae..adfa364f0fc 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt @@ -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] diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt index f6e631646fe..f28f1fc62f2 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt @@ -21,7 +21,7 @@ fun Iterable.paginate(with: Pagination): PaginationResult { } fun List.paginate(with: Pagination): PaginationResult { - 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() )