Add documentation for named exports and name default method as detect
This commit is contained in:
parent
66943d06b3
commit
76303b8a3a
2 changed files with 82 additions and 63 deletions
20
readme.md
20
readme.md
|
|
@ -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.
|
||||||
|
|
|
||||||
125
source/index.ts
125
source/index.ts
|
|
@ -149,73 +149,73 @@ 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 {
|
||||||
|
if (value === null) {
|
||||||
|
return 'null';
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (typeof value) {
|
||||||
|
case 'undefined': {
|
||||||
|
return 'undefined';
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'string': {
|
||||||
|
return 'string';
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'number': {
|
||||||
|
return Number.isNaN(value) ? 'NaN' : 'number';
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'boolean': {
|
||||||
|
return 'boolean';
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'function': {
|
||||||
|
return 'Function';
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'bigint': {
|
||||||
|
return 'bigint';
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'symbol': {
|
||||||
|
return 'symbol';
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isObservable(value)) {
|
||||||
|
return 'Observable';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isArray(value)) {
|
||||||
|
return 'Array';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBuffer(value)) {
|
||||||
|
return 'Buffer';
|
||||||
|
}
|
||||||
|
|
||||||
|
const tagType = getObjectType(value);
|
||||||
|
if (tagType) {
|
||||||
|
return tagType;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value instanceof String || value instanceof Boolean || value instanceof Number) {
|
||||||
|
throw new TypeError('Please don\'t use object wrappers for primitive types');
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'Object';
|
||||||
|
}
|
||||||
|
|
||||||
function hasPromiseApi<T = unknown>(value: unknown): value is Promise<T> {
|
function hasPromiseApi<T = unknown>(value: unknown): value is Promise<T> {
|
||||||
return isFunction((value as Promise<T>)?.then) && isFunction((value as Promise<T>)?.catch);
|
return isFunction((value as Promise<T>)?.then) && isFunction((value as Promise<T>)?.catch);
|
||||||
}
|
}
|
||||||
|
|
||||||
const is = Object.assign(
|
const is = Object.assign(
|
||||||
(value: unknown): TypeName => {
|
detect,
|
||||||
if (value === null) {
|
|
||||||
return 'null';
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (typeof value) {
|
|
||||||
case 'undefined': {
|
|
||||||
return 'undefined';
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'string': {
|
|
||||||
return 'string';
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'number': {
|
|
||||||
return Number.isNaN(value) ? 'NaN' : 'number';
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'boolean': {
|
|
||||||
return 'boolean';
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'function': {
|
|
||||||
return 'Function';
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'bigint': {
|
|
||||||
return 'bigint';
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'symbol': {
|
|
||||||
return 'symbol';
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isObservable(value)) {
|
|
||||||
return 'Observable';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isArray(value)) {
|
|
||||||
return 'Array';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isBuffer(value)) {
|
|
||||||
return 'Buffer';
|
|
||||||
}
|
|
||||||
|
|
||||||
const tagType = getObjectType(value);
|
|
||||||
if (tagType) {
|
|
||||||
return tagType;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value instanceof String || value instanceof Boolean || value instanceof Number) {
|
|
||||||
throw new TypeError('Please don\'t use object wrappers for primitive types');
|
|
||||||
}
|
|
||||||
|
|
||||||
return 'Object';
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
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 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue