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

commonMain.org.jetbrains.letsPlot.intern.ThemeOptionsUtil.kt Maven / Gradle / Ivy

There is a newer version: 4.9.2
Show newest version
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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy