commonMain.org.jetbrains.letsPlot.intern.ThemeOptionsUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-kotlin-kernel Show documentation
Show all versions of lets-plot-kotlin-kernel Show documentation
Lets-Plot Kotlin API without dependencies.
package org.jetbrains.letsPlot.intern
import org.jetbrains.letsPlot.core.spec.Option
internal object ThemeOptionsUtil {
fun toSpec(themeOptionList: List): Map? {
val specList = themeOptionList.map { it.toSpec() }
val result: Map? = specList.reduceOrNull() { prev, next ->
val merged = next[Option.Meta.NAME]?.let {
// keep the previously specified flavor
val flavor = prev.filterKeys { it == Option.Theme.FLAVOR }
// 'named' theme overrides all prev theme options.
next + flavor
} ?: mergeThemeOptions(prev, next)
merged.toMutableMap()
}
return result
}
private fun mergeThemeOptions(m0: Map, m1: Map): Map {
val overlappingKeys = m0.keys.intersect(m1.keys)
val keysToMerge = overlappingKeys.filter {
m0[it] is Map<*, *> && m1[it] is Map<*, *>
}
val m2 = keysToMerge.map {
it to (m0[it] as Map<*, *> + m1[it] as Map<*, *>)
}.toMap()
return m0 + m1 + m2
}
}