Skip to content

Commit c2728ef

Browse files
committed
[patch] add types
1 parent 2ca9333 commit c2728ef

File tree

7 files changed

+77
-14
lines changed

7 files changed

+77
-14
lines changed

.github/workflows/node-aught.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ jobs:
99
range: '< 10'
1010
type: minors
1111
command: npm run tests-only
12+
skip-ls-check: true
1213

1314
node:
1415
name: 'node < 10'
1516
needs: [tests]
1617
runs-on: ubuntu-latest
1718
steps:
18-
- run: 'echo tests completed'
19+
- run: true

.github/workflows/node-tens.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ jobs:
1515
needs: [tests]
1616
runs-on: ubuntu-latest
1717
steps:
18-
- run: 'echo tests completed'
18+
- run: true

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare function isDataView(value: unknown): value is DataView;
2+
3+
export = isDataView;

index.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ var $DataView = GetIntrinsic('%DataView%', true);
77

88
var callBound = require('call-bind/callBound');
99

10+
// node <= 0.10, < 0.11.4 has a nonconfigurable own property instead of a prototype getter
1011
var $dataViewBuffer = callBound('DataView.prototype.buffer', true);
1112

1213
var isTypedArray = require('is-typed-array');
1314

14-
// node <= 0.10, < 0.11.4 has a nonconfigurable own property instead of a prototype getter
15+
/** @type {import('.')} */
1516
module.exports = function isDataView(x) {
1617
if (!x || typeof x !== 'object' || !$DataView || isTypedArray(x)) {
1718
return false;
@@ -26,20 +27,13 @@ module.exports = function isDataView(x) {
2627
}
2728
}
2829

29-
/*
30-
* try {
31-
* new $DataView(x); // eslint-disable-line no-new
32-
* x is an ArrayBuffer
33-
* } catch (e) {
34-
*/
3530
if (
3631
('getInt8' in x)
3732
&& typeof x.getInt8 === 'function'
3833
&& x.getInt8 === new $DataView(new $ArrayBuffer(1)).getInt8
3934
) {
4035
return true;
4136
}
42-
// }
4337

4438
return false;
4539
};

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
".": "./index.js",
88
"./package.json": "./package.json"
99
},
10+
"types": "./index.d.ts",
1011
"scripts": {
1112
"prepack": "npmignore --auto --commentLines=autogenerated",
1213
"prepublishOnly": "safe-publish-latest",
1314
"prepublish": "not-in-publish || npm run prepublishOnly",
1415
"prelint": "evalmd README.md",
1516
"lint": "eslint --ext=js,mjs .",
17+
"postlint": "tsc -p .",
1618
"pretest": "npm run lint",
1719
"tests-only": "nyc tape 'test/**/*.js'",
1820
"test": "npm run tests-only",
@@ -47,6 +49,14 @@
4749
},
4850
"devDependencies": {
4951
"@ljharb/eslint-config": "^21.1.0",
52+
"@types/call-bind": "^1.0.5",
53+
"@types/es-value-fixtures": "^1.4.4",
54+
"@types/for-each": "^0.3.3",
55+
"@types/make-arrow-function": "^1.2.2",
56+
"@types/make-generator-function": "^2.0.3",
57+
"@types/node": "^20.11.14",
58+
"@types/object-inspect": "^1.8.4",
59+
"@types/tape": "^5.6.4",
5060
"aud": "^2.0.4",
5161
"auto-changelog": "^2.4.0",
5262
"available-typed-arrays": "^1.0.6",
@@ -62,7 +72,8 @@
6272
"nyc": "^10.3.2",
6373
"object-inspect": "^1.13.1",
6474
"safe-publish-latest": "^2.0.0",
65-
"tape": "^5.7.4"
75+
"tape": "^5.7.4",
76+
"typescript": "next"
6677
},
6778
"testling": {
6879
"files": "test/index.js"

test/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ var inspect = require('object-inspect');
1212
var availableTypedArrays = require('available-typed-arrays')();
1313

1414
test('not DataViews', function (t) {
15-
forEach(v.primitives.concat(
15+
forEach([].concat(
16+
// @ts-expect-error TS sucks at [].concat
17+
v.primitives,
1618
v.objects,
1719
function () {},
1820
generators,
1921
arrowFns,
2022
[]
21-
), function (nonDV) {
23+
), /** @type {(nonDV: unknown) => void} */ function (nonDV) {
2224
t.equal(
2325
isDataView(nonDV),
2426
false,
@@ -37,8 +39,11 @@ test('not DataViews', function (t) {
3739

3840
test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
3941
forEach(availableTypedArrays, function (typedArray) {
42+
// @ts-expect-error
4043
var fakeTypedArray = [];
44+
// @ts-expect-error
4145
fakeTypedArray[Symbol.toStringTag] = typedArray;
46+
// @ts-expect-error
4247
t.notOk(isDataView(fakeTypedArray), 'faked ' + typedArray + ' is not typed array');
4348
});
4449

@@ -49,7 +54,7 @@ test('Data Views', { skip: typeof DataView !== 'function' }, function (t) {
4954
var ab = new ArrayBuffer(1);
5055
var dv = new DataView(ab);
5156

52-
t.equal(isDataView(dv), true, inspect(dv) + 'is a DataView');
57+
t.equal(isDataView(dv), true, inspect(dv) + ' is a DataView');
5358

5459
t.end();
5560
});

tsconfig.json

Lines changed: 49 additions & 0 deletions
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)