Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/niksy/array-string-at

Array/string `.at()` ponyfill.
https://github.com/niksy/array-string-at

Last synced: 2 months ago
JSON representation

Array/string `.at()` ponyfill.

Awesome Lists containing this project

README

        

# array-string-at

[![Build Status][ci-img]][ci]
[![Browser testing by BrowserStack][browserstack-img]][browserstack]

[Array/string `.at()`](https://github.com/tc39/proposal-relative-indexing-method)
[ponyfill](https://ponyfill.com).

> […] `at()`, that is on the prototype of the built-in indexable objects:
> `Array`, `String`, and `TypedArrays` objects. The method supports relative
> indexing from the end when passed a negative index.

## Install

```sh
npm install array-string-at --save
```

## Usage

```js
import at from 'array-string-at';

at('becky', 1); // e
at('becky', -2); // k
```

You can **use named export `preferNative` if you wish to use native
implementation if it’s available**. In all other cases, ponyfill will be used.
Beware of
[caveats](https://github.com/sindresorhus/ponyfill#user-content-ponyfill:~:text=Ponyfills%20should%20never%20use%20the%20native,between%20environments%2C%20which%20can%20cause%20bugs.)!

### Polyfill

```js
import at from 'array-string-at';

if (typeof String.prototype.at === 'undefined') {
Object.defineProperty(String.prototype, 'at', {
configurable: true,
writable: true,
value: function (index) {
return at(this, index);
}
});
}

if (typeof Array.prototype.at === 'undefined') {
Object.defineProperty(Array.prototype, 'at', {
configurable: true,
writable: true,
value: function (index) {
return at(this, index);
}
});
}
```

## API

### at(item, index)

Returns: `*`

Returns value at specified index. Supports relative indexing from the end when
passed a negative index.

#### item

Type: `string|Array|TypedArray`

Item to search.

#### index

Type: `number`

Relative index.

## Browser support

Tested in Chrome 72, Edge 15, Firefox 65 and should work in all modern browsers
([support based on Browserslist configuration](https://browserslist.dev/?q=bGFzdCAzIG1ham9yIHZlcnNpb25zLCBzaW5jZSAyMDE5LCBlZGdlID49IDE1LCBub3QgaWUgPiAw)).

## Test

Test suite is taken and modified from following packages:

- [es-shims, string](https://github.com/es-shims/String.prototype.at/blob/main/test/tests.js)
- [es-shims, array](https://github.com/es-shims/Array.prototype.at/blob/main/test/tests.js)

For automated tests, run `npm run test:automated` (append `:watch` for watcher
support).

## License

MIT © [Ivan Nikolić](http://ivannikolic.com)

[ci]: https://travis-ci.com/niksy/array-string-at
[ci-img]: https://travis-ci.com/niksy/array-string-at.svg?branch=master
[browserstack]: https://www.browserstack.com/
[browserstack-img]: https://img.shields.io/badge/browser%20testing-BrowserStack-informational?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NCA2NCI+CiAgPGRlZnMvPgogIDxyYWRpYWxHcmFkaWVudCBpZD0iYSIgY3g9IjIwLjk0Mjk3NiIgY3k9IjI4LjA5NDY3ODczIiByPSIzLjc5MTM0MTQxIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSI+CiAgICA8c3RvcCBvZmZzZXQ9IjAiIHN0b3AtY29sb3I9IiM3OTc5NzkiLz4KICAgIDxzdG9wIG9mZnNldD0iMSIgc3RvcC1jb2xvcj0iIzRjNGM0YyIvPgogIDwvcmFkaWFsR3JhZGllbnQ+CiAgPGcgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTI5LjcyOTIwNCAtNTcuMTg3NjExKSBzY2FsZSgyLjk3MjkyKSI+CiAgICA8Y2lyY2xlIGN4PSIyMC43ODkiIGN5PSIzMC4wMjUiIHI9IjEwLjczOSIgZmlsbD0iI2Y0Yjk2MCIvPgogICAgPGNpcmNsZSBjeD0iMTkuNyIgY3k9IjI4LjkzNiIgcj0iOS43IiBmaWxsPSIjZTY2ZjMyIi8+CiAgICA8Y2lyY2xlIGN4PSIyMS4wMzYiIGN5PSIyNy42OTkiIHI9IjguNDEzIiBmaWxsPSIjZTQzYzQxIi8+CiAgICA8Y2lyY2xlIGN4PSIyMS42NzkiIGN5PSIyOC4zNDIiIHI9IjcuNzIiIGZpbGw9IiNiZGQwNDEiLz4KICAgIDxjaXJjbGUgY3g9IjIxLjEzNS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