Skip to content

Commit

Permalink
Test build
Browse files Browse the repository at this point in the history
  • Loading branch information
cjaehnen committed Nov 23, 2023
1 parent 88a4226 commit bedfecf
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ package io.opengood.commons.kotlin.extension.method

import io.opengood.commons.kotlin.infix.then

fun <T : Any> Array<T>.printAll() =
isNotEmpty() then { forEachIndexed { i: Int, item: T -> println("Item #$i: $item") } }
fun <T : Any> Array<T>.printAll() = isNotEmpty() then { forEachIndexed { i: Int, item: T -> println("Item #$i: $item") } }
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package io.opengood.commons.kotlin.extension.method
import java.math.BigDecimal
import java.math.RoundingMode

fun BigDecimal.roundUp(scale: Int): BigDecimal =
this.setScale(scale, RoundingMode.HALF_UP)
fun BigDecimal.roundUp(scale: Int): BigDecimal = this.setScale(scale, RoundingMode.HALF_UP)
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.opengood.commons.kotlin.extension.method

fun List<String>.containsIgnoreCase(element: String): Boolean =
any { it.contains(element, ignoreCase = true) }
fun List<String>.containsIgnoreCase(element: String): Boolean = any { it.contains(element, ignoreCase = true) }

fun <K : Any, V : Any> List<Map<K, V>>.sortAscending(key: K): List<Map<K, V>> =
sortedBy { it[key] as String }
fun <K : Any, V : Any> List<Map<K, V>>.sortAscending(key: K): List<Map<K, V>> = sortedBy { it[key] as String }

fun <K : Any, V : Any> List<Map<K, V>>.sortDescending(key: K): List<Map<K, V>> =
sortedByDescending { it[key] as String }
fun <K : Any, V : Any> List<Map<K, V>>.sortDescending(key: K): List<Map<K, V>> = sortedByDescending { it[key] as String }
15 changes: 5 additions & 10 deletions src/main/kotlin/io/opengood/commons/kotlin/extension/method/Map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import io.opengood.commons.kotlin.infix.then
fun <K : Any, V : Any> Map<K, List<V>>.containsMultipleListItems(key: K): Boolean =
(containsKey(key) then { this[key]!!.size > 1 }) ?: false

fun <K : Any, V : Any> Map<K, V>.keyByIndex(i: Int): K =
keys.toList()[i]
fun <K : Any, V : Any> Map<K, V>.keyByIndex(i: Int): K = keys.toList()[i]

fun <K : Any, V : Any> Map<K, V>.notContainsKey(key: K): Boolean =
!containsKey(key)
fun <K : Any, V : Any> Map<K, V>.notContainsKey(key: K): Boolean = !containsKey(key)

fun <K : Any, V : Any> Map<K, V>.notContainsValue(value: V): Boolean =
!containsValue(value)
fun <K : Any, V : Any> Map<K, V>.notContainsValue(value: V): Boolean = !containsValue(value)

fun <K : Any, V : Any> MutableMap<K, V>.putIfNotAbsent(entry: Map.Entry<K, V>?) =
entry?.let { putIfAbsent(it.key, it.value) }
fun <K : Any, V : Any> MutableMap<K, V>.putIfNotAbsent(entry: Map.Entry<K, V>?) = entry?.let { putIfAbsent(it.key, it.value) }

fun <K : Any, V : Any> Map<K, V>.valueByIndex(i: Int): V =
values.toList()[i]
fun <K : Any, V : Any> Map<K, V>.valueByIndex(i: Int): V = values.toList()[i]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.opengood.commons.kotlin.extension.method

fun <K : Any, V : Any> Pair<K, V>.toEntry() = object : Map.Entry<K, V> {
override val key: K = first
override val value: V = second
}
fun <K : Any, V : Any> Pair<K, V>.toEntry() =
object : Map.Entry<K, V> {
override val key: K = first
override val value: V = second
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,16 @@ fun String.capitalizeWord(): String =
}
}

fun String.capitalizeWords(): String =
split(String.whitespace).joinToString(String.whitespace) { it.capitalizeWord() }
fun String.capitalizeWords(): String = split(String.whitespace).joinToString(String.whitespace) { it.capitalizeWord() }

fun String.containsIgnoreCase(substr: String): Boolean =
(isNotBlank() then { contains(substr, ignoreCase = true) }) ?: false
fun String.containsIgnoreCase(substr: String): Boolean = (isNotBlank() then { contains(substr, ignoreCase = true) }) ?: false

fun String.equalsIgnoreCase(value: String): Boolean =
(isNotBlank() then { equals(value, ignoreCase = true) }) ?: false
fun String.equalsIgnoreCase(value: String): Boolean = (isNotBlank() then { equals(value, ignoreCase = true) }) ?: false

