From 11696ac4c085bb4d20cbbcf7cc99d0291b40c745 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Sun, 7 Jan 2018 00:52:44 -0500 Subject: [PATCH] Implement String.toBoolean() for JS. #KT-16348 --- js/js.libraries/src/core/numberConversions.kt | 5 +++++ libraries/stdlib/common/src/kotlin/TextH.kt | 5 +++++ libraries/stdlib/test/text/StringNumberConversionTest.kt | 1 - 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/js/js.libraries/src/core/numberConversions.kt b/js/js.libraries/src/core/numberConversions.kt index 8abfa49dcf9..394061268f2 100644 --- a/js/js.libraries/src/core/numberConversions.kt +++ b/js/js.libraries/src/core/numberConversions.kt @@ -18,6 +18,11 @@ package kotlin.text +/** + * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise. + */ +public fun String.toBoolean(): Boolean = toLowerCase() == "true" + /** * Parses the string as a signed [Byte] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. diff --git a/libraries/stdlib/common/src/kotlin/TextH.kt b/libraries/stdlib/common/src/kotlin/TextH.kt index e1f172266ac..c13716eacf3 100644 --- a/libraries/stdlib/common/src/kotlin/TextH.kt +++ b/libraries/stdlib/common/src/kotlin/TextH.kt @@ -126,6 +126,11 @@ expect fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, othe +/** + * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise. + */ +expect fun String.toBoolean(): Boolean + /** * Parses the string as a signed [Byte] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. diff --git a/libraries/stdlib/test/text/StringNumberConversionTest.kt b/libraries/stdlib/test/text/StringNumberConversionTest.kt index 7bd15aa72d9..2a5ed755bc6 100644 --- a/libraries/stdlib/test/text/StringNumberConversionTest.kt +++ b/libraries/stdlib/test/text/StringNumberConversionTest.kt @@ -5,7 +5,6 @@ import kotlin.test.* class StringNumberConversionTest { - @kotlin.jvm.JvmVersion @Test fun toBoolean() { assertEquals(true, "true".toBoolean()) assertEquals(true, "True".toBoolean())