Unverified Commit ffe2d040 authored by Sloane Sturzenegger's avatar Sloane Sturzenegger Committed by GitHub

Polyfill Array.prototype.at (#44436)



fixes issue #44141 , discussion #44148

This is missing and has caused me issues in production. Seems like a great polyfill to have, given that Next already polyfills so many adjacent Array methods.

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Co-authored-by: 's avatarEyas Valdez <37156127+spiltbeans@users.noreply.github.com>
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent 895f104b
......@@ -3,6 +3,7 @@ import { defineRule } from '../utils/define-rule'
// Keep in sync with next.js polyfills file : https://github.com/vercel/next.js/blob/master/packages/next-polyfill-nomodule/src/index.js
const NEXT_POLYFILLED_FEATURES = [
'Array.prototype.@@iterator',
'Array.prototype.at',
'Array.prototype.copyWithin',
'Array.prototype.fill',
'Array.prototype.find',
......
......@@ -118,3 +118,26 @@ if (!Object.fromEntries) {
}, {})
}
}
/**
* Available in:
* Internet Explorer: never
* Edge: 92
* Firefox: 90
* Chrome: 92
* Safari: 15.4
*
* https://caniuse.com/mdn-javascript_builtins_array_at
*/
// Modified from TC39 at proposal polyfill: https://github.com/tc39/proposal-relative-indexing-method#polyfill
if (!Array.prototype.at) {
Array.prototype.at = function at(n) {
let i = Math.trunc(n) || 0
if (i < 0) i += this.length
if (i < 0 || i >= this.length) return undefined
return this[i]
}
}
/* eslint-disable import/no-extraneous-dependencies */
// Keep in sync with eslint no-unwanted-polyfillio rule: https://github.com/vercel/next.js/blob/master/packages/eslint-plugin-next/lib/rules/no-unwanted-polyfillio.js
import 'core-js/features/array/at'
import 'core-js/features/array/copy-within'
import 'core-js/features/array/fill'
import 'core-js/features/array/find'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment