Skip to content

Commit 2c3ac99

Browse files
committed
Fix displaying -0 in lists and variables
1 parent f2fc199 commit 2c3ac99

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/lib/monitor-adapter.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
2828
label = `${spriteName}: ${label}`;
2929
}
3030

31-
// If value is a number, round it to six decimal places
32-
if (typeof value === 'number') {
33-
value = Number(value.toFixed(6));
31+
// If value is a normal finite number, round it to six decimal places.
32+
if (typeof value === 'number' && Number.isFinite(value) && !Object.is(value, -0)) {
33+
value = Math.round(value * 1e6) / 1e6;
3434
}
3535

36-
// Anything that isn't a string or number, such as a boolean or object, should be converted to string.
37-
// For lists, we do this when we display the list row instead of doing a full list copy on every change.
38-
if (!Array.isArray(value) && (typeof value !== 'string' || typeof value !== 'number')) {
36+
// Convert to a string now. That should help avoid unnecessary re-renders in a few edge cases.
37+
// For lists, we stringify when we display the list row instead of doing a full list copy on every change.
38+
if (!Array.isArray(value)) {
3939
value = safeStringify(value);
4040
}
4141

src/lib/tw-safe-stringify.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ const circularReplacer = () => {
1212
};
1313

1414
/**
15-
* Safely stringify, properly handling circular relations.
15+
* Safely stringify, properly handling circular relations and -0.
1616
* @param {unknown} input Any value
1717
* @returns {string} A stringified version of the input.
1818
*/
1919
export const safeStringify = input => {
2020
if (typeof input === 'object' && input !== null) {
21+
// TODO: this will not handle -0 inside the input properly, though that is very low priority.
2122
return JSON.stringify(input, circularReplacer());
2223
}
24+
// -0 stringifies as "0" by default.
25+
if (Object.is(input, -0)) {
26+
return '-0';
27+
}
2328
return `${input}`;
2429
};

0 commit comments

Comments
 (0)