typed-array-byte-offset 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "root": true,
3
+
4
+ "extends": "@ljharb",
5
+
6
+ "rules": {
7
+ },
8
+ }
@@ -0,0 +1,12 @@
1
+ # These are supported funding model platforms
2
+
3
+ github: [ljharb]
4
+ patreon: # Replace with a single Patreon username
5
+ open_collective: # Replace with a single Open Collective username
6
+ ko_fi: # Replace with a single Ko-fi username
7
+ tidelift: npm/typed-array-length
8
+ community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
+ liberapay: # Replace with a single Liberapay username
10
+ issuehunt: # Replace with a single IssueHunt username
11
+ otechie: # Replace with a single Otechie username
12
+ custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
package/.nycrc ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "all": true,
3
+ "check-coverage": false,
4
+ "reporter": ["text-summary", "text", "html", "json"],
5
+ "lines": 86,
6
+ "statements": 85.93,
7
+ "functions": 82.43,
8
+ "branches": 76.06,
9
+ "exclude": [
10
+ "coverage",
11
+ "test"
12
+ ]
13
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## v1.0.0 - 2023-06-06
9
+
10
+ ### Commits
11
+
12
+ - Initial implementation, tests, readme [`f227633`](https://github.com/inspect-js/typed-array-byte-offset/commit/f2276337a907bdfe9725af1b36c3109e76f2430d)
13
+ - Initial commit [`806bbaf`](https://github.com/inspect-js/typed-array-byte-offset/commit/806bbaf81e0267aebce5ae68cbf138718513642a)
14
+ - npm init [`1151981`](https://github.com/inspect-js/typed-array-byte-offset/commit/1151981427eb1fddab8599d36e6afea50a78293f)
15
+ - Only apps should have lockfiles [`5fa9933`](https://github.com/inspect-js/typed-array-byte-offset/commit/5fa9933275f10bdb9e8a175cc70a8228d4811642)
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Inspect JS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
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
package/index.js ADDED
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ var forEach = require('for-each');
4
+ var callBind = require('call-bind');
5
+
6
+ var typedArrays = require('available-typed-arrays')();
7
+
8
+ var getters = {};
9
+ var hasProto = require('has-proto')();
10
+
11
+ var gOPD = Object.getOwnPropertyDescriptor;
12
+ var oDP = Object.defineProperty;
13
+ if (gOPD) {
14
+ var getByteOffset = function (x) {
15
+ return x.byteOffset;
16
+ };
17
+ forEach(typedArrays, function (typedArray) {
18
+ // In Safari 7, Typed Array constructors are typeof object
19
+ if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
20
+ var Proto = global[typedArray].prototype;
21
+ var descriptor = gOPD(Proto, 'byteOffset');
22
+ if (!descriptor && hasProto) {
23
+ var superProto = Proto.__proto__; // eslint-disable-line no-proto
24
+ descriptor = gOPD(superProto, 'byteOffset');
25
+ }
26
+ // Opera 12.16 has a magic byteOffset data property on instances AND on Proto
27
+ if (descriptor && descriptor.get) {
28
+ getters[typedArray] = callBind(descriptor.get);
29
+ } else if (oDP) {
30
+ // this is likely an engine where instances have a magic byteOffset data property
31
+ var arr = new global[typedArray](2);
32
+ descriptor = gOPD(arr, 'byteOffset');
33
+ if (descriptor && descriptor.configurable) {
34
+ oDP(arr, 'length', { value: 3 });
35
+ }
36
+ if (arr.length === 2) {
37
+ getters[typedArray] = getByteOffset;
38
+ }
39
+ }
40
+ }
41
+ });
42
+ }
43
+
44
+ var tryTypedArrays = function tryAllTypedArrays(value) {
45
+ var foundOffset;
46
+ forEach(getters, function (getter) {
47
+ if (typeof foundOffset !== 'number') {
48
+ try {
49
+ var offset = getter(value);
50
+ if (typeof offset === 'number') {
51
+ foundOffset = offset;
52
+ }
53
+ } catch (e) {}
54
+ }
55
+ });
56
+ return foundOffset;
57
+ };
58
+
59
+ var isTypedArray = require('is-typed-array');
60
+
61
+ module.exports = function typedArrayByteOffset(value) {
62
+ if (!isTypedArray(value)) {
63
+ return false;
64
+ }
65
+ return tryTypedArrays(value);
66
+ };
package/package.json ADDED
@@ -0,0 +1,98 @@
1
+ {
2
+ "name": "typed-array-byte-offset",
3
+ "version": "1.0.0",
4
+ "description": "Robustly get the byte offset of a Typed Array",
5
+ "main": "index.js",
6
+ "exports": {
7
+ ".": "./index.js",
8
+ "./package.json": "./package.json"
9
+ },
10
+ "scripts": {
11
+ "prepack": "npmignore --auto --commentLines=autogenerated",
12
+ "prepublishOnly": "safe-publish-latest",
13
+ "prepublish": "not-in-publish || npm run prepublishOnly",
14
+ "pretest": "npm run lint",
15
+ "prelint": "evalmd README.md",
16
+ "lint": "eslint --ext=js,mjs .",
17
+ "tests-only": "nyc tape 'test/**/*.js'",
18
+ "test": "npm run tests-only",
19
+ "posttest": "aud --production",
20
+ "version": "auto-changelog && git add CHANGELOG.md",
21
+ "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/inspect-js/typed-array-byte-offset.git"
26
+ },
27
+ "keywords": [
28
+ "typed",
29
+ "array",
30
+ "byteOffset",
31
+ "byte",
32
+ "offset",
33
+ "robust",
34
+ "es",
35
+ "Int8Array",
36
+ "Uint8Array",
37
+ "Uint8ClampedArray",
38
+ "Int16Array",
39
+ "Uint16Array",
40
+ "Int32Array",
41
+ "Uint32Array",
42
+ "Float32Array",
43
+ "Float64Array",
44
+ "BigInt64Array",
45
+ "BigUint64Array"
46
+ ],
47
+ "author": "Jordan Harband <ljharb@gmail.com>",
48
+ "funding": {
49
+ "url": "https://github.com/sponsors/ljharb"
50
+ },
51
+ "license": "MIT",
52
+ "bugs": {
53
+ "url": "https://github.com/inspect-js/typed-array-byte-offset/issues"
54
+ },
55
+ "homepage": "https://github.com/inspect-js/typed-array-byte-offset#readme",
56
+ "dependencies": {
57
+ "available-typed-arrays": "^1.0.5",
58
+ "call-bind": "^1.0.2",
59
+ "for-each": "^0.3.3",
60
+ "has-proto": "^1.0.1",
61
+ "is-typed-array": "^1.1.10"
62
+ },
63
+ "devDependencies": {
64
+ "@ljharb/eslint-config": "^21.1.0",
65
+ "aud": "^2.0.2",
66
+ "auto-changelog": "^2.4.0",
67
+ "eslint": "=8.8.0",
68
+ "evalmd": "^0.0.19",
69
+ "in-publish": "^2.0.1",
70
+ "is-callable": "^1.2.7",
71
+ "make-arrow-function": "^1.2.0",
72
+ "make-generator-function": "^2.0.0",
73
+ "npmignore": "^0.3.0",
74
+ "nyc": "^10.3.2",
75
+ "object-inspect": "^1.12.3",
76
+ "safe-publish-latest": "^2.0.0",
77
+ "tape": "^5.6.3"
78
+ },
79
+ "engines": {
80
+ "node": ">= 0.4"
81
+ },
82
+ "auto-changelog": {
83
+ "output": "CHANGELOG.md",
84
+ "template": "keepachangelog",
85
+ "unreleased": false,
86
+ "commitLimit": false,
87
+ "backfillLimit": false,
88
+ "hideCredit": true
89
+ },
90
+ "testling": {
91
+ "files": "test/index.js"
92
+ },
93
+ "publishConfig": {
94
+ "ignore": [
95
+ ".github/workflows"
96
+ ]
97
+ }
98
+ }
package/test/index.js ADDED
@@ -0,0 +1,88 @@
1
+ 'use strict';
2
+
3
+ var test = require('tape');
4
+ var typedArrayByteOffset = require('../');
5
+ var isCallable = require('is-callable');
6
+ var generators = require('make-generator-function')();
7
+ var arrowFn = require('make-arrow-function')();
8
+ var forEach = require('for-each');
9
+ var inspect = require('object-inspect');
10
+
11
+ var typedArrayNames = [
12
+ 'Int8Array',
13
+ 'Uint8Array',
14
+ 'Uint8ClampedArray',
15
+ 'Int16Array',
16
+ 'Uint16Array',
17
+ 'Int32Array',
18
+ 'Uint32Array',
19
+ 'Float32Array',
20
+ 'Float64Array',
21
+ 'BigInt64Array',
22
+ 'BigUint64Array'
23
+ ];
24
+
25
+ test('not arrays', function (t) {
26
+ t.test('non-number/string primitives', function (st) {
27
+ st.equal(false, typedArrayByteOffset(), 'undefined is not typed array');
28
+ st.equal(false, typedArrayByteOffset(null), 'null is not typed array');
29
+ st.equal(false, typedArrayByteOffset(false), 'false is not typed array');
30
+ st.equal(false, typedArrayByteOffset(true), 'true is not typed array');
31
+ st.end();
32
+ });
33
+
34
+ t.equal(false, typedArrayByteOffset({}), 'object is not typed array');
35
+ t.equal(false, typedArrayByteOffset(/a/g), 'regex literal is not typed array');
36
+ t.equal(false, typedArrayByteOffset(new RegExp('a', 'g')), 'regex object is not typed array');
37
+ t.equal(false, typedArrayByteOffset(new Date()), 'new Date() is not typed array');
38
+
39
+ t.test('numbers', function (st) {
40
+ st.equal(false, typedArrayByteOffset(42), 'number is not typed array');
41
+ st.equal(false, typedArrayByteOffset(Object(42)), 'number object is not typed array');
42
+ st.equal(false, typedArrayByteOffset(NaN), 'NaN is not typed array');
43
+ st.equal(false, typedArrayByteOffset(Infinity), 'Infinity is not typed array');
44
+ st.end();
45
+ });
46
+
47
+ t.test('strings', function (st) {
48
+ st.equal(false, typedArrayByteOffset('foo'), 'string primitive is not typed array');
49
+ st.equal(false, typedArrayByteOffset(Object('foo')), 'string object is not typed array');
50
+ st.end();
51
+ });
52
+
53
+ t.end();
54
+ });
55
+
56
+ test('Functions', function (t) {
57
+ t.equal(false, typedArrayByteOffset(function () {}), 'function is not typed array');
58
+ t.end();
59
+ });
60
+
61
+ test('Generators', { skip: generators.length === 0 }, function (t) {
62
+ forEach(generators, function (genFn) {
63
+ t.equal(false, typedArrayByteOffset(genFn), 'generator function ' + inspect(genFn) + ' is not typed array');
64
+ });
65
+ t.end();
66
+ });
67
+
68
+ test('Arrow functions', { skip: !arrowFn }, function (t) {
69
+ t.equal(false, typedArrayByteOffset(arrowFn), 'arrow function is not typed array');
70
+ t.end();
71
+ });
72
+
73
+ test('Typed Arrays', function (t) {
74
+ var length = 32;
75
+ var byteOffset = 16;
76
+
77
+ forEach(typedArrayNames, function (typedArray) {
78
+ var buffer = new ArrayBuffer(length);
79
+ var TypedArray = global[typedArray];
80
+ if (isCallable(TypedArray)) {
81
+ var arr = new TypedArray(buffer, byteOffset);
82
+ t.equal(typedArrayByteOffset(arr), byteOffset, 'new ' + typedArray + '(new ArrayBuffer(' + length + '), ' + byteOffset + ') is typed array of byte offset ' + byteOffset);
83
+ } else {
84
+ t.comment('# SKIP ' + typedArray + ' is not supported');
85
+ }
86
+ });
87
+ t.end();
88
+ });