kotools / types

Explicit types for Kotlin Multiplatform.
https://types.kotools.org
MIT License
90 stars 6 forks source link

✨ New factory functions for `EmailAddress` accepting a custom regular expression #601

Closed LVMVRQUXL closed 8 months ago

LVMVRQUXL commented 8 months ago

📝 Description

Like documented in https://github.com/kotools/types/issues/586#issuecomment-2015314223, we would like to introduce the following experimental factory functions in the EmailAddress type for accepting a custom regular expression:

fun EmailAddress.Companion.create(text: String, regex: Regex): EmailAddress
fun EmailAddress.Companion.createOrNull(text: String, regex: Regex): EmailAddress?

The specified regex should match the pattern used in the default regular expression.

val result: Boolean = "^[a-z]+@[a-z]+\\.[a-z]+$" matches Regex("^\\S+@\\S+\\.\\S+$")
println(result) // true

Here are some examples of calling these functions from Kotlin code:

EmailAddress.create("contact@kotools.org") // passes using the default regular expression
EmailAddress.create(" @kotools.org") // fails using the default regular expression

val regex = Regex("^[a-z]+@[a-z]+\\.[a-z]+$")
EmailAddress.create("contact@kotools.org", regex) // passes using the specified regular expression
EmailAddress.create(" @kotools.org") // fails using the specified regular expression

The Regex type being unavailable on Java, these functions shouldn't be available on that platform.

✅ Checklist

LVMVRQUXL commented 8 months ago

This issue is not ready to implement and needs more design thinking.