Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add polyfill for Array.prototype.at #2752

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** i18next polyfill to handle intl format for pluralization. For more info see https://www.i18next.com/misc/json-format#i-18-next-json-v4 */
import 'intl-pluralrules';
import './polyfills';

export * from './components';
export * from './hooks';
Expand Down
28 changes: 28 additions & 0 deletions package/src/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(function () {
if (!Array.prototype.at) {
// eslint-disable-next-line no-extend-native
Object.defineProperty(Array.prototype, 'at', {
configurable: true,
enumerable: false,
value: function at(index: number) {
// Convert to integer if index is not provided
const len = this.length;
let relativeIndex = Number(index) || 0;

// Handle negative indices
if (relativeIndex < 0) {
relativeIndex += len;
}

// Return undefined if index is out of bounds
if (relativeIndex < 0 || relativeIndex >= len) {
return undefined;
}

// Return the element at the calculated index
return this[relativeIndex];
},
writable: true,
});
}
})();
Loading