Internalize and migrate DimensionPropConverter to Kotlin (#49676)

Summary:
As part of the initiative to reduce the public API surface, this class can be internalized. I've checked there are [no relevant OSS usages](https://github.com/search?type=code&q=NOT+is%3Afork+NOT+org%3Afacebook+NOT+repo%3Areact-native-tvos%2Freact-native-tvos+NOT+repo%3Anuagoz%2Freact-native+NOT+repo%3A2lambda123%2Freact-native+NOT+repo%3Abeanchips%2Ffacebookreactnative+NOT+repo%3AfabOnReact%2Freact-native-notes+NOT+user%3Ahuntie+NOT+user%3Acortinico+NOT+repo%3AMaxdev18%2Fpowersync_app+NOT+repo%3Acarter-0%2Finstagram-decompiled+NOT+repo%3Am0mosenpai%2Finstadamn+NOT+repo%3AA-Star100%2FA-Star100-AUG2-2024+NOT+repo%3Alclnrd%2Fdetox-scrollview-reproductible+NOT+repo%3ADionisisChytiris%2FWorldWiseTrivia_Main+NOT+repo%3Apast3l%2Fhi2+NOT+repo%3AoneDotpy%2FCaribouQuest+NOT+repo%3Abejayoharen%2Fdailytodo+NOT+repo%3Amolangning%2Freversing-discord+NOT+repo%3AScottPrzy%2Freact-native+NOT+repo%3Agabrieldonadel%2Freact-native-visionos+NOT+repo%3AGabriel2308%2FTestes-Soft+NOT+repo%3Adawnzs03%2FflakyBuild+NOT+repo%3Acga2351%2Fcode+NOT+repo%3Astreeg%2Ftcc+NOT+repo%3Asoftware-mansion-labs%2Freact-native-swiftui+NOT+repo%3Apkcsecurity%2Fdecompiled-lightbulb+com.facebook.react.bridge.DimensionPropConverter).

In this PR I'm also converting this class to Kotlin as it's needed to make it internal as per https://github.com/facebook/react-native/pull/49676#issuecomment-2683579829.

## Changelog:

[INTERNAL] - Internalize and migrate com.facebook.react.bridge.DimensionPropConverter to Kotlin

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

Test Plan:
```bash
yarn test-android
yarn android
```

Reviewed By: NickGerleman

Differential Revision: D70208912

Pulled By: arushikesarwani94

fbshipit-source-id: c74326ca1d82417457c2a438fa28e20f0a0eaa82
This commit is contained in:
Mateo Guzmán
2025-02-26 17:48:07 -08:00
committed by Facebook GitHub Bot
parent 798f2e47aa
commit 8a4a6231b7
4 changed files with 72 additions and 38 deletions
@@ -700,11 +700,6 @@ public class com/facebook/react/bridge/DefaultJSExceptionHandler : com/facebook/
public fun handleException (Ljava/lang/Exception;)V
}
public class com/facebook/react/bridge/DimensionPropConverter {
public fun <init> ()V
public static fun getDimension (Ljava/lang/Object;)Lcom/facebook/yoga/YogaValue;
}
public abstract interface class com/facebook/react/bridge/Dynamic {
public abstract fun asArray ()Lcom/facebook/react/bridge/ReadableArray;
public abstract fun asBoolean ()Z
@@ -1,33 +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.bridge;
import androidx.annotation.Nullable;
import com.facebook.yoga.YogaUnit;
import com.facebook.yoga.YogaValue;
public class DimensionPropConverter {
@Nullable
public static YogaValue getDimension(@Nullable Object value) {
if (value == null) {
return null;
}
if (value instanceof Double) {
return new YogaValue(((Double) value).floatValue(), YogaUnit.POINT);
}
if (value instanceof String) {
return YogaValue.parse((String) value);
}
throw new JSApplicationCausedNativeException(
"DimensionValue: the value must be a number or string.");
}
}
@@ -0,0 +1,27 @@
/*
* 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.bridge
import com.facebook.yoga.YogaUnit
import com.facebook.yoga.YogaValue
internal class DimensionPropConverter {
companion object {
@JvmStatic
fun getDimension(value: Any?): YogaValue? {
return when (value) {
null -> null
is Double -> YogaValue(value.toFloat(), YogaUnit.POINT)
is String -> YogaValue.parse(value)
else ->
throw JSApplicationCausedNativeException(
"DimensionValue: the value must be a number or string.")
}
}
}
}
@@ -0,0 +1,45 @@
/*
* 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.bridge
import com.facebook.yoga.YogaUnit
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
/** Tests for [DimensionPropConverter] */
class DimensionPropConverterTest {
@Test
fun doubleReturnsYogaValue() {
val result = DimensionPropConverter.getDimension(10.5)
assertThat(result).isNotNull
assertThat(result?.value).isEqualTo(10.5f)
assertThat(result?.unit).isEqualTo(YogaUnit.POINT)
}
@Test
fun stringReturnsParsedYogaValue() {
val result = DimensionPropConverter.getDimension("100%")
assertThat(result).isNotNull
assertThat(result?.unit).isEqualTo(YogaUnit.PERCENT)
}
@Test
fun nullReturnsNull() {
val result = DimensionPropConverter.getDimension(null)
assertThat(result).isNull()
}
@Test(expected = JSApplicationCausedNativeException::class)
fun invalidTypeThrowsException() {
DimensionPropConverter.getDimension(listOf(1, 2, 3))
}
}