Skip to content

Commit 15fab4c

Browse files
committed
[patch] add types
1 parent 3d0ceea commit 15fab4c

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
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 isArrayBuffer(value: unknown): value is ArrayBuffer;
2+
3+
export = isArrayBuffer;

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ var callBind = require('call-bind');
44
var callBound = require('call-bind/callBound');
55
var GetIntrinsic = require('get-intrinsic');
66

7-
var $ArrayBuffer = GetIntrinsic('ArrayBuffer', true);
7+
var $ArrayBuffer = GetIntrinsic('%ArrayBuffer%', true);
8+
/** @type {undefined | ((receiver: ArrayBuffer) => number) | ((receiver: unknown) => never)} */
89
var $byteLength = callBound('ArrayBuffer.prototype.byteLength', true);
910
var $toString = callBound('Object.prototype.toString');
1011

1112
// in node 0.10, ArrayBuffers have no prototype methods, but have an own slot-checking `slice` method
12-
var abSlice = $ArrayBuffer && !$byteLength && new $ArrayBuffer().slice;
13-
var $abSlice = abSlice && callBind(abSlice);
13+
var abSlice = !!$ArrayBuffer && !$byteLength && new $ArrayBuffer(0).slice;
14+
var $abSlice = !!abSlice && callBind(abSlice);
1415

16+
/** @type {import('.')} */
1517
module.exports = $byteLength || $abSlice
1618
? function isArrayBuffer(obj) {
1719
if (!obj || typeof obj !== 'object') {
1820
return false;
1921
}
2022
try {
2123
if ($byteLength) {
24+
// @ts-expect-error no idea why TS can't handle the overload
2225
$byteLength(obj);
2326
} else {
27+
// @ts-expect-error TS chooses not to type-narrow inside a closure
2428
$abSlice(obj, 0);
2529
}
2630
return true;
@@ -30,7 +34,7 @@ module.exports = $byteLength || $abSlice
3034
}
3135
: $ArrayBuffer
3236
// in node 0.8, ArrayBuffers have no prototype or own methods, but also no Symbol.toStringTag
33-
? function IsArrayBuffer(obj) {
37+
? function isArrayBuffer(obj) {
3438
return $toString(obj) === '[object ArrayBuffer]';
3539
}
3640
: function isArrayBuffer(obj) { // eslint-disable-line no-unused-vars

package.json

Lines changed: 10 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
"sideEffects": false,
1112
"scripts": {
1213
"prepack": "npmignore --auto --commentLines=autogenerated",
1314
"prepublishOnly": "safe-publish-latest",
1415
"prepublish": "not-in-publish || npm run prepublishOnly",
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 --",
@@ -43,6 +45,12 @@
4345
"homepage": "https://github.com/inspect-js/is-array-buffer#readme",
4446
"devDependencies": {
4547
"@ljharb/eslint-config": "^21.1.0",
48+
"@types/call-bind": "^1.0.5",
49+
"@types/es-value-fixtures": "^1.4.4",
50+
"@types/for-each": "^0.3.3",
51+
"@types/get-intrinsic": "^1.2.2",
52+
"@types/object-inspect": "^1.8.4",
53+
"@types/tape": "^5.6.4",
4654
"aud": "^2.0.4",
4755
"auto-changelog": "^2.4.0",
4856
"available-typed-arrays": "^1.0.6",
@@ -54,7 +62,8 @@
5462
"nyc": "^10.3.2",
5563
"object-inspect": "^1.13.1",
5664
"safe-publish-latest": "^2.0.0",
57-
"tape": "^5.7.4"
65+
"tape": "^5.7.4",
66+
"typescript": "next"
5867
},
5968
"auto-changelog": {
6069
"output": "CHANGELOG.md",

test/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ var isArrayBuffer = require('..');
1111
test('isArrayBuffer', function (t) {
1212
t.equal(typeof isArrayBuffer, 'function', 'is a function');
1313

14-
var nonABs = v.primitives.concat(
14+
/** @type {unknown[]} */
15+
var nonABs = [].concat(
16+
// @ts-expect-error TS sucks with [].concat
17+
v.primitives,
1518
v.objects,
16-
typeof SharedArrayBuffer === 'function' ? new SharedArrayBuffer() : []
19+
typeof SharedArrayBuffer === 'function' ? new SharedArrayBuffer(0) : []
1720
);
1821
forEach(nonABs, function (nonAB) {
1922
t.equal(isArrayBuffer(nonAB), false, inspect(nonAB) + ' is not an ArrayBuffer');
2023
});
2124

2225
t.test('actual ArrayBuffer instances', { skip: typeof ArrayBuffer === 'undefined' }, function (st) {
26+
// @ts-expect-error TS grumbles about 0 args
2327
var ab = new ArrayBuffer();
2428
st.equal(isArrayBuffer(ab), true, inspect(ab) + ' is an ArrayBuffer');
2529

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)