Skip to content

Commit

Permalink
Add TextTransform KTX (#47525)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47525

This API is awkward for nullsafe code.  Adding an extension allows Kotlin code to use `applyTextTransform()` on either nullable or non-null strings:
```
val a: String = string.applyTextTransform(...)
val b: String? = nullableString?.applyTextTransform(...)
```
Changelog:
[Android][Added] TextTransform ktx

Reviewed By: javache

Differential Revision: D65618709

fbshipit-source-id: 28a0fe61a0eaf27aa9677af39d932982f3b694b2
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Nov 9, 2024
1 parent 34c157f commit 7794d7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
4 changes: 4 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -7598,6 +7598,10 @@ public final class com/facebook/react/views/text/TextTransform$Companion {
public final fun apply (Ljava/lang/String;Lcom/facebook/react/views/text/TextTransform;)Ljava/lang/String;
}

public final class com/facebook/react/views/text/TextTransformKt {
public static final fun applyTextTransform (Ljava/lang/String;Lcom/facebook/react/views/text/TextTransform;)Ljava/lang/String;
}

public class com/facebook/react/views/text/frescosupport/FrescoBasedReactTextInlineImageViewManager : com/facebook/react/uimanager/BaseViewManager {
public static final field REACT_CLASS Ljava/lang/String;
public fun <init> ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,28 @@ public enum class TextTransform {

public companion object {
@JvmStatic
public fun apply(text: String?, textTransform: TextTransform?): String? {
if (text == null) {
return null
}
val transformed: String =
when (textTransform) {
UPPERCASE -> text.uppercase(Locale.getDefault())
LOWERCASE -> text.lowercase(Locale.getDefault())
CAPITALIZE -> capitalize(text)
else -> text
}
return transformed
}
public fun apply(text: String?, textTransform: TextTransform?): String? =
text?.applyTextTransform(textTransform)
}
}

private fun capitalize(text: String): String {
public fun String.applyTextTransform(textTransform: TextTransform?): String {
return when (textTransform) {
TextTransform.UPPERCASE -> uppercase(Locale.getDefault())
TextTransform.LOWERCASE -> lowercase(Locale.getDefault())
TextTransform.CAPITALIZE -> {
val wordIterator = BreakIterator.getWordInstance()
wordIterator.setText(text)

val res = StringBuilder(text.length)
wordIterator.setText(this)
val res = StringBuilder(length)
var start = wordIterator.first()
var end = wordIterator.next()
while (end != BreakIterator.DONE) {
res.append(text[start].uppercaseChar())
res.append(text.substring(start + 1, end))
res.append(substring(start, end).replaceFirstChar { it.uppercaseChar() })
start = end
end = wordIterator.next()
}

return res.toString()
res.toString()
}
else -> this
}
}

0 comments on commit 7794d7a

Please sign in to comment.