Skip to content

Commit

Permalink
♻️ Refactor EmailAddress.Companion.fromString functions (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Apr 1, 2024
1 parent 63f1797 commit 8c10499
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/commonMain/kotlin/org/kotools/types/EmailAddress.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ public class EmailAddress private constructor() {
* instead of throwing an exception in case of invalid [value].
*/
@JvmStatic
public fun fromString(value: Any): EmailAddress {
val address: EmailAddress? = fromStringOrNull(value)
return requireNotNull(address) { InvalidEmailAddress(value) }
}
public fun fromString(value: Any): EmailAddress =
fromString(value, PATTERN)

/**
* Creates an instance of [EmailAddress] from the string representation
Expand Down Expand Up @@ -136,12 +134,14 @@ public class EmailAddress private constructor() {
require(patternAsString matches defaultRegex) {
InvalidEmailAddressPattern(pattern)
}
val valueAsString: String = value.toString()
val customRegex = Regex(patternAsString)
require(valueAsString matches customRegex) {
return fromString(value, patternAsString)
}

private fun fromString(value: Any, pattern: String): EmailAddress {
val address: EmailAddress? = fromStringOrNull(value, pattern)
return requireNotNull(address) {
InvalidEmailAddress(value, pattern)
}
return EmailAddress()
}

/**
Expand Down

0 comments on commit 8c10499

Please sign in to comment.