Skip to content

Commit c5a3046

Browse files
authored
fix: add polyfill for Array.prototype.at (#2752)
* fix: add polyfill for Array.prototype.at * fix: ts errors
1 parent 0a4c852 commit c5a3046

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

package/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** i18next polyfill to handle intl format for pluralization. For more info see https://www.i18next.com/misc/json-format#i-18-next-json-v4 */
22
import 'intl-pluralrules';
3+
import './polyfills';
34

45
export * from './components';
56
export * from './hooks';

package/src/polyfills.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(function () {
2+
if (!Array.prototype.at) {
3+
// eslint-disable-next-line no-extend-native
4+
Object.defineProperty(Array.prototype, 'at', {
5+
configurable: true,
6+
enumerable: false,
7+
value: function at(index: number) {
8+
// Convert to integer if index is not provided
9+
const len = this.length;
10+
let relativeIndex = Number(index) || 0;
11+
12+
// Handle negative indices
13+
if (relativeIndex < 0) {
14+
relativeIndex += len;
15+
}
16+
17+
// Return undefined if index is out of bounds
18+
if (relativeIndex < 0 || relativeIndex >= len) {
19+
return undefined;
20+
}
21+
22+
// Return the element at the calculated index
23+
return this[relativeIndex];
24+
},
25+
writable: true,
26+
});
27+
}
28+
})();

0 commit comments

Comments
 (0)