Skip to content

Commit

Permalink
Add support for TypedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
niksy committed Sep 13, 2021
1 parent 0baa1b8 commit 46fa19d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/* eslint-disable no-undefined */

import { loose as isTypedArray } from 'is-typedarray';

/**
* @param {string|Array} item
* @param {number} _index
* @param {string|Array} item
* @param {number} _index
*
* @returns {*}
* @throws {TypeError}
*/
export default function ponyfill(item, _index) {
if (typeof item !== 'string' && !Array.isArray(item)) {
if (
typeof item !== 'string' &&
!Array.isArray(item) &&
!isTypedArray(item)
) {
throw new TypeError('Expected a string or array-like item.');
}

Expand All @@ -26,7 +32,8 @@ export const preferNative = (item, index) => {
if (
(typeof item === 'string' &&
typeof String.prototype.at !== 'undefined') ||
(Array.isArray(item) && Array.prototype.at !== 'undefined')
(Array.isArray(item) && Array.prototype.at !== 'undefined') ||
(isTypedArray(item) && item.at !== 'undefined')
) {
return item.at(index);
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"test:automated:watch": "npm run test:automated -- --auto-watch --no-single-run",
"version": "if [ $(git rev-parse --abbrev-ref HEAD) == 'master' ]; then version-changelog CHANGELOG.md && changelog-verify CHANGELOG.md && git add CHANGELOG.md; else echo; fi"
},
"dependencies": {
"is-typedarray": "^1.0.0"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
Expand Down
6 changes: 5 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const path = require('path');
const { promises: fs } = require('fs');
const { default: babel } = require('@rollup/plugin-babel');
const { default: resolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');

module.exports = {
input: 'index.js',
Expand Down Expand Up @@ -49,6 +51,8 @@ module.exports = {
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**'
})
}),
resolve(),
commonjs()
]
};
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ it('works for sparse arrays', function () {
assert.deepEqual(at(array, -1), [1]);
});

it('works for typed arrays', function () {
const array = new Int8Array([1, [2], [3, 4]]);

assert.equal(at(array, 0), array[0]);
assert.equal(at(array, -3), array[0]);

assert.deepEqual(at(array, 1), array[1]);
assert.deepEqual(at(array, -2), array[1]);

assert.deepEqual(at(array, 2), array[2]);
assert.deepEqual(at(array, -1), array[2]);

assert.equal(at(array, 3), undefined);
assert.equal(at(array, -4), undefined);

assert.equal(at([], 0), undefined);
assert.equal(at([], -1), undefined);

assert.equal(at([], Infinity), undefined);
assert.equal(at([], Infinity), undefined);
});

it('uses native implementation if it’s available', function () {
assert.equal(preferNative('abc', -1), 'c');
});

0 comments on commit 46fa19d

Please sign in to comment.