Files
web/packages/common/polyfills/at.ts
Joakim Jäderberg e24c92918e Merged in fix/BOOK-398-polyfill-at (pull request #2876)
Fix/BOOK-398 polyfill at

* fix(BOOK-398): Add polyfills for .at()

* fix(BOOK-398): Add polyfills for .at()


Approved-by: Linus Flood
2025-09-29 06:58:31 +00:00

27 lines
587 B
TypeScript

if (!Array.prototype.at) {
Array.prototype.at = function (index: number) {
// Convert the index to an integer
index = Math.trunc(index) || 0
if (index < 0) {
index += this.length
}
if (index < 0 || index >= this.length) {
return undefined
}
return this[index]
}
}
if (!String.prototype.at) {
String.prototype.at = function (index: number) {
index = Math.trunc(index) || 0
if (index < 0) {
index += this.length
}
if (index < 0 || index >= this.length) {
return undefined
}
return this.charAt(index)
}
}