Fix/BOOK-398 polyfill at * fix(BOOK-398): Add polyfills for .at() * fix(BOOK-398): Add polyfills for .at() Approved-by: Linus Flood
27 lines
587 B
TypeScript
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)
|
|
}
|
|
}
|