From ee32dfc8b928565c7aef2083dda718140ad619ae Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Tue, 1 Sep 2026 09:14:19 -0700 Subject: [PATCH] Sort IntersectionObserver thresholds numerically (#58245) Summary: Both standard `threshold` and React Native `rnRootThreshold` arrays currently use default lexicographic sorting. Add numeric comparators so valid small values whose string forms use exponent notation are exposed in ascending numeric order, and cover both normalization paths. Fixes https://github.com/react/react-native/issues/58244. ## Changelog: [GENERAL] [FIXED] - Sort IntersectionObserver threshold arrays numerically. Pull Request resolved: https://github.com/react/react-native/pull/58245 Test Plan: - Exact upstream focused run: 108 existing tests passed and both new threshold assertions failed. - Fixed focused Fantom run: 110/110 passed across two suites. - Fresh Flow check: 0 errors. - Targeted no-ignore ESLint, Prettier, and `git diff --check` passed. Only incorrectly ordered threshold arrays change. Reviewed By: cortinico Differential Revision: D118266760 Pulled By: javache fbshipit-source-id: 24c8daf4fec16a41d91e583a9c49623bb68025eb --- .../webapis/intersectionobserver/IntersectionObserver.js | 4 ++-- .../__tests__/IntersectionObserver-itest.js | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js index 2d723e8cd7a..f75e7c0375a 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js @@ -323,7 +323,7 @@ function normalizeThreshold( return threshold .map(t => normalizeThresholdValue(t, 'threshold')) .map(t => t ?? 0) - .sort(); + .sort((a, b) => a - b); } else if (defaultEmpty) { return []; } else { @@ -359,7 +359,7 @@ function normalizeRootThreshold( const normalizedArr = rootThreshold .map(rt => normalizeThresholdValue(rt, 'rnRootThreshold')) .filter((rt): rt is number => rt != null) - .sort(); + .sort((a, b) => a - b); return normalizedArr.length === 0 ? null : normalizedArr; } diff --git a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js index ac6eda8c618..db5ad0a407b 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js @@ -321,6 +321,10 @@ describe('IntersectionObserver', () => { expect( new IntersectionObserver(() => {}, {threshold: [0.5, 0, 1]}).thresholds, ).toEqual([0, 0.5, 1]); + expect( + new IntersectionObserver(() => {}, {threshold: [0.000001, 1e-7]}) + .thresholds, + ).toEqual([1e-7, 0.000001]); // Does NOT deduplicate (browsers don't do it - shrug) expect( @@ -427,6 +431,11 @@ describe('IntersectionObserver', () => { new IntersectionObserver(() => {}, {rnRootThreshold: [0.5, 0, 1]}) .rnRootThresholds, ).toEqual([0, 0.5, 1]); + expect( + new IntersectionObserver(() => {}, { + rnRootThreshold: [0.000001, 1e-7], + }).rnRootThresholds, + ).toEqual([1e-7, 0.000001]); // Does NOT deduplicate (browsers don't do it - shrug) expect(