mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-04 23:59:29 +00:00
Revert "more annotations to god of annotations"
This reverts commit f8a8808508
.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import kotlin.js.JsExport
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
|
||||
@@ -10,7 +9,6 @@ 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.
|
||||
@@ -27,7 +25,6 @@ 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
|
||||
|
||||
@@ -37,21 +34,18 @@ 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(),
|
||||
@@ -61,7 +55,6 @@ 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,9 +1,7 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.js.JsExport
|
||||
|
||||
@JsExport
|
||||
@Serializable
|
||||
data class PaginationResult<T>(
|
||||
override val page: Int,
|
||||
@@ -12,10 +10,8 @@ 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
|
||||
@@ -29,7 +25,6 @@ fun <T> List<T>.createPaginationResult(
|
||||
pagination.size
|
||||
)
|
||||
|
||||
@JsExport
|
||||
fun <T> List<T>.createPaginationResult(
|
||||
firstIndex: Int,
|
||||
commonObjectsNumber: Long
|
||||
@@ -43,7 +38,6 @@ fun <T> List<T>.createPaginationResult(
|
||||
size
|
||||
)
|
||||
|
||||
@JsExport
|
||||
fun <T> Pair<Long, List<T>>.createPaginationResult(
|
||||
pagination: Pagination
|
||||
) = second.createPaginationResult(pagination, first)
|
||||
|
@@ -1,14 +1,12 @@
|
||||
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(
|
||||
@@ -16,7 +14,6 @@ inline fun FirstPagePagination(size: Int = defaultMediumPageSize) =
|
||||
size = size
|
||||
)
|
||||
|
||||
@JsExport
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun Pagination.nextPage() =
|
||||
SimplePagination(
|
||||
@@ -24,14 +21,12 @@ 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,8 +1,5 @@
|
||||
package dev.inmo.micro_utils.pagination
|
||||
|
||||
import kotlin.js.JsExport
|
||||
|
||||
@JsExport
|
||||
inline fun doWithPagination(
|
||||
startPagination: Pagination = FirstPagePagination(),
|
||||
requestMaker: (pagination: Pagination) -> Pagination?
|
||||
@@ -13,7 +10,6 @@ inline fun doWithPagination(
|
||||
}
|
||||
}
|
||||
|
||||
@JsExport
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun PaginationResult<*>.nextPageIfNotEmpty() = if (results.isNotEmpty()) {
|
||||
SimplePagination(
|
||||
@@ -24,7 +20,6 @@ inline fun PaginationResult<*>.nextPageIfNotEmpty() = if (results.isNotEmpty())
|
||||
null
|
||||
}
|
||||
|
||||
@JsExport
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun PaginationResult<*>.thisPageIfNotEmpty(): Pagination? = if (results.isNotEmpty()) {
|
||||
this
|
||||
|
@@ -1,9 +1,7 @@
|
||||
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>()
|
||||
@@ -22,7 +20,6 @@ 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,
|
||||
@@ -30,7 +27,6 @@ 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,27 +1,20 @@
|
||||
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