Skip to content

Commit 49c4d4c

Browse files
committed
[patch] add types
1 parent 1efa8bf commit 49c4d4c

File tree

7 files changed

+146
-20
lines changed

7 files changed

+146
-20
lines changed

.editorconfig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
max_line_length = 150
11+
12+
[CHANGELOG.md]
13+
indent_style = space
14+
indent_size = 2
15+
16+
[*.json]
17+
max_line_length = off
18+
19+
[Makefile]
20+
max_line_length = off

.github/workflows/node-aught.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ jobs:
99
range: '< 10'
1010
type: minors
1111
command: npm run tests-only
12+
skip-ls-check: false
1213

1314
node:
1415
name: 'node < 10'

index.d.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type TypedArrayName =
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 whichTypedArray(value: unknown): TypedArrayName | false | null;
15+
16+
export = whichTypedArray;

index.js

+39-18
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,32 @@ var typedArrays = availableTypedArrays();
1515
var $slice = callBound('String.prototype.slice');
1616
var getPrototypeOf = Object.getPrototypeOf; // require('getprototypeof');
1717

18-
var $indexOf = callBound('Array.prototype.indexOf', true) || function indexOf(array, value) {
18+
var $indexOf = callBound('Array.prototype.indexOf', true) || /** @type {(array: readonly unknown[], value: unknown) => keyof array} */ function indexOf(array, value) {
1919
for (var i = 0; i < array.length; i += 1) {
2020
if (array[i] === value) {
2121
return i;
2222
}
2323
}
2424
return -1;
2525
};
26+
27+
/** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
28+
/** @typedef {'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray' | 'Int16Array' | 'Uint16Array' | 'Int32Array' | 'Uint32Array' | 'Float32Array' | 'Float64Array' | 'BigInt64Array' | 'BigUint64Array'} TypedArrayName */
29+
/** @type {{ [k in `\$${TypedArrayName}`]?: (receiver: TypedArray) => string | typeof Uint8Array.prototype.slice.call | typeof Uint8Array.prototype.set.call } & { __proto__: null }} */
2630
var cache = { __proto__: null };
2731
if (hasToStringTag && gOPD && getPrototypeOf) {
2832
forEach(typedArrays, function (typedArray) {
2933
var arr = new g[typedArray]();
3034
if (Symbol.toStringTag in arr) {
3135
var proto = getPrototypeOf(arr);
36+
// @ts-expect-error TS won't narrow inside a closure
3237
var descriptor = gOPD(proto, Symbol.toStringTag);
3338
if (!descriptor) {
3439
var superProto = getPrototypeOf(proto);
40+
// @ts-expect-error TS won't narrow inside a closure
3541
descriptor = gOPD(superProto, Symbol.toStringTag);
3642
}
43+
// @ts-expect-error TODO: fix
3744
cache['$' + typedArray] = callBind(descriptor.get);
3845
}
3946
});
@@ -42,38 +49,52 @@ if (hasToStringTag && gOPD && getPrototypeOf) {
4249
var arr = new g[typedArray]();
4350
var fn = arr.slice || arr.set;
4451
if (fn) {
52+
// @ts-expect-error TODO: fix
4553
cache['$' + typedArray] = callBind(fn);
4654
}
4755
});
4856
}
4957

