From 1f8298f6264b387f69c6162f2815a3c741ec9a61 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 9 Feb 2022 01:26:33 +0600 Subject: [PATCH] attribute builder now may skip nullable values --- .../dev/inmo/jsuikit/UIKitAttributeValueBuilder.kt | 3 ++- .../dev/inmo/jsuikit/utils/AttributeBuilder.kt | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/jsMain/kotlin/dev/inmo/jsuikit/UIKitAttributeValueBuilder.kt b/src/jsMain/kotlin/dev/inmo/jsuikit/UIKitAttributeValueBuilder.kt index 4713631..e454721 100644 --- a/src/jsMain/kotlin/dev/inmo/jsuikit/UIKitAttributeValueBuilder.kt +++ b/src/jsMain/kotlin/dev/inmo/jsuikit/UIKitAttributeValueBuilder.kt @@ -24,9 +24,10 @@ class UIKitAttributeValueBuilder { fun AttrsBuilder<*>.buildAndAddAttribute( attributeName: String, + skipNullValues: Boolean = true, block: AttributeBuilder.() -> Unit ) { - buildAttribute(attributeName, block).let { + buildAttribute(attributeName, skipNullValues, block).let { attr(it.first, it.second) } } diff --git a/src/jsMain/kotlin/dev/inmo/jsuikit/utils/AttributeBuilder.kt b/src/jsMain/kotlin/dev/inmo/jsuikit/utils/AttributeBuilder.kt index ea829b3..a0ae1d7 100644 --- a/src/jsMain/kotlin/dev/inmo/jsuikit/utils/AttributeBuilder.kt +++ b/src/jsMain/kotlin/dev/inmo/jsuikit/utils/AttributeBuilder.kt @@ -2,10 +2,15 @@ package dev.inmo.jsuikit.utils class AttributeBuilder ( val attributeName: String, + val skipNullValues: Boolean = true, private val parametersPreset: MutableMap = mutableMapOf() ) { - fun add(k: String, v: Any? = null) = parametersPreset.set(k, v ?.toString()) + fun add(k: String, v: Any? = null) { + if (v != null || !skipNullValues) { + parametersPreset[k] = v ?.toString() + } + } infix fun String.to(value: Any?) = add(this, value) operator fun String.unaryPlus() = add(this, null) @@ -16,6 +21,7 @@ class AttributeBuilder ( ) } -inline fun buildAttribute(attributeName: String, block: AttributeBuilder.() -> Unit) = AttributeBuilder( - attributeName +inline fun buildAttribute(attributeName: String, skipNullValues: Boolean = true, block: AttributeBuilder.() -> Unit) = AttributeBuilder( + attributeName, + skipNullValues ).apply(block).build()