Skip to content

Commit ee32dfc

Browse files
OskarEichlermeta-codesync[bot]
authored andcommitted
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 #58244. ## Changelog: [GENERAL] [FIXED] - Sort IntersectionObserver threshold arrays numerically. Pull Request resolved: #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
1 parent 790289c commit ee32dfc

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function normalizeThreshold(
323323
return threshold
324324
.map(t => normalizeThresholdValue(t, 'threshold'))
325325
.map(t => t ?? 0)
326-
.sort();
326+
.sort((a, b) => a - b);
327327
} else if (defaultEmpty) {
328328
return [];
329329
} else {
@@ -359,7 +359,7 @@ function normalizeRootThreshold(
359359
const normalizedArr = rootThreshold
360360
.map(rt => normalizeThresholdValue(rt, 'rnRootThreshold'))
361361
.filter((rt): rt is number => rt != null)
362-
.sort();
362+
.sort((a, b) => a - b);
363363
return normalizedArr.length === 0 ? null : normalizedArr;
364364
}
365365

packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ describe('IntersectionObserver', () => {
321321
expect(
322322
new IntersectionObserver(() => {}, {threshold: [0.5, 0, 1]}).thresholds,
323323
).toEqual([0, 0.5, 1]);
324+
expect(
325+
new IntersectionObserver(() => {}, {threshold: [0.000001, 1e-7]})
326+
.thresholds,
327+
).toEqual([1e-7, 0.000001]);
324328

325329
// Does NOT deduplicate (browsers don't do it - shrug)
326330
expect(
@@ -427,6 +431,11 @@ describe('IntersectionObserver', () => {
427431
new IntersectionObserver(() => {}, {rnRootThreshold: [0.5, 0, 1]})
428432
.rnRootThresholds,
429433
).toEqual([0, 0.5, 1]);
434+
expect(
435+
new IntersectionObserver(() => {}, {
436+
rnRootThreshold: [0.000001, 1e-7],
437+
}).rnRootThresholds,
438+
).toEqual([1e-7, 0.000001]);
430439

431440
// Does NOT deduplicate (browsers don't do it - shrug)
432441
expect(

0 commit comments

Comments
 (0)