From 76303b8a3aa67cca1064f80b238c555c9f00f7d7 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 3 Aug 2023 09:21:14 +0900 Subject: [PATCH] Add documentation for named exports and name default method as detect --- readme.md | 20 +++++++- source/index.ts | 125 ++++++++++++++++++++++++------------------------ 2 files changed, 82 insertions(+), 63 deletions(-) diff --git a/readme.md b/readme.md index 115709d..9be7458 100644 --- a/readme.md +++ b/readme.md @@ -72,11 +72,17 @@ Example: - `'Function'` - `'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')`. ### 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 @@ -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. +## 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 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. diff --git a/source/index.ts b/source/index.ts index a035dc2..0239440 100644 --- a/source/index.ts +++ b/source/index.ts @@ -149,73 +149,73 @@ const getObjectType = (value: unknown): ObjectTypeName | undefined => { return undefined; }; -const isObjectOfType = (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(value: unknown): value is Promise { return isFunction((value as Promise)?.then) && isFunction((value as Promise)?.catch); } const is = Object.assign( - (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'; - }, + detect, { all: isAll, any: isAny, @@ -238,6 +238,7 @@ const is = Object.assign( class_: isClass, dataView: isDataView, date: isDate, + detect, directInstanceOf: isDirectInstanceOf, domElement: isDomElement, emptyArray: isEmptyArray, @@ -380,7 +381,7 @@ export function isBigUint64Array(value: unknown): value is BigUint64Array { } export function isBlob(value: unknown): value is Blob { - return isObjectOfType('Blob')(value); + return getObjectType(value) === 'Blob'; } export function isBoolean(value: unknown): value is boolean {