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

commonMain.in.shabinder.soundbound.utils.Ext.kt Maven / Gradle / Ivy

package `in`.shabinder.soundbound.utils

import `in`.shabinder.soundbound.models.Artist
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlin.jvm.JvmName
import kotlin.math.min


@Suppress("NOTHING_TO_INLINE")
internal inline fun List.cleaned(): List {
    return this.filter { it.isNotBlank() && it != "null" }
}

@Suppress("NOTHING_TO_INLINE")
@JvmName("cleanedArtists")
internal inline fun List.cleaned(): List {
    return filter { it.name.isNotBlank() && it.name != "null" }
}

// Not the Best Practice, use when you just care less for the exceptions.
inline fun  safeRunCatching(block: () -> R): Result {
    return try {
        Result.success(block())
    } catch (t: TimeoutCancellationException) {
        Result.failure(t)
    } catch (c: CancellationException) {
        throw c
    } catch (e: Throwable) {
        Result.failure(e)
    }
}

fun  T.limitDecimals(maxDecimals: Int): String {
    val result = toString()
    val lastIndex = result.length - 1
    var pos = lastIndex
    while (pos >= 0 && result[pos] != '.') {
        pos--
    }
    return if (maxDecimals < 1 && pos >= 0) {
        result.substring(0, min(pos, result.length))
    } else if (pos >= 0) {
        result.substring(0, min(pos + 1 + maxDecimals, result.length))
    } else {
        return result
    }
}

val GlobalJson by lazy {
    Json {
        isLenient = true
        ignoreUnknownKeys = true
        coerceInputValues = true
        encodeDefaults = true

        @OptIn(ExperimentalSerializationApi::class)
        explicitNulls = false
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy