[PM-28157] Add string extension to prefix URIs with www (#6183)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Patrick Honkonen
2025-11-20 10:32:54 -05:00
committed by GitHub
parent 7918abdccf
commit 7b491d3c3c
4 changed files with 155 additions and 1 deletions

View File

@@ -239,6 +239,34 @@ fun String.prefixHttpsIfNecessaryOrNull(): String? =
fun String.prefixHttpsIfNecessary(): String =
prefixHttpsIfNecessaryOrNull() ?: this
/**
* If the given [String] is a valid URI, "www." will be prepended (or inserted after the scheme
* if present) if it is not already present. Otherwise `null` will be returned.
*
* Examples:
* - "example.com" -> "www.example.com"
* - "www.example.com" -> "www.example.com"
* - "https://example.com" -> "https://www.example.com"
* - "https://www.example.com" -> "https://www.example.com"
*/
fun String.prefixWwwIfNecessaryOrNull(): String? =
when {
this.isBlank() || !this.isValidUri() -> null
this.startsWith("www.") -> this
this.startsWith("http://") || this.startsWith("https://") -> {
if ("://www." in this) this else this.replaceFirst("://", "://www.")
}
else -> "www.$this"
}
/**
* If the given [String] is a valid URI, "www." will be prepended (or inserted after the scheme
* if present) if it is not already present. Otherwise the original [String] will be returned.
*/
fun String.prefixWwwIfNecessary(): String =
prefixWwwIfNecessaryOrNull() ?: this
/**
* Checks if a string is using base32 digits.
*/

View File

@@ -281,4 +281,65 @@ class StringExtensionsTest {
fun `orZeroWidthSpace returns the original value for a non-blank string`() {
assertEquals("test", "test".orZeroWidthSpace())
}
@Suppress("MaxLineLength")
@Test
fun `prefixWwwIfNecessaryOrNull should prefix www when URI is valid and no scheme and no www`() {
val uri = "example.com"
val expected = "www.$uri"
val actual = uri.prefixWwwIfNecessaryOrNull()
assertEquals(expected, actual)
}
@Test
fun `prefixWwwIfNecessaryOrNull should return URI unchanged when starts with www`() {
val uri = "www.example.com"
val actual = uri.prefixWwwIfNecessaryOrNull()
assertEquals(uri, actual)
}
@Test
fun `prefixWwwIfNecessaryOrNull should prefix www when scheme is http and no www`() {
val uri = "http://example.com"
val expected = "http://www.example.com"
val actual = uri.prefixWwwIfNecessaryOrNull()
assertEquals(expected, actual)
}
@Test
fun `prefixWwwIfNecessaryOrNull should prefix www when scheme is https and no www`() {
val uri = "https://example.com"
val expected = "https://www.example.com"
val actual = uri.prefixWwwIfNecessaryOrNull()
assertEquals(expected, actual)
}
@Suppress("MaxLineLength")
@Test
fun `prefixWwwIfNecessaryOrNull should return URI unchanged when scheme is http and www is present`() {
val uri = "http://www.example.com"
val actual = uri.prefixWwwIfNecessaryOrNull()
assertEquals(uri, actual)
}
@Suppress("MaxLineLength")
@Test
fun `prefixWwwIfNecessaryOrNull should return URI unchanged when scheme is https and www is present`() {
val uri = "https://www.example.com"
val actual = uri.prefixWwwIfNecessaryOrNull()
assertEquals(uri, actual)
}
@Test
fun `prefixWwwIfNecessaryOrNull should return null when URI is empty string`() {
val uri = ""
assertNull(uri.prefixWwwIfNecessaryOrNull())
}
@Test
fun `prefixWwwIfNecessaryOrNull should return null when URI is invalid`() {
val invalidUri = "invalid uri"
assertNull(invalidUri.prefixWwwIfNecessaryOrNull())
}
}