diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index c1870cf..d50ada6 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
+ - 18
+ - 16
- 14
- - 12
- - 10
steps:
- - uses: actions/checkout@v2
- - uses: actions/setup-node@v1
+ - uses: actions/checkout@v3
+ - uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
diff --git a/package.json b/package.json
index 8052a87..34f31f6 100644
--- a/package.json
+++ b/package.json
@@ -10,9 +10,11 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
- "main": "dist/index.js",
+ "type": "module",
+ "exports": "./index.js",
+ "types": "./index.d.ts",
"engines": {
- "node": ">=10"
+ "node": ">=14.16"
},
"scripts": {
"build": "del dist && tsc",
@@ -47,50 +49,34 @@
"types"
],
"devDependencies": {
- "@sindresorhus/tsconfig": "^0.7.0",
- "@types/jsdom": "^16.1.0",
- "@types/node": "^14.0.13",
- "@types/zen-observable": "^0.8.0",
- "@typescript-eslint/eslint-plugin": "^2.20.0",
- "@typescript-eslint/parser": "^2.20.0",
- "ava": "^3.3.0",
- "del-cli": "^2.0.0",
- "eslint-config-xo-typescript": "^0.26.0",
- "jsdom": "^16.0.1",
- "rxjs": "^6.4.0",
- "tempy": "^0.4.0",
- "ts-node": "^8.3.0",
- "typescript": "~3.8.2",
- "xo": "^0.26.1",
- "zen-observable": "^0.8.8"
+ "@sindresorhus/tsconfig": "^3.0.1",
+ "@types/jsdom": "^16.2.14",
+ "@types/node": "^17.0.42",
+ "@types/zen-observable": "^0.8.3",
+ "ava": "^4.3.0",
+ "del-cli": "^4.0.1",
+ "jsdom": "^19.0.0",
+ "rxjs": "^7.5.5",
+ "tempy": "^3.0.0",
+ "ts-node": "^10.8.1",
+ "typescript": "~4.7.3",
+ "xo": "^0.50.0",
+ "zen-observable": "^0.8.15"
},
- "types": "dist/index.d.ts",
"sideEffects": false,
"ava": {
- "extensions": [
- "ts"
- ],
- "require": [
- "ts-node/register"
+ "extensions": {
+ "ts": "module"
+ },
+ "nodeArguments": [
+ "--loader=ts-node/esm"
]
},
"xo": {
- "extends": "xo-typescript",
- "extensions": [
- "ts"
- ],
- "parserOptions": {
- "project": "./tsconfig.xo.json"
- },
- "globals": [
- "BigInt",
- "BigInt64Array",
- "BigUint64Array"
- ],
"rules": {
- "@typescript-eslint/promise-function-async": "off",
"@typescript-eslint/no-empty-function": "off",
- "@typescript-eslint/explicit-function-return-type": "off"
+ "@typescript-eslint/explicit-function-return-type": "off",
+ "@typescript-eslint/triple-slash-reference": "off"
}
}
}
diff --git a/readme.md b/readme.md
index 05a291a..90ac932 100644
--- a/readme.md
+++ b/readme.md
@@ -24,7 +24,7 @@ npm install @sindresorhus/is
## Usage
```js
-const is = require('@sindresorhus/is');
+import is from '@sindresorhus/is';
is('🦄');
//=> 'string'
@@ -39,7 +39,7 @@ is.number(6);
[Assertions](#type-assertions) perform the same type checks, but throw an error if the type does not match.
```js
-const {assert} = require('@sindresorhus/is');
+import {assert} from '@sindresorhus/is';
assert.string(2);
//=> Error: Expected value which is `string`, received value of type `number`.
@@ -436,7 +436,7 @@ Returns `true` if `value` is a DOM Element.
Returns `true` if `value` is a Node.js [stream](https://nodejs.org/api/stream.html).
```js
-const fs = require('fs');
+import fs from 'node:fs';
is.nodeStream(fs.createReadStream('unicorn.png'));
//=> true
@@ -447,7 +447,7 @@ is.nodeStream(fs.createReadStream('unicorn.png'));
Returns `true` if `value` is an `Observable`.
```js
-const {Observable} = require('rxjs');
+import {Observable} from 'rxjs';
is.observable(new Observable());
//=> true
diff --git a/source/index.ts b/source/index.ts
index 39e5883..6457799 100644
--- a/source/index.ts
+++ b/source/index.ts
@@ -1,8 +1,5 @@
-///
-///
-///
-
-import {Class, Falsy, TypedArray, ObservableLike, Primitive} from './types';
+import type {Buffer} from 'node:buffer';
+import type {Class, Falsy, TypedArray, ObservableLike, Primitive} from './types.js';
const typedArrayTypeNames = [
'Int8Array',
@@ -15,7 +12,7 @@ const typedArrayTypeNames = [
'Float32Array',
'Float64Array',
'BigInt64Array',
- 'BigUint64Array'
+ 'BigUint64Array',
] as const;
type TypedArrayTypeName = typeof typedArrayTypeNames[number];
@@ -52,7 +49,7 @@ const objectTypeNames = [
'URLSearchParams',
'HTMLElement',
'NaN',
- ...typedArrayTypeNames
+ ...typedArrayTypeNames,
] as const;
type ObjectTypeName = typeof objectTypeNames[number];
@@ -68,7 +65,7 @@ const primitiveTypeNames = [
'number',
'bigint',
'boolean',
- 'symbol'
+ 'symbol',
] as const;
type PrimitiveTypeName = typeof primitiveTypeNames[number];
@@ -149,6 +146,7 @@ function is(value: unknown): TypeName {
}
is.undefined = isOfType('undefined');
+
is.string = isOfType('string');
const isNumberType = isOfType('number');
@@ -159,9 +157,13 @@ is.bigint = isOfType('bigint');
// eslint-disable-next-line @typescript-eslint/ban-types
is.function_ = isOfType('function');
+// eslint-disable-next-line @typescript-eslint/ban-types
is.null_ = (value: unknown): value is null => value === null;
+
is.class_ = (value: unknown): value is Class => is.function_(value) && value.toString().startsWith('class ');
+
is.boolean = (value: unknown): value is boolean => value === true || value === false;
+
is.symbol = isOfType('symbol');
is.numericString = (value: unknown): value is string =>
@@ -176,14 +178,18 @@ is.array = (value: unknown, assertion?: (value: T) => value is T):
return true;
}
- return value.every(assertion);
+ return value.every(element => assertion(element));
};
+// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
is.buffer = (value: unknown): value is Buffer => (value as any)?.constructor?.isBuffer?.(value) ?? false;
+
is.blob = (value: unknown): value is Blob => isObjectOfType('Blob')(value);
-is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value);
-is.object = (value: unknown): value is object => !is.null_(value) && (typeof value === 'object' || is.function_(value));
+is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value); // eslint-disable-line @typescript-eslint/ban-types
+
+is.object = (value: unknown): value is object => !is.null_(value) && (typeof value === 'object' || is.function_(value)); // eslint-disable-line @typescript-eslint/ban-types
+
is.iterable = (value: unknown): value is Iterable => is.function_((value as Iterable)?.[Symbol.iterator]);
is.asyncIterable = (value: unknown): value is AsyncIterable => is.function_((value as AsyncIterable)?.[Symbol.asyncIterator]);
@@ -195,11 +201,11 @@ is.asyncGenerator = (value: unknown): value is AsyncGenerator => is.asyncIterabl
is.nativePromise = (value: unknown): value is Promise =>
isObjectOfType>('Promise')(value);
-const hasPromiseAPI = (value: unknown): value is Promise =>
- is.function_((value as Promise)?.then) &&
- is.function_((value as Promise)?.catch);
+const hasPromiseApi = (value: unknown): value is Promise =>
+ is.function_((value as Promise)?.then)
+ && is.function_((value as Promise)?.catch);
-is.promise = (value: unknown): value is Promise => is.nativePromise(value) || hasPromiseAPI(value);
+is.promise = (value: unknown): value is Promise => is.nativePromise(value) || hasPromiseApi(value);
is.generatorFunction = isObjectOfType('GeneratorFunction');
@@ -211,12 +217,18 @@ is.asyncFunction = (value: unknown): value is ((...args: any[]) =>
is.boundFunction = (value: unknown): value is Function => is.function_(value) && !value.hasOwnProperty('prototype');
is.regExp = isObjectOfType('RegExp');
+
is.date = isObjectOfType('Date');
+
is.error = isObjectOfType('Error');
+
is.map = (value: unknown): value is Map => isObjectOfType