Skip to content

Commit ffa9161

Browse files
trevoradecopybara-github
authored andcommitted
Add TypedArray.prototype.toReversed polyfill
PiperOrigin-RevId: 810668960
1 parent b229a92 commit ffa9161

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

externs/es6.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,16 @@ TypedArray.prototype.subarray = function(begin, opt_end) {};
709709
*/
710710
TypedArray.prototype.values = function() {};
711711

712+
/**
713+
* NOTE: this is an ES2023 extern.
714+
* @return {THIS}
715+
* @this {THIS}
716+
* @template THIS
717+
* @nosideeffects
718+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/toReversed
719+
*/
720+
TypedArray.prototype.toReversed = function() {};
721+
712722
/**
713723
* @return {string}
714724
* @nosideeffects

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
'require es6/typedarray/fill';
2424
'require es6/typedarray/findlast';
2525
'require es6/typedarray/findlastindex';
26+
'require es6/typedarray/toreversed';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 util/polyfill';
18+
19+
/**
20+
* @param {*} orig
21+
* @return {*}
22+
*/
23+
$jscomp.typedArrayToReversed = function(orig) {
24+
if (orig) return orig;
25+
26+
/**
27+
* Returns a new typed array with all elements reversed.
28+
*
29+
* Note: This polyfill purposefully is NOT spec compliant in order to minimize
30+
* code size. See skipped unit tests for specific gaps.
31+
*
32+
* @see https://tc39.es/ecma262/multipage/indexed-collections.html#sec-%typedarray%.prototype.toreversed
33+
*
34+
* @return {THIS}
35+
* @this {THIS}
36+
* @template THIS
37+
*/
38+
var polyfill = function() {
39+
return this.slice().reverse();
40+
};
41+
42+
return polyfill;
43+
};
44+
45+
$jscomp.polyfillTypedArrayMethod(
46+
'toReversed', $jscomp.typedArrayToReversed, 'es_next', 'es5');

0 commit comments

Comments
 (0)