Add documentation for named exports and name default method as detect

This commit is contained in:
= 2023-08-03 09:21:14 +09:00
parent 66943d06b3
commit 76303b8a3a
2 changed files with 82 additions and 63 deletions

View file

@ -72,11 +72,17 @@ Example:
- `'Function'` - `'Function'`
- `'Object'` - `'Object'`
This method is also exported as `detect`, you can import it like this:
```js
import {detect} from '@sindresorhus/is';
```
Note: It will throw an error if you try to feed it object-wrapped primitives, as that's a bad practice. For example `new String('foo')`. Note: It will throw an error if you try to feed it object-wrapped primitives, as that's a bad practice. For example `new String('foo')`.
### is.{method} ### is.{method}
All the below methods accept a value and returns a boolean for whether the value is of the desired type. All the below methods accept a value and return a boolean for whether the value is of the desired type.
#### Primitives #### Primitives
@ -566,6 +572,18 @@ This can be useful to confirm that a value is a valid count of something, ie. 0
Returns `true` if the value is a string with only whitespace characters. Returns `true` if the value is a string with only whitespace characters.
## Named Exports
Named exports allow tooling to perform tree-shaking, potentially reducing bundle size by including only code from the methods that are used.
Every method listed above is available as a named export. Each method is prefixed by either `is` or `assert` depending on usage.
For example:
```js
import {assertNull, isUndefined} from '@sindresorhus/is';
```
## Type guards ## Type guards
When using `is` together with TypeScript, [type guards](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) are being used extensively to infer the correct type inside if-else statements. When using `is` together with TypeScript, [type guards](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) are being used extensively to infer the correct type inside if-else statements.

View file

@ -149,14 +149,7 @@ const getObjectType = (value: unknown): ObjectTypeName | undefined => {
return undefined; return undefined;
}; };
const isObjectOfType = <T>(type: ObjectTypeName) => (value: unknown): value is T => getObjectType(value) === type; function detect(value: unknown): TypeName {
function hasPromiseApi<T = unknown>(value: unknown): value is Promise<T> {
return isFunction((value as Promise<T>)?.then) && isFunction((value as Promise<T>)?.catch);
}
const is = Object.assign(
(value: unknown): TypeName => {
if (value === null) { if (value === null) {
return 'null'; return 'null';
} }
@ -215,7 +208,14 @@ const is = Object.assign(
} }
return 'Object'; return 'Object';
}, }
function hasPromiseApi<T = unknown>(value: unknown): value is Promise<T> {
return isFunction((value as Promise<T>)?.then) && isFunction((value as Promise<T>)?.catch);
}
const is = Object.assign(
detect,
{ {
all: isAll, all: isAll,
any: isAny, any: isAny,
@ -238,6 +238,7 @@ const is = Object.assign(
class_: isClass, class_: isClass,
dataView: isDataView, dataView: isDataView,
date: isDate, date: isDate,
detect,
directInstanceOf: isDirectInstanceOf, directInstanceOf: isDirectInstanceOf,
domElement: isDomElement, domElement: isDomElement,
emptyArray: isEmptyArray, emptyArray: isEmptyArray,
@ -380,7 +381,7 @@ export function isBigUint64Array(value: unknown): value is BigUint64Array {
} }
export function isBlob(value: unknown): value is Blob { export function isBlob(value: unknown): value is Blob {
return isObjectOfType<Blob>('Blob')(value); return getObjectType(value) === 'Blob';
} }
export function isBoolean(value: unknown): value is boolean { export function isBoolean(value: unknown): value is boolean {