mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 15:15:34 +03:00
Add removeDiacritics helper method (#711)
This commit is contained in:
parent
79bc483491
commit
3ec95b0ffd
2 changed files with 20 additions and 0 deletions
|
@ -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),
|
||||
"",
|
||||
)
|
||||
|
|
|
@ -98,4 +98,10 @@ class StringExtensionsTest {
|
|||
result,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `removeDiacritics should remove diacritics from the string`() {
|
||||
val result = "áéíóů".removeDiacritics()
|
||||
assertEquals("aeiou", result)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue