|
1 |
| -# TypedArray.prototype.at |
2 |
| -An ES5 spec-compliant `TypedArray.prototype.at` shim/polyfill/replacement that works as far down as ES3 |
| 1 | +# typedarray.prototype.at <sup>[![Version Badge][npm-version-svg]][package-url]</sup> |
| 2 | + |
| 3 | +[![github actions][actions-image]][actions-url] |
| 4 | +[![coverage][codecov-image]][codecov-url] |
| 5 | +[![License][license-image]][license-url] |
| 6 | +[![Downloads][downloads-image]][downloads-url] |
| 7 | + |
| 8 | +[![npm badge][npm-badge-png]][package-url] |
| 9 | + |
| 10 | +An ES spec-compliant `%TypedArray%.prototype.at` shim/polyfill/replacement that works as far down as ES3. |
| 11 | + |
| 12 | +This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec](https://tc39.es/ecma262/#sec-%typedarray%.prototype.at). |
| 13 | + |
| 14 | +Because `%TypedArray%.prototype.at` depends on a receiver (the “this” value), the main export takes the typed array to operate on as the first argument. |
| 15 | + |
| 16 | +## Example |
| 17 | + |
| 18 | +```js |
| 19 | +var at = require('typedarray.prototype.at'); |
| 20 | +var assert = require('assert'); |
| 21 | + |
| 22 | +var ta = new Uint8Array([1, 2, 3]); |
| 23 | + |
| 24 | +assert.equal(at(ta, 0), 1); |
| 25 | +assert.equal(at(ta, 1), 2); |
| 26 | +assert.equal(at(ta, 2), 3); |
| 27 | +assert.equal(at(ta, -1), 3); |
| 28 | +assert.equal(at(ta, -2), 2); |
| 29 | +assert.equal(at(ta, -3), 1); |
| 30 | +``` |
| 31 | + |
| 32 | +```js |
| 33 | +var shim = require('typedarray.prototype.at/shim'); |
| 34 | +var getPolyfill = require('typedarray.prototype.at/polyfill'); |
| 35 | +var assert = require('assert'); |
| 36 | +/* when TypedArray#at is not present */ |
| 37 | +delete Object.getPrototypeOf(Uint8Array.prototype).at; |
| 38 | +var shimmed = shim(); |
| 39 | +assert.equal(shimmed, getPolyfill()); |
| 40 | +assert.equal(shimmed, Uint8Array.prototype.at); |
| 41 | + |
| 42 | +var ta = new Uint8Array([1, 2, 3]); |
| 43 | +assert.equal(ta.at(-1), at(ta, -1)); |
| 44 | +``` |
| 45 | + |
| 46 | +```js |
| 47 | +var shim = require('typedarray.prototype.at/shim'); |
| 48 | +var assert = require('assert'); |
| 49 | +/* when TypedArray#at is present */ |
| 50 | +var shimmed = shim(); |
| 51 | +assert.equal(shimmed, Uint8Array.prototype.at); |
| 52 | + |
| 53 | +var ta = new Uint8Array([1, 2, 3]); |
| 54 | +assert.equal(ta.at(-1), at(ta, -1)); |
| 55 | +``` |
| 56 | + |
| 57 | +## Tests |
| 58 | +Simply clone the repo, `npm install`, and run `npm test` |
| 59 | + |
| 60 | +[package-url]: https://npmjs.org/package/typedarray.prototype.at |
| 61 | +[npm-version-svg]: https://versionbadg.es/es-shims/TypedArray.prototype.at.svg |
| 62 | +[deps-svg]: https://david-dm.org/es-shims/TypedArray.prototype.at.svg |
| 63 | +[deps-url]: https://david-dm.org/es-shims/TypedArray.prototype.at |
| 64 | +[dev-deps-svg]: https://david-dm.org/es-shims/TypedArray.prototype.at/dev-status.svg |
| 65 | +[dev-deps-url]: https://david-dm.org/es-shims/TypedArray.prototype.at#info=devDependencies |
| 66 | +[npm-badge-png]: https://nodei.co/npm/typedarray.prototype.at.png?downloads=true&stars=true |
| 67 | +[license-image]: https://img.shields.io/npm/l/typedarray.prototype.at.svg |
| 68 | +[license-url]: LICENSE |
| 69 | +[downloads-image]: https://img.shields.io/npm/dm/typedarray.prototype.at.svg |
| 70 | +[downloads-url]: https://npm-stat.com/charts.html?package=typedarray.prototype.at |
| 71 | +[codecov-image]: https://codecov.io/gh/es-shims/TypedArray.prototype.at/branch/main/graphs/badge.svg |
| 72 | +[codecov-url]: https://app.codecov.io/gh/es-shims/TypedArray.prototype.at/ |
| 73 | +[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/TypedArray.prototype.at |
| 74 | +[actions-url]: https://github.com/es-shims/TypedArray.prototype.at/actions |
0 commit comments