All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.forkhandles.values.orNull.kt Maven / Gradle / Ivy

The newest version!
package dev.forkhandles.values

/**
 * Return a Object/null based on validation.
 */

fun , PRIMITIVE : Any> ValueFactory.ofOrNull(value: PRIMITIVE) =
    try {
        validate(value)
    } catch (e: Exception) {
        null
    }

fun , PRIMITIVE : Any> ValueFactory.parseOrNull(value: String) =
    try {
        validate(parseFn(value))
    } catch (e: Exception) {
        null
    }

fun , PRIMITIVE : Any> ValueFactory.ofListOrNull(vararg values: PRIMITIVE) =
    ofListOrNull(values.toList())

fun , PRIMITIVE : Any> ValueFactory.ofListOrNull(values: List) =
    values.orNull(::ofOrNull)

fun , PRIMITIVE : Any> ValueFactory.parseListOrNull(values: List) =
    values.orNull(::parseOrNull)

fun , PRIMITIVE : Any> ValueFactory.parseListOrNull(vararg values: String) =
    parseListOrNull(values.toList())

private fun , PRIMITIVE : Any> List.orNull(fn: (IN) -> DOMAIN?) =
    when {
        isEmpty() -> emptyList()
        else -> mapNotNull(fn).takeIf(List<*>::isNotEmpty)
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy