From 2840c821f394926f40a3d03174defdd116f5dcf3 Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Mon, 4 Nov 2024 11:04:43 +0100 Subject: [PATCH 1/2] fix: add polyfill for Array.prototype.at --- package/polyfills.ts | 28 ++++++++++++++++++++++++++++ package/src/index.ts | 1 + 2 files changed, 29 insertions(+) create mode 100644 package/polyfills.ts diff --git a/package/polyfills.ts b/package/polyfills.ts new file mode 100644 index 0000000000..55f82860bf --- /dev/null +++ b/package/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, + }); + } +})(); diff --git a/package/src/index.ts b/package/src/index.ts index 9c4c8156f8..606f336865 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'; From 8a531e4e4b783c297966ce678e615ee93427061b Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Mon, 4 Nov 2024 11:14:17 +0100 Subject: [PATCH 2/2] fix: ts errors --- package/src/index.ts | 2 +- package/{ => src}/polyfills.ts | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename package/{ => src}/polyfills.ts (100%) diff --git a/package/src/index.ts b/package/src/index.ts index 606f336865..ca51f9ec0f 100644 --- a/package/src/index.ts +++ b/package/src/index.ts @@ -1,6 +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'; +import './polyfills'; export * from './components'; export * from './hooks'; diff --git a/package/polyfills.ts b/package/src/polyfills.ts similarity index 100% rename from package/polyfills.ts rename to package/src/polyfills.ts