ideosorter.github.io icon indicating copy to clipboard operation
ideosorter.github.io copied to clipboard

Polyfill for Array.prototype.at()

Open justlucdewit opened this issue 2 years ago • 3 comments

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

justlucdewit avatar Oct 03 '22 12:10 justlucdewit

Safari actually recently added support for the .at() method but if you want to add it for compatibility with older browsers feel free.

TheGhostOfInky avatar Oct 03 '22 14:10 TheGhostOfInky

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

justlucdewit avatar Oct 04 '22 10:10 justlucdewit

@TheGhostOfInky I updated the readme in https://github.com/Ideosorter/ideosorter.github.io/pull/40

justlucdewit avatar Oct 04 '22 10:10 justlucdewit

Deternmined unecessary and readme updated

TheGhostOfInky avatar Jan 10 '23 13:01 TheGhostOfInky