Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamKwokX committed Aug 8, 2023
1 parent 08889a8 commit a86adf0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ tasks.withType<DokkaTask>().configureEach {

// publish
mavenPublishing {
coordinates("io.github.shawxingkwok", "kt-util", "1.0.0-SNAPSHOT")
val isSnapshot = false
val version = "1.0.0"
coordinates("io.github.shawxingkwok", "kt-util", if (isSnapshot) "$version-SNAPSHOT" else version)
pom {
val repo = "KtUtil"
name.set(repo)
Expand Down
8 changes: 4 additions & 4 deletions src/commonMain/kotlin/pers/shawxingkwok/ktutil/Lazy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import kotlin.reflect.KProperty
*/
public inline fun <T> fastLazy(crossinline initialize: () -> T): ReadWriteProperty<Any?, T> =
object : ReadWriteProperty<Any?, T> {
var _value: Any? = null
var value: Any? = null
var initialized = false

override fun getValue(thisRef: Any?, property: KProperty<*>): T {
if (!initialized){
initialized = true
_value = initialize()
value = initialize()
}

@Suppress("UNCHECKED_CAST")
return _value as T
return value as T
}

override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
_value = value
this.value = value
}
}

Expand Down
42 changes: 24 additions & 18 deletions src/commonMain/kotlin/pers/shawxingkwok/ktutil/Number.toOrder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package pers.shawxingkwok.ktutil

private fun Number.parseToOrder(): String{
val str = toString()
val suffix = when(str.last()){
'1' -> "st"
'2' -> "nd"
'3' -> "rd"
else -> "th"
}

val suffix =
if (str.getOrNull(str.lastIndex - 1) == '1')
"th"
else
when(str.last()){
'1' -> "st"
'2' -> "nd"
'3' -> "rd"
else -> "th"
}

return "$str$suffix"
}

Expand All @@ -17,10 +23,10 @@ private fun Number.parseToOrder(): String{
* Usage example:
*
* ```
* 11.toShort().toOrder() // 11st
* 12.toShort().toOrder() // 12nd
* 13.toShort().toOrder() // 13rd
* 14.toShort().toOrder() // 14th
* 21.toShort().toOrder() // 21st
* 32.toShort().toOrder() // 32nd
* 43.toShort().toOrder() // 43rd
* 54.toShort().toOrder() // 54th
*/
public fun Short.toOrder(): String = parseToOrder()

Expand All @@ -30,10 +36,10 @@ public fun Short.toOrder(): String = parseToOrder()
* Usage example:
*
* ```
* 11.toOrder() // 11st
* 12.toOrder() // 12nd
* 13.toOrder() // 13rd
* 14.toOrder() // 14th
* 21.toOrder() // 21st
* 32.toOrder() // 32nd
* 43.toOrder() // 43rd
* 54.toOrder() // 54th
*/
public fun Int.toOrder(): String = parseToOrder()

Expand All @@ -42,9 +48,9 @@ public fun Int.toOrder(): String = parseToOrder()
*
* Usage example:
* ```
* 11L.toOrder() // 11st
* 12L.toOrder() // 12nd
* 13L.toOrder() // 13rd
* 14L.toOrder() // 14th
* 21L.toOrder() // 21st
* 32L.toOrder() // 32nd
* 43L.toOrder() // 43rd
* 54L.toOrder() // 54th
*/
public fun Long.toOrder(): String = parseToOrder()
12 changes: 12 additions & 0 deletions src/commonTest/kotlin/AllTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pers.shawxingkwok.ktutil.KReadOnlyProperty
import pers.shawxingkwok.ktutil.KReadWriteProperty
import pers.shawxingkwok.ktutil.getOrPutNullable
import pers.shawxingkwok.ktutil.toOrder
import kotlin.reflect.KProperty
import kotlin.test.Test

Expand Down Expand Up @@ -35,4 +36,15 @@ class AllTest {
x = "Fp"
println(31)
}

@Test
fun testToOrder(){
1.toOrder().let(::println)
11.toOrder().let(::println)
21.toOrder().let(::println)

2.toOrder().let(::println)
12.toOrder().let(::println)
22.toOrder().let(::println)
}
}

0 comments on commit a86adf0

Please sign in to comment.