Skip to content

Commit f8c4b12

Browse files
Merge pull request #51 from nimblehq/chore/50-add-contract-to-string-extensions
[#50] As a developer, I can use String extensions in effective way
2 parents e2207c3 + de023ea commit f8c4b12

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

  • common-ktx/src/main/java/co/nimblehq/common/extensions

common-ktx/src/main/java/co/nimblehq/common/extensions/StringExt.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ package co.nimblehq.common.extensions
22

33
import androidx.core.util.PatternsCompat
44
import java.util.*
5+
import kotlin.contracts.ExperimentalContracts
6+
import kotlin.contracts.contract
7+
8+
private val THAI_REGEX = "([\\u0E00-\\u0E7F]+)".toRegex()
59

610
/**
711
* Check if the string not null or empty.
812
* This extension wraps for more readable.
913
*
1014
* @return true if this nullable char sequence is NOT either null or empty
1115
*/
12-
fun String?.isNotNullOrEmpty(): Boolean = !this.isNullOrEmpty()
16+
@OptIn(ExperimentalContracts::class)
17+
fun String?.isNotNullOrEmpty(): Boolean {
18+
contract {
19+
returns(true) implies (this@isNotNullOrEmpty != null)
20+
}
21+
return !this.isNullOrEmpty()
22+
}
1323

1424
/**
1525
* Check if the string not null or blank.
@@ -18,7 +28,13 @@ fun String?.isNotNullOrEmpty(): Boolean = !this.isNullOrEmpty()
1828
* @return true if this nullable char sequence is NOT either null or empty or consists solely of
1929
* whitespace
2030
*/
21-
fun String?.isNotNullOrBlank(): Boolean = !this.isNullOrBlank()
31+
@OptIn(ExperimentalContracts::class)
32+
fun String?.isNotNullOrBlank(): Boolean {
33+
contract {
34+
returns(true) implies (this@isNotNullOrBlank != null)
35+
}
36+
return !this.isNullOrBlank()
37+
}
2238

2339
/**
2440
* Eliminate the given character then titleize.
@@ -68,6 +84,5 @@ fun String.isEmailValid(): Boolean {
6884
* @return true if this string is Thai
6985
*/
7086
fun String.isThai(): Boolean {
71-
val thaiRegex = "([\\u0E00-\\u0E7F]+)".toRegex()
72-
return if (isEmpty() || isBlank()) false else all { thaiRegex.matches(it.toString()) }
87+
return isNotEmpty() && all { THAI_REGEX.matches(it.toString()) }
7388
}

0 commit comments

Comments
 (0)