Skip to content

Commit 81458f2

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Prettify NaN/Infinity in DebugStringConvertible
Summary: Improve readability of debug output by rendering floating-point NaN and Infinity as "NaN", "Infinity", and "-Infinity" instead of the lowercase stream defaults. This aligns React Native debug strings with developer expectations and JS conventions, making logs and snapshots clearer. Changelog: [Internal] --- AI generated Summary & Test Plan from DEV97500299 Differential Revision: D95499647
1 parent 42db21a commit 81458f2

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

packages/react-native/Libraries/Components/Pressable/__tests__/Pressable-itest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ describe('<Pressable>', () => {
148148
<rn-paragraph
149149
allowFontScaling="true"
150150
ellipsizeMode="tail"
151-
fontSize="nan"
152-
fontSizeMultiplier="nan"
151+
fontSize="NaN"
152+
fontSizeMultiplier="NaN"
153153
foregroundColor="rgba(0, 0, 0, 0)"
154154
overflow="hidden">
155155
the quick brown fox

packages/react-native/Libraries/Text/__tests__/Text-itest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ describe('<Text>', () => {
4040
<rn-paragraph
4141
allowFontScaling="true"
4242
ellipsizeMode="tail"
43-
fontSize="nan"
44-
fontSizeMultiplier="nan"
43+
fontSize="NaN"
44+
fontSizeMultiplier="NaN"
4545
foregroundColor="rgba(0, 0, 0, 0)"
4646
overflow="hidden">
4747
{TEST_TEXT}

packages/react-native/ReactCommon/react/renderer/debug/DebugStringConvertible.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "DebugStringConvertible.h"
99

10+
#include <cmath>
1011
#include <cstdint>
1112
#include <iomanip>
1213
#include <sstream>
@@ -127,6 +128,14 @@ SharedDebugStringConvertibleList DebugStringConvertible::getDebugProps() const {
127128
* `toString`-family implementation.
128129
*/
129130
std::string toString(const double& value) {
131+
// Handle special floating-point values with prettier output
132+
if (std::isnan(value)) {
133+
return "NaN";
134+
}
135+
if (std::isinf(value)) {
136+
return value < 0 ? "-Infinity" : "Infinity";
137+
}
138+
130139
std::ostringstream stream;
131140
stream << std::fixed << std::setprecision(4) << value;
132141
std::string result = stream.str();

0 commit comments

Comments
 (0)