org.jetbrains.kotlinx.kandy.letsplot.util.containerUtil.kt Maven / Gradle / Ivy
The newest version!
package org.jetbrains.kotlinx.kandy.letsplot.util
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
internal fun Map.extendedBy(other: Map, join: (V, V) -> V): Map {
return (this + other).mapValues { (k, v) ->
if (k in this && k in other)
join(this[k]!!, other[k]!!)
else
v
}
}
@OptIn(ExperimentalContracts::class)
private inline fun bothOfType(a: Any, b: Any): Boolean {
contract { returns(true) implies (a is T && b is T) }
return a is T && b is T
}
@OptIn(ExperimentalContracts::class)
private inline fun bothOfTypeAnd(a: Any, b: Any, condition: (T) -> Boolean): Boolean {
contract { returns(true) implies (a is T && b is T) }
return a is T && b is T && condition(a) && condition(b)
}
internal infix fun Map.extendedByJson(other: Map): JsonObject {
val map = this.extendedBy(other) { a, b ->
// This logic might be enhanced
when {
bothOfTypeAnd(a, b) { it.isString } -> JsonPrimitive(a.content + b.content)
bothOfType(a, b) -> JsonArray(a + b)
bothOfType(a, b) -> JsonObject(a + b)
else -> a
}
}
return JsonObject(map)
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy