Skip to content

Commit c4ff885

Browse files
trevoradecopybara-github
authored andcommitted
Add Array.prototype.toSorted polyfill
PiperOrigin-RevId: 810668573
1 parent 32f95f2 commit c4ff885

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

externs/es6.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,19 @@ Array.prototype.flat = function(depth) {};
17861786
*/
17871787
Array.prototype.toReversed = function() {};
17881788

1789+
/**
1790+
* NOTE: this is an ES2023 extern.
1791+
* @override
1792+
* @param {function(T, T): number=} compareFn A function that defines the sort
1793+
* order.
1794+
* @return {!Array<T>}
1795+
* @this {!IArrayLike<T>}
1796+
* @template VALUE
1797+
* @nosideeffects
1798+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted
1799+
*/
1800+
Array.prototype.toSorted = function(compareFn) {};
1801+
17891802
/**
17901803
* NOTE: this is an ES2023 extern.
17911804
* @override
@@ -1932,6 +1945,18 @@ ReadonlyArray.prototype.flat = function(depth) {};
19321945
*/
19331946
ReadonlyArray.prototype.toReversed = function() {};
19341947

1948+
/**
1949+
* NOTE: this is an ES2023 extern.
1950+
* @param {function(T, T): number=} compareFn A function that defines the sort
1951+
* order.
1952+
* @return {!Array<T>}
1953+
* @this {!IArrayLike<T>}
1954+
* @template T
1955+
* @nosideeffects
1956+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted
1957+
*/
1958+
ReadonlyArray.prototype.toSorted = function(compareFn) {};
1959+
19351960
/**
19361961
* NOTE: this is an ES2023 extern.
19371962
* @param {number} start

src/com/google/javascript/jscomp/js/es6/array.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
'require es6/array/keys';
3434
'require es6/array/of';
3535
'require es6/array/toreversed';
36+
'require es6/array/tosorted';
3637
'require es6/array/tospliced';
3738
'require es6/array/values';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2025 The Closure Compiler Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'require es6/array/from';
18+
'require util/polyfill';
19+
20+
$jscomp.polyfill('Array.prototype.toSorted', function(orig) {
21+
if (orig) return orig;
22+
23+
/**
24+
* Returns a new array with all elements sorted.
25+
*
26+
* Note: This polyfill purposefully is NOT spec compliant in order to minimize
27+
* code size. See skipped unit tests for specific gaps.
28+
*
29+
* @see https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.tosorted
30+
*
31+
* @param {function(T, T): number=} compareFn A function that defines the sort
32+
* order.
33+
* @return {!Array<T>}
34+
* @this {!IArrayLike<T>}
35+
* @template T
36+
*/
37+
var polyfill = function(compareFn) {
38+
return Array.from(this).sort(compareFn);
39+
};
40+
41+
return polyfill;
42+
}, 'es_next', 'es3');

0 commit comments

Comments
 (0)