1 | # typed-array-byte-offset <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 | Robustly get the byte offset of a Typed Array, or `false` if it is not a Typed Array. Works cross-realm, in every engine, even if the `byteOffset` property is overridden.
|
---|
11 |
|
---|
12 | ## Example
|
---|
13 |
|
---|
14 | ```js
|
---|
15 | var typedArrayByteOffset = require('typed-array-byte-offset');
|
---|
16 | var assert = require('assert');
|
---|
17 |
|
---|
18 | assert.equal(false, typedArrayByteOffset(undefined));
|
---|
19 | assert.equal(false, typedArrayByteOffset(null));
|
---|
20 | assert.equal(false, typedArrayByteOffset(false));
|
---|
21 | assert.equal(false, typedArrayByteOffset(true));
|
---|
22 | assert.equal(false, typedArrayByteOffset([]));
|
---|
23 | assert.equal(false, typedArrayByteOffset({}));
|
---|
24 | assert.equal(false, typedArrayByteOffset(/a/g));
|
---|
25 | assert.equal(false, typedArrayByteOffset(new RegExp('a', 'g')));
|
---|
26 | assert.equal(false, typedArrayByteOffset(new Date()));
|
---|
27 | assert.equal(false, typedArrayByteOffset(42));
|
---|
28 | assert.equal(false, typedArrayByteOffset(NaN));
|
---|
29 | assert.equal(false, typedArrayByteOffset(Infinity));
|
---|
30 | assert.equal(false, typedArrayByteOffset(new Number(42)));
|
---|
31 | assert.equal(false, typedArrayByteOffset('foo'));
|
---|
32 | assert.equal(false, typedArrayByteOffset(Object('foo')));
|
---|
33 | assert.equal(false, typedArrayByteOffset(function () {}));
|
---|
34 | assert.equal(false, typedArrayByteOffset(function* () {}));
|
---|
35 | assert.equal(false, typedArrayByteOffset(x => x * x));
|
---|
36 | assert.equal(false, typedArrayByteOffset([]));
|
---|
37 |
|
---|
38 | const buffer = new ArrayBuffer(32);
|
---|
39 |
|
---|
40 | assert.equal(8, typedArrayByteOffset(new Int8Array(buffer, 8)));
|
---|
41 | assert.equal(8, typedArrayByteOffset(new Uint8Array(buffer, 8)));
|
---|
42 | assert.equal(8, typedArrayByteOffset(new Uint8ClampedArray(buffer, 8)));
|
---|
43 | assert.equal(4, typedArrayByteOffset(new Int16Array(buffer, 4)));
|
---|
44 | assert.equal(4, typedArrayByteOffset(new Uint16Array(buffer, 4)));
|
---|
45 | assert.equal(8, typedArrayByteOffset(new Int32Array(buffer, 8)));
|
---|
46 | assert.equal(8, typedArrayByteOffset(new Uint32Array(buffer, 8)));
|
---|
47 | assert.equal(16, typedArrayByteOffset(new Float32Array(buffer, 16)));
|
---|
48 | assert.equal(16, typedArrayByteOffset(new Float64Array(buffer, 16)));
|
---|
49 | assert.equal(16, typedArrayByteOffset(new BigInt64Array(buffer, 16)));
|
---|
50 | assert.equal(16, typedArrayByteOffset(new BigUint64Array(buffer, 16)));
|
---|
51 | ```
|
---|
52 |
|
---|
53 | ## Tests
|
---|
54 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
55 |
|
---|
56 | [package-url]: https://npmjs.org/package/typed-array-byte-offset
|
---|
57 | [npm-version-svg]: https://versionbadg.es/inspect-js/typed-array-byte-offset.svg
|
---|
58 | [deps-svg]: https://david-dm.org/inspect-js/typed-array-byte-offset.svg
|
---|
59 | [deps-url]: https://david-dm.org/inspect-js/typed-array-byte-offset
|
---|
60 | [dev-deps-svg]: https://david-dm.org/inspect-js/typed-array-byte-offset/dev-status.svg
|
---|
61 | [dev-deps-url]: https://david-dm.org/inspect-js/typed-array-byte-offset#info=devDependencies
|
---|
62 | [npm-badge-png]: https://nodei.co/npm/typed-array-byte-offset.png?downloads=true&stars=true
|
---|
63 | [license-image]: https://img.shields.io/npm/l/typed-array-byte-offset.svg
|
---|
64 | [license-url]: LICENSE
|
---|
65 | [downloads-image]: https://img.shields.io/npm/dm/typed-array-byte-offset.svg
|
---|
66 | [downloads-url]: https://npm-stat.com/charts.html?package=typed-array-byte-offset
|
---|
67 | [codecov-image]: https://codecov.io/gh/inspect-js/typed-array-byte-offset/branch/main/graphs/badge.svg
|
---|
68 | [codecov-url]: https://app.codecov.io/gh/inspect-js/typed-array-byte-offset/
|
---|
69 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/typed-array-byte-offset
|
---|
70 | [actions-url]: https://github.com/inspect-js/typed-array-byte-offset/actions
|
---|