Add removeDiacritics helper method (#711)

This commit is contained in:
David Perez 2024-01-22 14:21:07 -06:00 committed by Álison Fernandes
parent 79bc483491
commit 3ec95b0ffd
2 changed files with 20 additions and 0 deletions

View file

@ -12,6 +12,7 @@ import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.core.graphics.toColorInt
import java.net.URI
import java.text.Normalizer
import java.util.Locale
import kotlin.math.floor
@ -163,3 +164,16 @@ fun String.toHexColorRepresentation(): String {
*/
fun String.capitalize(locale: Locale = Locale.getDefault()): String =
replaceFirstChar { if (it.isLowerCase()) it.titlecase(locale) else it.toString() }
/**
* Normalizes the [String] by removing diacritics, such as an umlaut.
*
* Example: áéíóů --> aeiou
*/
fun String.removeDiacritics(): String =
"\\p{InCombiningDiacriticalMarks}+"
.toRegex()
.replace(
Normalizer.normalize(this, Normalizer.Form.NFKD),
"",
)

View file

@ -98,4 +98,10 @@ class StringExtensionsTest {
result,
)
}
@Test
fun `removeDiacritics should remove diacritics from the string`() {
val result = "áéíóů".removeDiacritics()
assertEquals("aeiou", result)
}
}