ideosorter.github.io
ideosorter.github.io copied to clipboard
Polyfill for Array.prototype.at()
Considering the readme mentions that Array.prototype.at() doesnt work on safari, it would be a good idea to add a polyfill for this method, so that it would work on safari and that part of the readme can be removed
this polyfill should work:
function at(n) {
// ToInteger() abstract op
n = Math.trunc(n) || 0;
// Allow negative indexing from the end
if (n < 0) n += this.length;
// OOB access is guaranteed to return undefined
if (n < 0 || n >= this.length) return undefined;
// Otherwise, this is just normal property access
return this[n];
}
const TypedArray = Reflect.getPrototypeOf(Int8Array);
for (const C of [Array, String, TypedArray]) {
Object.defineProperty(C.prototype, "at",
{ value: at,
writable: true,
enumerable: false,
configurable: true });
}
Based on this: https://github.com/tc39/proposal-relative-indexing-method#polyfill
Safari actually recently added support for the .at() method but if you want to add it for compatibility with older browsers feel free.
In that case i dont think theres need for that, but that part should be removed from the readme then, I shall make a PR for that, no big deal considering its just README i assume
@TheGhostOfInky I updated the readme in https://github.com/Ideosorter/ideosorter.github.io/pull/40
Deternmined unecessary and readme updated