Skip to content

Commit b44e38e

Browse files
committed
Use String Extensions
1 parent a30bcc1 commit b44e38e

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

core/common/src/commonMain/kotlin/org/mifospay/core/common/utils/StringExtensions.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ fun maskString(input: String, maskChar: Char = '*'): String {
3131
fun String.capitalizeWords(): String = split(" ").joinToString(" ") { it ->
3232
it.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
3333
}
34+
35+
fun String.hasSpaces(): Boolean {
36+
return this.contains(" ")
37+
}
38+
39+
fun String.hasConsecutiveRepetitions(): Boolean {
40+
return Regex("(.)\\1").containsMatchIn(this)
41+
}

core/ui/src/commonMain/kotlin/org/mifospay/core/ui/utils/PasswordChecker.kt

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
package org.mifospay.core.ui.utils
1111

12+
import org.mifospay.core.common.utils.hasConsecutiveRepetitions
13+
import org.mifospay.core.common.utils.hasSpaces
1214
import kotlin.math.log2
1315
import kotlin.math.pow
1416

@@ -19,29 +21,22 @@ object PasswordChecker {
1921
private const val MAX_PASSWORD_LENGTH = 50
2022

2123
fun getPasswordStrengthResult(password: String): PasswordStrengthResult {
22-
when {
23-
password.isEmpty() -> return PasswordStrengthResult.Error("Password cannot be empty.")
24-
password.length > MAX_PASSWORD_LENGTH -> {
25-
return PasswordStrengthResult.Error(
26-
"Password is too long. Maximum length is $MAX_PASSWORD_LENGTH characters.",
27-
)
28-
}
29-
hasSpaceOrConsecutiveRepetitions(password) -> {
30-
return PasswordStrengthResult.Error(
31-
"Password must not contain spaces or repeating characters.",
32-
)
33-
}
24+
val errors = buildList {
25+
if (password.isEmpty()) add("- Password cannot be empty.")
26+
if (password.length > MAX_PASSWORD_LENGTH) add("- Password is too long. Maximum length is $MAX_PASSWORD_LENGTH characters.")
27+
if (password.hasSpaces()) add("- Password must not contain spaces.")
28+
if (password.hasConsecutiveRepetitions()) add("- Password must not contain consecutive repetitive characters.")
29+
}
30+
31+
if (errors.isNotEmpty()) {
32+
return PasswordStrengthResult.Error(errors.joinToString("\n"))
3433
}
3534

3635
val result = getPasswordStrength(password)
3736

3837
return PasswordStrengthResult.Success(result)
3938
}
4039

41-
fun hasSpaceOrConsecutiveRepetitions(password: String): Boolean {
42-
return Regex("(.)\\1").containsMatchIn(password) || password.contains(" ")
43-
}
44-
4540
private fun getPasswordStrength(password: String): PasswordStrength {
4641
val length = password.length
4742
val hasUpperCase = password.any { it.isUpperCase() }
@@ -91,10 +86,10 @@ object PasswordChecker {
9186
if (password.length < STRONG_PASSWORD_LENGTH) {
9287
feedback.add("For a stronger password, use at least $STRONG_PASSWORD_LENGTH characters.")
9388
}
94-
if (Regex("(.)\\1").containsMatchIn(password)) {
89+
if (password.hasConsecutiveRepetitions()) {
9590
feedback.add("Remove consecutive repeating characters.")
9691
}
97-
if (password.contains(" ")) {
92+
if (password.hasSpaces()) {
9893
feedback.add("Remove spaces.")
9994
}
10095

0 commit comments

Comments
 (0)