Convert com.facebook.react.views.text.TextTransform to Kotlin (#47412)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47412

Changelog: [Internal]

Reviewed By: cortinico, fabriziocucci

Differential Revision: D65478027

fbshipit-source-id: e65019b7a90b696c3764e1deaf94f5f8bb0a7a95
This commit is contained in:
Pieter De Baets
2024-11-06 06:38:29 -08:00
committed by Facebook GitHub Bot
parent 10f6d5adb5
commit 5b41c7a84b
3 changed files with 62 additions and 59 deletions
@@ -7667,15 +7667,21 @@ public class com/facebook/react/views/text/TextLayoutManager {
public final class com/facebook/react/views/text/TextTransform : java/lang/Enum {
public static final field CAPITALIZE Lcom/facebook/react/views/text/TextTransform;
public static final field Companion Lcom/facebook/react/views/text/TextTransform$Companion;
public static final field LOWERCASE Lcom/facebook/react/views/text/TextTransform;
public static final field NONE Lcom/facebook/react/views/text/TextTransform;
public static final field UNSET Lcom/facebook/react/views/text/TextTransform;
public static final field UPPERCASE Lcom/facebook/react/views/text/TextTransform;
public static fun apply (Ljava/lang/String;Lcom/facebook/react/views/text/TextTransform;)Ljava/lang/String;
public static final fun apply (Ljava/lang/String;Lcom/facebook/react/views/text/TextTransform;)Ljava/lang/String;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/facebook/react/views/text/TextTransform;
public static fun values ()[Lcom/facebook/react/views/text/TextTransform;
}
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 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
@@ -1,58 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.text;
import androidx.annotation.Nullable;
import java.text.BreakIterator;
/** Types of text transforms for CustomTextTransformSpan */
public enum TextTransform {
NONE,
UPPERCASE,
LOWERCASE,
CAPITALIZE,
UNSET;
public static String apply(@Nullable String text, TextTransform textTransform) {
if (text == null) {
return null;
}
String transformed;
switch (textTransform) {
case UPPERCASE:
transformed = text.toUpperCase();
break;
case LOWERCASE:
transformed = text.toLowerCase();
break;
case CAPITALIZE:
transformed = capitalize(text);
break;
default:
transformed = text;
}
return transformed;
}
private static String capitalize(String text) {
BreakIterator wordIterator = BreakIterator.getWordInstance();
wordIterator.setText(text);
StringBuilder res = new StringBuilder(text.length());
int start = wordIterator.first();
for (int end = wordIterator.next(); end != BreakIterator.DONE; end = wordIterator.next()) {
res.append(Character.toUpperCase(text.charAt(start)));
res.append(text.substring(start + 1, end));
start = end;
}
return res.toString();
}
};
@@ -0,0 +1,55 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.text
import java.text.BreakIterator
import java.util.Locale
/** Types of text transforms for CustomTextTransformSpan */
public enum class TextTransform {
NONE,
UPPERCASE,
LOWERCASE,
CAPITALIZE,
UNSET;
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
}
private fun capitalize(text: String): String {
val wordIterator = BreakIterator.getWordInstance()
wordIterator.setText(text)
val res = StringBuilder(text.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))
start = end
end = wordIterator.next()
}
return res.toString()
}
}
}