mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Polyfill Array copying methods (#59091)
Summary: Pull Request resolved: https://github.com/react/react-native/pull/59091 Install guarded `Array.prototype.toSorted` and `Array.prototype.toReversed` polyfills during React Native environment setup. Both methods return shallow copies and preserve native implementations when the runtime already provides them. Register the language polyfills from the default environment setup rather than coupling them to generic global-variable setup. Changelog: [General][Added] - Polyfill `Array.prototype.toSorted` and `Array.prototype.toReversed` when the JavaScript runtime does not provide them Reviewed By: mweststrate Differential Revision: D125283374 fbshipit-source-id: 7b195319f6ba316fc73ede4d81c6431cb46ccb23
This commit is contained in:
1 parent
210b1fb304
commit
3ff956a2a2
3 files changed
+89
No files matched your search
+48
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @flow strict-local
|
||||
* @format
|
||||
* @fantom_hermes_variant *
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const originalToSorted = Array.prototype.toSorted;
|
||||
const originalToReversed = Array.prototype.toReversed;
|
||||
|
||||
require('@react-native/fantom/src/setUpDefaultReactNativeEnvironment');
|
||||
|
||||
describe('Array ES2023 method polyfills', () => {
|
||||
it('preserves native methods and fills missing methods', () => {
|
||||
if (originalToSorted == null) {
|
||||
expect(Array.prototype.toSorted).toBeDefined();
|
||||
} else {
|
||||
expect(Array.prototype.toSorted).toBe(originalToSorted);
|
||||
}
|
||||
|
||||
if (originalToReversed == null) {
|
||||
expect(Array.prototype.toReversed).toBeDefined();
|
||||
} else {
|
||||
expect(Array.prototype.toReversed).toBe(originalToReversed);
|
||||
}
|
||||
});
|
||||
|
||||
it('provides non-mutating copying behavior', () => {
|
||||
const input = [3, 1, 2];
|
||||
|
||||
expect(input.toSorted((a, b) => a - b)).toEqual([1, 2, 3]);
|
||||
expect(input.toReversed()).toEqual([2, 1, 3]);
|
||||
expect(input).toEqual([3, 1, 2]);
|
||||
expect(
|
||||
Object.getOwnPropertyDescriptor(Array.prototype, 'toSorted')?.enumerable,
|
||||
).toBe(false);
|
||||
expect(
|
||||
Object.getOwnPropertyDescriptor(Array.prototype, 'toReversed')
|
||||
?.enumerable,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @flow strict
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const arrayPrototype = Array.prototype;
|
||||
|
||||
if (arrayPrototype.toSorted == null) {
|
||||
Object.defineProperty(arrayPrototype, 'toSorted' as string, {
|
||||
configurable: true,
|
||||
value: function <T>(
|
||||
this: Array<T>,
|
||||
compareFn?: (a: T, b: T) => number,
|
||||
): Array<T> {
|
||||
const copy = Array.from(this);
|
||||
copy.sort(compareFn);
|
||||
return copy;
|
||||
},
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
|
||||
if (arrayPrototype.toReversed == null) {
|
||||
Object.defineProperty(arrayPrototype, 'toReversed' as string, {
|
||||
configurable: true,
|
||||
value: function <T>(this: Array<T>): Array<T> {
|
||||
const copy = Array.from(this);
|
||||
copy.reverse();
|
||||
return copy;
|
||||
},
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
@@ -23,6 +23,7 @@ export default function setUpDefaultReactNativeEnvironment(
|
||||
require('./setUpDOM').default();
|
||||
require('./setUpPerformance').default();
|
||||
require('../../../Libraries/Core/polyfillPromise');
|
||||
require('../../../Libraries/Core/polyfillArrayES2023Methods');
|
||||
require('../../../Libraries/Core/setUpTimers');
|
||||
if (__DEV__ && enableDeveloperTools) {
|
||||
require('../../../Libraries/Core/setUpReactDevTools');
|
||||
|
||||
Reference in new issue
Block a user