Skip to content

Commit

Permalink
Some more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Elifarley Cruz committed Jan 2, 2020
1 parent 3b7bd28 commit 19db5ec
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
32 changes: 32 additions & 0 deletions base/src/main/kotlin/klib/base/FunKit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package klib.base

/**
* Created by elifarley on 22/11/16.
*/

inline fun Any?.nullIf(nullValue: Any) = this.takeUnless { this == nullValue }

/**
* Calls the specified function [block] with no argument and always returns `Unit`.
*/
inline fun unit(block: () -> Any?) {
block()
}

/**
* Calls the specified function [block] with `this` value as its argument and always returns `Unit`.
*/
inline fun <T> T.unit(block: (T) -> Any?) {
block(this)
}

/**
* Can be used to transform a `when` receiver into an expression, making it **exhaustive**.
* @return `Unit`
*/
val Any?.unit inline get() = Unit

inline operator fun <T> Boolean?.rem(block: () -> T): T? = this?.let { if (this) block() else null }

inline infix operator fun <A, B, C> ((A) -> B).rem(crossinline other: (B) -> C): (A) -> C =
{ other(this(it)) }
10 changes: 10 additions & 0 deletions base/src/main/kotlin/klib/base/LoggingKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ abstract class WithLogging {
val log: Logger by lazyLogger()
}

/**
* Executes the given [block], logs elapsed time and returns whatever [block] returned.
*/
inline fun <T> Logger.debugTime(logMessage: String? = null, block: () -> T): T = System.nanoTime().let { start ->
block().also {
if (isDebugEnabled)
debug("{}; ELAPSED {} ms", logMessage, 1e3 * (System.nanoTime() - start))
}
}

class MDCCloseable : Closeable {

private val keys = mutableSetOf<String>()
Expand Down
24 changes: 0 additions & 24 deletions base/src/main/kotlin/klib/base/MiscKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,8 @@ import java.util.concurrent.ThreadLocalRandom
* Created by elifarley on 22/11/16.
*/

inline fun Any?.nullIf(nullValue: Any) = if (this == nullValue) null else this

/**
* Calls the specified function [block] with no argument and always returns `Unit`.
*/
inline fun unit(block: () -> Any?) {
block()
}

/**
* Calls the specified function [block] with `this` value as its argument and always returns `Unit`.
*/
inline fun <T> T.unit(block: (T) -> Any?) {
block(this)
}

/**
* Can be used to transform a `when` receiver into an expression, making it **exhaustive**.
* @return `Unit`
*/
val Any?.unit inline get() = Unit

val Throwable.simpleMessage get() = "(${javaClass.simpleName}) ${message.trimToDefault(toString())}"

inline operator fun <T> Boolean?.rem(block: () -> T): T? = this?.let { if (this) block() else null }

//fun StopWatch.stopIfRunning(): StopWatch { if (!isStopped) stop(); return this }

fun Random.nextInt(range: IntRange): Int = if (range.first == range.last) range.first
Expand Down
18 changes: 18 additions & 0 deletions base/src/main/kotlin/klib/base/StringKit.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package klib.base

import kotlin.random.Random

val ALPHA_CHARS = ('a'..'z') + ('A'..'Z')
val ALPHANUM_CHARS = ALPHA_CHARS + ('0'..'9')
val COMMON_CHARS = ALPHANUM_CHARS + ".- ".toList()

// TODO Remove once it gets out of experimental state in Kotlin stdlib
fun CharArray.concatToString() = String(this)

fun Random.string(len: Int = 10, chars: Array<Char> = ALPHANUM_CHARS.toTypedArray()) = CharArray(len) {
Random.nextInt(0, ALPHANUM_CHARS.size).let(ALPHANUM_CHARS::get)
}.concatToString()

inline fun String.prefixWith(prefix: String?) = prefix?.plus(this) ?: this

fun Any?.fmt(format: String, defaultWhenNull: String = ""): String = this?.let {
Expand All @@ -22,6 +35,11 @@ fun String?.toNormalizedLowerCase() = this.trimToNull()
?.toLowerCase()?.replace(illegalNameCharsLowerCase, " ")
?.toNormalizedSpaces()

private val notAlpha: Regex by lazy { "[^0-9a-zA-Z]+".toRegex() }
fun String?.toNormalizedAlpha() = this
?.replace(notAlpha, " ")
?.toNormalizedSpaces()

private val noDigits: Regex by lazy { "[^0-9]+".toRegex() }
val String?.tryLong get() = this?.replace(noDigits, "")?.toLongOrNull()

0 comments on commit 19db5ec

Please sign in to comment.