Skip to content

Commit

Permalink
Polyfill Array.prototype.at (#44436)
Browse files Browse the repository at this point in the history
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: Eyas Valdez <[email protected]>
Co-authored-by: JJ Kasper <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2023
1 parent 895f104 commit ffe2d04
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
23 changes: 23 additions & 0 deletions packages/next-polyfill-module/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
1 change: 1 addition & 0 deletions packages/next-polyfill-nomodule/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* 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'
Expand Down

0 comments on commit ffe2d04

Please sign in to comment.