polyfill.ts 790 B

1234567891011121314151617181920212223242526
  1. const polyfill = () => {
  2. if (!Array.prototype.at) {
  3. // Taken from https://github.com/tc39/proposal-relative-indexing-method#polyfill so that it works in tests
  4. /* eslint-disable */
  5. Object.defineProperty(Array.prototype, "at", {
  6. value: function (n: number) {
  7. // ToInteger() abstract op
  8. n = Math.trunc(n) || 0;
  9. // Allow negative indexing from the end
  10. if (n < 0) {
  11. n += this.length;
  12. }
  13. // OOB access is guaranteed to return undefined
  14. if (n < 0 || n >= this.length) {
  15. return undefined;
  16. }
  17. // Otherwise, this is just normal property access
  18. return this[n];
  19. },
  20. writable: true,
  21. enumerable: false,
  22. configurable: true,
  23. });
  24. }
  25. };
  26. export default polyfill;