diff --git a/package/src/index.ts b/package/src/index.ts index 9c4c8156f..ca51f9ec0 100644 --- a/package/src/index.ts +++ b/package/src/index.ts @@ -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'; diff --git a/package/src/polyfills.ts b/package/src/polyfills.ts new file mode 100644 index 000000000..55f82860b --- /dev/null +++ b/package/src/polyfills.ts @@ -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, + }); + } +})();