mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-04 23:59:29 +00:00
more annotations to god of annotations
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import kotlin.js.JsExport
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
|
||||
@@ -9,6 +10,7 @@ import kotlin.math.floor
|
||||
* If you want to request something, you should use [SimplePagination]. If you need to return some result including
|
||||
* pagination - [PaginationResult]
|
||||
*/
|
||||
@JsExport
|
||||
interface Pagination {
|
||||
/**
|
||||
* Started with 0.
|
||||
@@ -25,6 +27,7 @@ interface Pagination {
|
||||
/**
|
||||
* First number in index of objects. It can be used as offset for databases or other data sources
|
||||
*/
|
||||
@JsExport
|
||||
val Pagination.firstIndex: Int
|
||||
get() = page * size
|
||||
|
||||
@@ -34,18 +37,21 @@ val Pagination.firstIndex: Int
|
||||
* [[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.
|
||||
*/
|
||||
@JsExport
|
||||
val Pagination.lastIndex: Int
|
||||
get() = firstIndex + size - 1
|
||||
|
||||
/**
|
||||
* Calculates pages count for given [datasetSize]
|
||||
*/
|
||||
@JsExport
|
||||
fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int {
|
||||
return ceil(datasetSize.toDouble() / pageSize).toInt()
|
||||
}
|
||||
/**
|
||||
* Calculates pages count for given [datasetSize]
|
||||
*/
|
||||
@JsExport
|
||||
fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int =
|
||||
calculatePagesNumber(
|
||||
datasetSize.toLong(),
|
||||
@@ -55,6 +61,7 @@ fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int =
|
||||
/**
|
||||
* @return calculated page number which can be correctly used in [PaginationResult] as [PaginationResult.page] value
|
||||
*/
|
||||
@JsExport
|
||||
fun calculatePage(firstIndex: Int, resultsSize: Int): Int = if (resultsSize > 0) {
|
||||
floor(firstIndex.toFloat() / resultsSize).toInt()
|
||||
} else {
|
||||
|
@@ -1,7 +1,9 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.js.JsExport
|
||||
|
||||
@JsExport
|
||||
@Serializable
|
||||
data class PaginationResult<T>(
|
||||
override val page: Int,
|
||||
@@ -10,8 +12,10 @@ data class PaginationResult<T>(
|
||||
override val size: Int
|
||||
) : Pagination
|
||||
|
||||
@JsExport
|
||||
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0)
|
||||
|
||||
@JsExport
|
||||
fun <T> List<T>.createPaginationResult(
|
||||
pagination: Pagination,
|
||||
commonObjectsNumber: Long
|
||||
@@ -25,6 +29,7 @@ fun <T> List<T>.createPaginationResult(
|
||||
pagination.size
|
||||
)
|
||||
|
||||
@JsExport
|
||||
fun <T> List<T>.createPaginationResult(
|
||||
firstIndex: Int,
|
||||
commonObjectsNumber: Long
|
||||
@@ -38,6 +43,7 @@ fun <T> List<T>.createPaginationResult(
|
||||
size
|
||||
)
|
||||
|
||||
@JsExport
|
||||
fun <T> Pair<Long, List<T>>.createPaginationResult(
|
||||
pagination: Pagination
|
||||
) = second.createPaginationResult(pagination, first)
|
||||
|
@@ -1,12 +1,14 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.js.JsExport
|
||||
|
||||
const val defaultSmallPageSize = 2
|
||||
const val defaultMediumPageSize = 5
|
||||
const val defaultLargePageSize = 10
|
||||
const val defaultExtraLargePageSize = 15
|
||||
|
||||
@JsExport
|
||||
@Suppress("NOTHING_TO_INLINE", "FunctionName")
|
||||
inline fun FirstPagePagination(size: Int = defaultMediumPageSize) =
|
||||
SimplePagination(
|
||||
@@ -14,6 +16,7 @@ inline fun FirstPagePagination(size: Int = defaultMediumPageSize) =
|
||||
size = size
|
||||
)
|
||||
|
||||
@JsExport
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun Pagination.nextPage() =
|
||||
SimplePagination(
|
||||
@@ -21,12 +24,14 @@ inline fun Pagination.nextPage() =
|
||||
size
|
||||
)
|
||||
|
||||
@JsExport
|
||||
@Serializable
|
||||
data class SimplePagination(
|
||||
override val page: Int,
|
||||
override val size: Int
|
||||
) : Pagination
|
||||
|
||||
@JsExport
|
||||
fun Pagination(
|
||||
page: Int,
|
||||
size: Int
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import kotlin.js.JsExport
|
||||
|
||||
@JsExport
|
||||
inline fun doWithPagination(
|
||||
startPagination: Pagination = FirstPagePagination(),
|
||||
requestMaker: (pagination: Pagination) -> Pagination?
|
||||
@@ -10,6 +13,7 @@ inline fun doWithPagination(
|
||||
}
|
||||
}
|
||||
|
||||
@JsExport
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun PaginationResult<*>.nextPageIfNotEmpty() = if (results.isNotEmpty()) {
|
||||
SimplePagination(
|
||||
@@ -20,6 +24,7 @@ inline fun PaginationResult<*>.nextPageIfNotEmpty() = if (results.isNotEmpty())
|
||||
null
|
||||
}
|
||||
|
||||
@JsExport
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun PaginationResult<*>.thisPageIfNotEmpty(): Pagination? = if (results.isNotEmpty()) {
|
||||
this
|
||||
|
@@ -1,7 +1,9 @@
|
||||
package dev.inmo.micro_utils.pagination.utils
|
||||
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import kotlin.js.JsExport
|
||||
|
||||
@JsExport
|
||||
fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||
var i = 0
|
||||
val result = mutableListOf<T>()
|
||||
@@ -20,6 +22,7 @@ fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||
return result.createPaginationResult(with, i.toLong())
|
||||
}
|
||||
|
||||
@JsExport
|
||||
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||
return subList(with.firstIndex, with.lastIndex + 1).createPaginationResult(
|
||||
with,
|
||||
@@ -27,6 +30,7 @@ fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||
)
|
||||
}
|
||||
|
||||
@JsExport
|
||||
fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||
return this.drop(with.firstIndex).take(with.size).createPaginationResult(
|
||||
with,
|
||||
|
@@ -1,20 +1,27 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import kotlin.js.JsExport
|
||||
|
||||
@JsExport
|
||||
const val paginationPageKey = "ppage"
|
||||
@JsExport
|
||||
const val paginationSizeKey = "psize"
|
||||
|
||||
@JsExport
|
||||
val Pagination.asUrlQueryParts
|
||||
get() = mapOf(
|
||||
paginationPageKey to page.toString(),
|
||||
paginationSizeKey to size.toString()
|
||||
)
|
||||
|
||||
@JsExport
|
||||
val Pagination.asUrlQueryArrayParts
|
||||
get() = arrayOf(
|
||||
paginationPageKey to page.toString(),
|
||||
paginationSizeKey to size.toString()
|
||||
)
|
||||
|
||||
@JsExport
|
||||
val Map<String, String?>.extractPagination: Pagination
|
||||
get() = SimplePagination(
|
||||
get(paginationPageKey) ?.toIntOrNull() ?: 0,
|
||||
|
Reference in New Issue
Block a user