array-string-at

Array/string `.at()` ponyfill.

Usage no npm install needed!

<script type="module">
  import arrayStringAt from 'https://cdn.skypack.dev/array-string-at';
</script>

README

array-string-at

Build Status Browser testing by BrowserStack

Array/string .at() ponyfill.

[…] 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

npm install array-string-at --save

Usage

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!

Polyfill

import at from 'array-string-at';

if (typeof String.prototype.at === 'undefined') {
    String.prototype.at = function (index) {
        return at(this, index);
    };
}

if (typeof Array.prototype.at === 'undefined') {
    Array.prototype.at = 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<unknown>|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).

Test

Test suite is taken and modified from following packages:

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

License

MIT © Ivan Nikolić