diff --git a/build.gradle.kts b/build.gradle.kts index e8070ad..a0e8894 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -123,7 +123,9 @@ tasks.withType().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) diff --git a/src/commonMain/kotlin/pers/shawxingkwok/ktutil/Lazy.kt b/src/commonMain/kotlin/pers/shawxingkwok/ktutil/Lazy.kt index b593afc..87d4515 100755 --- a/src/commonMain/kotlin/pers/shawxingkwok/ktutil/Lazy.kt +++ b/src/commonMain/kotlin/pers/shawxingkwok/ktutil/Lazy.kt @@ -9,21 +9,21 @@ import kotlin.reflect.KProperty */ public inline fun fastLazy(crossinline initialize: () -> T): ReadWriteProperty = object : ReadWriteProperty { - 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 } } diff --git a/src/commonMain/kotlin/pers/shawxingkwok/ktutil/Number.toOrder.kt b/src/commonMain/kotlin/pers/shawxingkwok/ktutil/Number.toOrder.kt index 908fb40..eb78ad1 100644 --- a/src/commonMain/kotlin/pers/shawxingkwok/ktutil/Number.toOrder.kt +++ b/src/commonMain/kotlin/pers/shawxingkwok/ktutil/Number.toOrder.kt @@ -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" } @@ -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() @@ -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() @@ -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() \ No newline at end of file diff --git a/src/commonTest/kotlin/AllTest.kt b/src/commonTest/kotlin/AllTest.kt index b3c42b9..66ab100 100644 --- a/src/commonTest/kotlin/AllTest.kt +++ b/src/commonTest/kotlin/AllTest.kt @@ -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 @@ -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) + } } \ No newline at end of file