58+
/** @type {import('.')} */
5059
var tryTypedArrays = function tryAllTypedArrays(value) {
51-
var found = false;
52-
forEach(cache, function (getter, typedArray) {
53-
if (!found) {
54-
try {
55-
if ('$' + getter(value) === typedArray) {
56-
found = $slice(typedArray, 1);
57-
}
58-
} catch (e) { /**/ }
60+
/** @type {ReturnType<tryAllTypedArrays>} */ var found = false;
61+
forEach(
62+
// eslint-disable-next-line no-extra-parens
63+
/** @type {Record<`\$${TypedArrayName}`, typeof cache>} */ /** @type {any} */ (cache),
64+
/** @type {(getter: typeof cache, name: `\$${TypedArrayName}`) => void} */ function (getter, typedArray) {
65+
if (!found) {
66+
try {
67+
// @ts-expect-error TODO: fix
68+
if ('$' + getter(value) === typedArray) {
69+
found = $slice(typedArray, 1);
70+
}
71+
} catch (e) { /**/ }
72+
}
5973
}
60-
});
74+
);
6175
return found;
6276
};
6377

78+
/** @type {import('.')} */
6479
var trySlices = function tryAllSlices(value) {
65-
var found = false;
66-
forEach(cache, function (getter, name) {
67-
if (!found) {
68-
try {
69-
getter(value);
70-
found = $slice(name, 1);
71-
} catch (e) { /**/ }
80+
/** @type {ReturnType<tryAllSlices>} */ var found = false;
81+
forEach(
82+
// eslint-disable-next-line no-extra-parens
83+
/** @type {any} */ (cache),
84+
/** @type {(getter: typeof cache, name: `\$${TypedArrayName}`) => void} */ function (getter, name) {
85+
if (!found) {
86+
try {
87+
// @ts-expect-error TODO: fix
88+
getter(value);
89+
found = $slice(name, 1);
90+
} catch (e) { /**/ }
91+
}
7292
}
73-
});
93+
);
7494
return found;
7595
};
7696

97+
/** @type {import('.')} */
7798
module.exports = function whichTypedArray(value) {
7899
if (!value || typeof value !== 'object') { return false; }
79100
if (!hasToStringTag) {

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"description": "Which kind of Typed Array is this JavaScript value? Works cross-realm, without `instanceof`, and despite Symbol.toStringTag.",
2020
"license": "MIT",
2121
"main": "index.js",
22+
"types": "./index.d.ts",
2223
"sideEffects": false,
2324
"scripts": {
2425
"prepack": "npmignore --auto --commentLines=autogenerated",
@@ -30,6 +31,7 @@
3031
"test:harmony": "nyc node --harmony --es-staging test",
3132
"posttest": "aud --production",
3233
"lint": "eslint --ext=js,mjs .",
34+
"postlint": "tsc -p .",
3335
"version": "auto-changelog && git add CHANGELOG.md",
3436
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
3537
},
@@ -66,6 +68,14 @@
6668
},
6769
"devDependencies": {
6870
"@ljharb/eslint-config": "^21.1.0",
71+
"@types/call-bind": "^1.0.5",
72+
"@types/for-each": "^0.3.3",
73+
"@types/gopd": "^1.0.3",
74+
"@types/is-callable": "^1.1.2",
75+
"@types/make-arrow-function": "^1.2.2",
76+
"@types/make-generator-function": "^2.0.3",
77+
"@types/node": "^20.11.14",
78+
"@types/tape": "^5.6.4",
6979
"aud": "^2.0.4",
7080
"auto-changelog": "^2.4.0",
7181
"eslint": "=8.8.0",
@@ -76,7 +86,8 @@
7686
"npmignore": "^0.3.1",
7787
"nyc": "^10.3.2",
7888
"safe-publish-latest": "^2.0.0",
79-
"tape": "^5.7.4"
89+
"tape": "^5.7.4",
90+
"typescript": "^5.4.0-dev.20240131"
8091
},
8192
"testling": {
8293
"files": "test/index.js",

test/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var typedArrayNames = [
2424

2525
test('not arrays', function (t) {
2626
t.test('non-number/string primitives', function (st) {
27+
// @ts-expect-error
2728
st.equal(false, whichTypedArray(), 'undefined is not typed array');
2829
st.equal(false, whichTypedArray(null), 'null is not typed array');
2930
st.equal(false, whichTypedArray(false), 'false is not typed array');
@@ -74,9 +75,13 @@ test('Arrow functions', { skip: arrows.length === 0 }, function (t) {
7475

7576
test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
7677
forEach(typedArrayNames, function (typedArray) {
78+
// @ts-expect-error TODO: fix
7779
if (typeof global[typedArray] === 'function') {
80+
// @ts-expect-error TODO: fix
7881
var fakeTypedArray = [];
82+
// @ts-expect-error TODO: fix
7983
fakeTypedArray[Symbol.toStringTag] = typedArray;
84+
// @ts-expect-error TODO: fix
8085
t.equal(false, whichTypedArray(fakeTypedArray), 'faked ' + typedArray + ' is not typed array');
8186
} else {
8287
t.comment('# SKIP ' + typedArray + ' is not supported');
@@ -85,9 +90,12 @@ test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
8590
t.end();
8691
});
8792

93+
/** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructor} TypedArrayConstructor */
94+
8895
test('Typed Arrays', function (t) {
8996
forEach(typedArrayNames, function (typedArray) {
90-
var TypedArray = global[typedArray];
97+
// @ts-expect-error TODO: fix
98+
/** @type {TypedArrayConstructor} */ var TypedArray = global[typedArray];
9199
if (isCallable(TypedArray)) {
92100
var arr = new TypedArray(10);
93101
t.equal(whichTypedArray(arr), typedArray, 'new ' + typedArray + '(10) is typed array of type ' + typedArray);

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)