Skip to content

Instantly share code, notes, and snippets.

@fluidsonic
Created November 24, 2020 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fluidsonic/473758c30dd084042905e77c5ebd0374 to your computer and use it in GitHub Desktop.
Save fluidsonic/473758c30dd084042905e77c5ebd0374 to your computer and use it in GitHub Desktop.
Array.push/join-based StringBuilder for Kotlin/JS
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