array-string-at
v1.1.0
Published
Array/string `.at()` ponyfill.
Downloads
3,664
Maintainers
Readme
array-string-at
[…]
at()
, that is on the prototype of the built-in indexable objects:Array
,String
, andTypedArrays
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ć