fun String.isBoolean(): Boolean =
(isNotBlank() then { listOf("true", "false", "1", "0").any { it.equalsIgnoreCase(this) } }) ?: false
fun String.isBoolean(): Boolean = (isNotBlank() then { listOf("true", "false", "1", "0").any { it.equalsIgnoreCase(this) } }) ?: false

fun String.isInt(): Boolean =
(isNotBlank() then { all { Character.isDigit(it) } }) ?: false
fun String.isInt(): Boolean = (isNotBlank() then { all { Character.isDigit(it) } }) ?: false

fun String.isUuid(): Boolean =
Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\$").matches(this)
fun String.isUuid(): Boolean = Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\$").matches(this)

fun String.toIntOrZero(): Int =
toIntOrNull() ?: 0
fun String.toIntOrZero(): Int = toIntOrNull() ?: 0
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.opengood.commons.kotlin.extension.property
import java.math.BigDecimal

class Decimal {

companion object {
val zero: BigDecimal = BigDecimal(0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.opengood.commons.kotlin.extension.property
import java.util.UUID

class Uuid {

companion object {
val empty: UUID = UUID(0L, 0L)
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/io/opengood/commons/kotlin/function/Map.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.opengood.commons.kotlin.function

fun <K : Any, V : Any> makeEntry(key: K, value: V) = object : Map.Entry<K, V> {
fun <K : Any, V : Any> makeEntry(
key: K,
value: V,
) = object : Map.Entry<K, V> {
override val key: K = key
override val value: V = value
}
3 changes: 1 addition & 2 deletions src/main/kotlin/io/opengood/commons/kotlin/infix/Boolean.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package io.opengood.commons.kotlin.infix

infix fun <T> Boolean.then(param: () -> T): T? =
if (this) param() else null
infix fun <T> Boolean.then(param: () -> T): T? = if (this) param() else null
3 changes: 1 addition & 2 deletions src/main/kotlin/io/opengood/commons/kotlin/infix/Generic.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package io.opengood.commons.kotlin.infix

infix fun <T : Any> T?.firstOrDefault(default: T): T =
((this != null) then { this }) ?: default
infix fun <T : Any> T?.firstOrDefault(default: T): T = ((this != null) then { this }) ?: default
3 changes: 1 addition & 2 deletions src/main/kotlin/io/opengood/commons/kotlin/infix/Int.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package io.opengood.commons.kotlin.infix

infix fun Int.isDivFactorOf(value: Int): Boolean =
this % value == 0
infix fun Int.isDivFactorOf(value: Int): Boolean = this % value == 0
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import io.opengood.commons.kotlin.function.captureStdOut
class ArrayTest : FunSpec({

test("array of generic type printAll extension method prints all items to standard output") {
val expected = """
val expected =
"""
|Item #0: foo
|Item #1: bar
|
""".trimMargin()
""".trimMargin()

val output = captureStdOut { arrayOf("foo", "bar").printAll() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,54 +26,58 @@ class ListTest : FunSpec({
}

test("list sortAscending extension method sorts list of maps keys in ascending order") {
val list = listOf(
mapOf(
"foo" to "bar",
"baz" to "pas",
),
mapOf(
"foo" to "par",
"baz" to "taz",
),
)
val list =
listOf(
mapOf(
"foo" to "bar",
"baz" to "pas",
),
mapOf(
"foo" to "par",
"baz" to "taz",
),
)

val result = list.sortAscending("baz")

result shouldBe listOf(
mapOf(
"baz" to "pas",
"foo" to "bar",
),
mapOf(
"baz" to "taz",
"foo" to "par",
),
)
result shouldBe
listOf(
mapOf(
"baz" to "pas",
"foo" to "bar",
),
mapOf(
"baz" to "taz",
"foo" to "par",
),
)
}

test("list sortDescending extension method sorts list of maps keys in descending order") {
val list = listOf(
mapOf(
"baz" to "pas",
"foo" to "bar",
),
mapOf(
"baz" to "taz",
"foo" to "par",
),
)
val list =
listOf(
mapOf(
"baz" to "pas",
"foo" to "bar",
),
mapOf(
"baz" to "taz",
"foo" to "par",
),
)

val result = list.sortDescending("baz")

result shouldBe listOf(
mapOf(
"foo" to "par",
"baz" to "taz",
),
mapOf(
"foo" to "bar",
"baz" to "pas",
),
)
result shouldBe
listOf(
mapOf(
"foo" to "par",
"baz" to "taz",
),
mapOf(
"foo" to "bar",
"baz" to "pas",
),
)
}
})

0 comments on commit bedfecf

Please sign in to comment.