Skip to content

Commit

Permalink
[patch] add types
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 1, 2024
1 parent 19c974f commit 8a8a679
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 150

[CHANGELOG.md]
indent_style = space
indent_size = 2

[*.json]
max_line_length = off

[Makefile]
max_line_length = off
1 change: 1 addition & 0 deletions .github/workflows/node-aught.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
range: '< 10'
type: minors
command: npm run tests-only
skip-ls-check: true

node:
name: 'node < 10'
Expand Down
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;

declare function isTypedArray(value: unknown): value is TypedArray;

export = isTypedArray;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var whichTypedArray = require('which-typed-array');

/** @type {import('.')} */
module.exports = function isTypedArray(value) {
return !!whichTypedArray(value);
};
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
"description": "Is this value a JS Typed Array? This module works cross-realm/iframe, does not depend on `instanceof` or mutable properties, and despite ES6 Symbol.toStringTag.",
"license": "MIT",
"main": "index.js",
"types": "./index.d.ts",
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublishOnly": "safe-publish-latest",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prelint": "evalmd README.md",
"lint": "eslint --ext=js,mjs .",
"postlint": "tsc -p .",
"pretest": "npm run --silent lint",
"test": "npm run tests-only && npm run test:harmony",
"tests-only": "nyc tape test",
Expand Down Expand Up @@ -62,6 +64,13 @@
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.0",
"@types/for-each": "^0.3.3",
"@types/is-callable": "^1.1.2",
"@types/make-arrow-function": "^1.2.2",
"@types/make-generator-function": "^2.0.3",
"@types/node": "^20.11.14",
"@types/object-inspect": "^1.8.4",
"@types/tape": "^5.6.4",
"aud": "^2.0.4",
"auto-changelog": "^2.4.0",
"eslint": "=8.8.0",
Expand All @@ -76,7 +85,8 @@
"nyc": "^10.3.2",
"object-inspect": "^1.13.1",
"safe-publish-latest": "^2.0.0",
"tape": "^5.7.4"
"tape": "^5.7.4",
"typescript": "next"
},
"testling": {
"files": "test/index.js",
Expand Down
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var typedArrayNames = [

test('not arrays', function (t) {
t.test('non-number/string primitives', function (st) {
// @ts-expect-error Expected 1 arguments, but got 0.ts(2554)
st.notOk(isTypedArray(), 'undefined is not typed array');
st.notOk(isTypedArray(null), 'null is not typed array');
st.notOk(isTypedArray(false), 'false is not typed array');
Expand Down Expand Up @@ -73,9 +74,13 @@ test('Arrow functions', { skip: !arrowFn }, function (t) {

test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
forEach(typedArrayNames, function (typedArray) {
// @ts-expect-error
if (typeof global[typedArray] === 'function') {
// @ts-expect-error
var fakeTypedArray = [];
// @ts-expect-error
fakeTypedArray[Symbol.toStringTag] = typedArray;
// @ts-expect-error
t.notOk(isTypedArray(fakeTypedArray), 'faked ' + typedArray + ' is not typed array');
} else {
t.comment('# SKIP ' + typedArray + ' is not supported');
Expand All @@ -89,9 +94,12 @@ test('non-Typed Arrays', function (t) {
t.end();
});

/** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructor} TypedArrayConstructor */

test('Typed Arrays', function (t) {
forEach(typedArrayNames, function (typedArray) {
var TypedArray = global[typedArray];
// @ts-expect-error
/** @type {TypedArrayConstructor} */ var TypedArray = global[typedArray];
if (isCallable(TypedArray)) {
var arr = new TypedArray(10);
t.ok(isTypedArray(arr), 'new ' + typedArray + '(10) is typed array');
Expand Down
49 changes: 49 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */

/* Projects */

/* Language and Environment */
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
"useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": ["types"], /* Specify multiple folders that act like './node_modules/@types'. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */

/* JavaScript Support */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
"maxNodeModuleJsDepth": 0, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */

/* Emit */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"declarationMap": true, /* Create sourcemaps for d.ts files. */
"noEmit": true, /* Disable emitting files from a compilation. */

/* Interop Constraints */
"allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */

/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */

/* Completeness */
//"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": [
"coverage"
]
}

0 comments on commit 8a8a679

Please sign in to comment.