add optionallyReverse extensions

This commit is contained in:
InsanusMokrassar 2022-01-17 15:38:23 +06:00
parent 7a9b7d98a1
commit 85b3e48d18
3 changed files with 30 additions and 0 deletions

View File

@ -2,6 +2,10 @@
## 0.9.4 ## 0.9.4
* `Pagination`:
* `Common`:
* Add several `optionallyReverse` functions
## 0.9.3 ## 0.9.3
* `Versions`: * `Versions`:

View File

@ -38,3 +38,17 @@ fun <T> Set<T>.paginate(with: Pagination): PaginationResult<T> {
size.toLong() size.toLong()
) )
} }
fun <T> Iterable<T>.optionallyReverse(reverse: Boolean) = if (reverse) {
reversed()
} else {
this
}
inline fun <reified T> Array<T>.optionallyReverse(reverse: Boolean) = if (reverse) {
Array(size) {
get(lastIndex - it)
}
} else {
this
}

View File

@ -26,3 +26,15 @@ fun Pagination.reverse(datasetSize: Long): SimplePagination {
* Shortcut for [reverse] * Shortcut for [reverse]
*/ */
fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong()) fun Pagination.reverse(objectsCount: Int) = reverse(objectsCount.toLong())
fun Pagination.optionallyReverse(objectsCount: Int, reverse: Boolean) = if (reverse) {
reverse(objectsCount)
} else {
this
}
fun Pagination.optionallyReverse(objectsCount: Long, reverse: Boolean) = if (reverse) {
reverse(objectsCount)
} else {
this
}