Skip to content

Commit 9eecdd2

Browse files
committed
add types
1 parent 805cee2 commit 9eecdd2

File tree

5 files changed

+94
-16
lines changed

5 files changed

+94
-16
lines changed

index.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type TypedArray =
2+
| Int8Array
3+
| Uint8Array
4+
| Uint8ClampedArray
5+
| Int16Array
6+
| Uint16Array
7+
| Int32Array
8+
| Uint32Array
9+
| Float32Array
10+
| Float64Array
11+
| BigInt64Array
12+
| BigUint64Array;
13+
14+
declare function typedArrayByteOffset(value: TypedArray): number;
15+
declare function typedArrayByteOffset(value: unknown): false;
16+
17+
export = typedArrayByteOffset;

index.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,30 @@ var callBind = require('call-bind');
55

66
var typedArrays = require('available-typed-arrays')();
77

8+
/** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
9+
/** @typedef {(x: TypedArray) => number} ByteOffsetGetter */
10+
11+
/** @type {Object.<typeof typedArrays, ByteOffsetGetter>} */
812
var getters = {};
913
var hasProto = require('has-proto')();
1014

1115
var gOPD = require('gopd');
1216
var oDP = Object.defineProperty;
1317
if (gOPD) {
18+
/** @type {ByteOffsetGetter} */
1419
var getByteOffset = function (x) {
1520
return x.byteOffset;
1621
};
1722
forEach(typedArrays, function (typedArray) {
1823
// In Safari 7, Typed Array constructors are typeof object
1924
if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
2025
var Proto = global[typedArray].prototype;
26+
// @ts-expect-error TS can't guarantee the callback is invoked sync
2127
var descriptor = gOPD(Proto, 'byteOffset');
2228
if (!descriptor && hasProto) {
29+
// @ts-expect-error hush, TS, every object has a dunder proto
2330
var superProto = Proto.__proto__; // eslint-disable-line no-proto
31+
// @ts-expect-error TS can't guarantee the callback is invoked sync
2432
descriptor = gOPD(superProto, 'byteOffset');
2533
}
2634
// Opera 12.16 has a magic byteOffset data property on instances AND on Proto
@@ -29,6 +37,7 @@ if (gOPD) {
2937
} else if (oDP) {
3038
// this is likely an engine where instances have a magic byteOffset data property
3139
var arr = new global[typedArray](2);
40+
// @ts-expect-error TS can't guarantee the callback is invoked sync
3241
descriptor = gOPD(arr, 'byteOffset');
3342
if (descriptor && descriptor.configurable) {
3443
oDP(arr, 'length', { value: 3 });
@@ -41,9 +50,10 @@ if (gOPD) {
4150
});
4251
}
4352

53+
/** @type {ByteOffsetGetter} */
4454
var tryTypedArrays = function tryAllTypedArrays(value) {
45-
var foundOffset;
46-
forEach(getters, function (getter) {
55+
/** @type {number} */ var foundOffset;
56+
forEach(getters, /** @type {(getter: ByteOffsetGetter) => void} */ function (getter) {
4757
if (typeof foundOffset !== 'number') {
4858
try {
4959
var offset = getter(value);
@@ -53,11 +63,13 @@ var tryTypedArrays = function tryAllTypedArrays(value) {
5363
} catch (e) {}
5464
}
5565
});
66+
// @ts-expect-error TS can't guarantee the callback is invoked sync
5667
return foundOffset;
5768
};
5869

5970
var isTypedArray = require('is-typed-array');
6071

72+
/** @type {import('.')} */
6173
module.exports = function typedArrayByteOffset(value) {
6274
if (!isTypedArray(value)) {
6375
return false;

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
".": "./index.js",
88
"./package.json": "./package.json"
99
},
10+
"types": "./index.d.ts",
1011
"sideEffects": false,
1112
"scripts": {
1213
"prepack": "npmignore --auto --commentLines=autogenerated",
@@ -64,6 +65,14 @@
6465
},
6566
"devDependencies": {
6667
"@ljharb/eslint-config": "^21.1.0",
68+
"@types/call-bind": "^1.0.5",
69+
"@types/for-each": "^0.3.3",
70+
"@types/gopd": "^1.0.3",
71+
"@types/is-callable": "^1.1.2",
72+
"@types/make-arrow-function": "^1.2.2",
73+
"@types/make-generator-function": "^2.0.3",
74+
"@types/object-inspect": "^1.8.4",
75+
"@types/tape": "^5.6.4",
6776
"aud": "^2.0.4",
6877
"auto-changelog": "^2.4.0",
6978
"eslint": "=8.8.0",
@@ -75,8 +84,10 @@
7584
"npmignore": "^0.3.1",
7685
"nyc": "^10.3.2",
7786
"object-inspect": "^1.13.1",
87+
"possible-typed-array-names": "^1.0.0",
7888
"safe-publish-latest": "^2.0.0",
79-
"tape": "^5.7.5"
89+
"tape": "^5.7.5",
90+
"typescript": "^5.4.0-dev.20240219"
8091
},
8192
"engines": {
8293
"node": ">= 0.4"

test/index.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,11 @@ var arrowFn = require('make-arrow-function')();
88
var forEach = require('for-each');
99
var inspect = require('object-inspect');
1010

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-
];
11+
var typedArrayNames = require('possible-typed-array-names');
2412

2513
test('not arrays', function (t) {
2614
t.test('non-number/string primitives', function (st) {
15+
// @ts-expect-error
2716
st.equal(false, typedArrayByteOffset(), 'undefined is not typed array');
2817
st.equal(false, typedArrayByteOffset(null), 'null is not typed array');
2918
st.equal(false, typedArrayByteOffset(false), 'false is not typed array');

tsconfig.json

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig to read more about this file */
4+
5+
/* Projects */
6+
7+
/* Language and Environment */
8+
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
9+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
10+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
11+
"useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
12+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
13+
14+
/* Modules */
15+
"module": "commonjs", /* Specify what module code is generated. */
16+
// "rootDir": "./", /* Specify the root folder within your source files. */
17+
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
18+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
19+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
20+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
21+
"typeRoots": ["types"], /* Specify multiple folders that act like './node_modules/@types'. */
22+
"resolveJsonModule": true, /* Enable importing .json files. */
23+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
24+
25+
/* JavaScript Support */
26+
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
27+
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
28+
"maxNodeModuleJsDepth": 0, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
29+
30+
/* Emit */
31+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
32+
"declarationMap": true, /* Create sourcemaps for d.ts files. */
33+
"noEmit": true, /* Disable emitting files from a compilation. */
34+
35+
/* Interop Constraints */
36+
"allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
37+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
38+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
39+
40+
/* Type Checking */
41+
"strict": true, /* Enable all strict type-checking options. */
42+
43+
/* Completeness */
44+
//"skipLibCheck": true /* Skip type checking all .d.ts files. */
45+
},
46+
"exclude": [
47+
"coverage"
48+
]
49+
}

0 commit comments

Comments
 (0)