Created
November 24, 2020 23:28
-
-
Save fluidsonic/473758c30dd084042905e77c5ebd0374 to your computer and use it in GitHub Desktop.
Array.push/join-based StringBuilder for Kotlin/JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public external interface StringBuilder | |
public inline fun StringBuilder.append(value: Any) { | |
append("$value") | |
} | |
public inline fun StringBuilder.append(string: String) { | |
if (string != "") | |
asDynamic().push(string) | |
} | |
public inline fun StringBuilder.isEmpty(): Boolean = | |
asDynamic().length == 0 | |
public inline fun StringBuilder.isNotEmpty(): Boolean = | |
asDynamic().length != 0 | |
public inline fun buildString(action: StringBuilder.() -> Unit): String { | |
val builder = js("[]") | |
action(builder.unsafeCast<StringBuilder>()) | |
return builder.join("").unsafeCast<String>() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment