Path: blob/master/node_modules/@sindresorhus/is/readme.md
2591 views
is
Type check values
For example, is.string('🦄') //=> true

Highlights
Written in TypeScript
Aware of generic type parameters (use with caution)
Actively maintained
Install
Usage
Assertions perform the same type checks, but throw an error if the type does not match.
And with TypeScript:
API
is(value)
Returns the type of value.
Primitives are lowercase and object types are camelcase.
Example:
'undefined''null''string''symbol''Array''Function''Object'
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.
Primitives
.undefined(value)
.null(value)
Note: TypeScript users must use .null_() because of a TypeScript naming limitation.
.string(value)
.number(value)
Note: is.number(NaN) returns false. This intentionally deviates from typeof behavior to increase user-friendliness of is type checks.
.boolean(value)
.symbol(value)
.bigint(value)
Built-in types
.array(value, assertion?)
Returns true if value is an array and all of its items match the assertion (if provided).
.function(value)
Note: TypeScript users must use .function_() because of a TypeScript naming limitation.
.buffer(value)
.blob(value)
.object(value)
Keep in mind that functions are objects too.
.numericString(value)
Returns true for a string that represents a number satisfying is.number, for example, '42' and '-8.3'.
Note: 'NaN' returns false, but 'Infinity' and '-Infinity' return true.
.regExp(value)
.date(value)
.error(value)
.nativePromise(value)
.promise(value)
Returns true for any object with a .then() and .catch() method. Prefer this one over .nativePromise() as you usually want to allow userland promise implementations too.
.generator(value)
Returns true for any object that implements its own .next() and .throw() methods and has a function definition for Symbol.iterator.
.generatorFunction(value)
.asyncFunction(value)
Returns true for any async function that can be called with the await operator.
.asyncGenerator(value)
.asyncGeneratorFunction(value)
.boundFunction(value)
Returns true for any bound function.
.map(value)
.set(value)
.weakMap(value)
.weakSet(value)
Typed arrays
.int8Array(value)
.uint8Array(value)
.uint8ClampedArray(value)
.int16Array(value)
.uint16Array(value)
.int32Array(value)
.uint32Array(value)
.float32Array(value)
.float64Array(value)
.bigInt64Array(value)
.bigUint64Array(value)
Structured data
.arrayBuffer(value)
.sharedArrayBuffer(value)
.dataView(value)
.enumCase(value, enum)
TypeScript-only. Returns true if value is a member of enum.
Emptiness
.emptyString(value)
Returns true if the value is a string and the .length is 0.
.emptyStringOrWhitespace(value)
Returns true if is.emptyString(value) or if it's a string that is all whitespace.
.nonEmptyString(value)
Returns true if the value is a string and the .length is more than 0.
.nonEmptyStringAndNotWhitespace(value)
Returns true if the value is a string that is not empty and not whitespace.
.emptyArray(value)
Returns true if the value is an Array and the .length is 0.
.nonEmptyArray(value)
Returns true if the value is an Array and the .length is more than 0.
.emptyObject(value)
Returns true if the value is an Object and Object.keys(value).length is 0.
Please note that Object.keys returns only own enumerable properties. Hence something like this can happen:
.nonEmptyObject(value)
Returns true if the value is an Object and Object.keys(value).length is more than 0.
.emptySet(value)
Returns true if the value is a Set and the .size is 0.
.nonEmptySet(Value)
Returns true if the value is a Set and the .size is more than 0.
.emptyMap(value)
Returns true if the value is a Map and the .size is 0.
.nonEmptyMap(value)
Returns true if the value is a Map and the .size is more than 0.
Miscellaneous
.directInstanceOf(value, class)
Returns true if value is a direct instance of class.
.urlInstance(value)
Returns true if value is an instance of the URL class.
.urlString(value)
Returns true if value is a URL string.
Note: this only does basic checking using the URL class constructor.
.truthy(value)
Returns true for all values that evaluate to true in a boolean context:
.falsy(value)
Returns true if value is one of: false, 0, '', null, undefined, NaN.
.nan(value)
.nullOrUndefined(value)
.primitive(value)
JavaScript primitives are as follows: null, undefined, string, number, boolean, symbol.
.integer(value)
.safeInteger(value)
Returns true if value is a safe integer.
.plainObject(value)
An object is plain if it's created by either {}, new Object(), or Object.create(null).
.iterable(value)
.asyncIterable(value)
.class(value)
Returns true for instances created by a class.
Note: TypeScript users must use .class_() because of a TypeScript naming limitation.
.typedArray(value)
.arrayLike(value)
A value is array-like if it is not a function and has a value.length that is a safe integer greater than or equal to 0.
.inRange(value, range)
Check if value (number) is in the given range. The range is an array of two values, lower bound and upper bound, in no specific order.
.inRange(value, upperBound)
Check if value (number) is in the range of 0 to upperBound.
.domElement(value)
Returns true if value is a DOM Element.
.nodeStream(value)
Returns true if value is a Node.js stream.
.observable(value)
Returns true if value is an Observable.
.infinite(value)
Check if value is Infinity or -Infinity.
.evenInteger(value)
Returns true if value is an even integer.
.oddInteger(value)
Returns true if value is an odd integer.
.propertyKey(value)
Returns true if value can be used as an object property key (either string, number, or symbol).
.formData(value)
Returns true if value is an instance of the FormData class.
.urlSearchParams(value)
Returns true if value is an instance of the URLSearchParams class.
.any(predicate | predicate[], ...values)
Using a single predicate argument, returns true if any of the input values returns true in the predicate:
Using an array of predicate[], returns true if any of the input values returns true for any of the predicates provided in an array:
.all(predicate, ...values)
Returns true if all of the input values returns true in the predicate:
Type guards
When using is together with TypeScript, type guards are being used extensively to infer the correct type inside if-else statements.
Type assertions
The type guards are also available as type assertions, which throw an error for unexpected types. It is a convenient one-line version of the often repetitive "if-not-expected-type-throw" pattern.
Generic type parameters
The type guards and type assertions are aware of generic type parameters, such as Promise<T> and Map<Key, Value>. The default is unknown for most cases, since is cannot check them at runtime. If the generic type is known at compile-time, either implicitly (inferred) or explicitly (provided), is propagates the type so it can be used later.
Use generic type parameters with caution. They are only checked by the TypeScript compiler, and not checked by is at runtime. This can lead to unexpected behavior, where the generic type is assumed at compile-time, but actually is something completely different at runtime. It is best to use unknown (default) and type-check the value of the generic type parameter at runtime with is or assert.
FAQ
Why yet another type checking module?
There are hundreds of type checking modules on npm, unfortunately, I couldn't find any that fit my needs:
Includes both type methods and ability to get the type
Types of primitives returned as lowercase and object types as camelcase
Covers all built-ins
Unsurprising behavior
Well-maintained
Comprehensive test suite
For the ones I found, pick 3 of these.
The most common mistakes I noticed in these modules was using instanceof for type checking, forgetting that functions are objects, and omitting symbol as a primitive.
Why not just use instanceof instead of this package?
instanceof does not work correctly for all types and it does not work across realms. Examples of realms are iframes, windows, web workers, and the vm module in Node.js.
For enterprise
Available as part of the Tidelift Subscription.
The maintainers of @sindresorhus/is and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
Related
ow - Function argument validation for humans
is-stream - Check if something is a Node.js stream
is-observable - Check if a value is an Observable
file-type - Detect the file type of a Buffer/Uint8Array
is-ip - Check if a string is an IP address
is-array-sorted - Check if an Array is sorted
is-error-constructor - Check if a value is an error constructor
is-empty-iterable - Check if an Iterable is empty
is-blob - Check if a value is a Blob - File-like object of immutable, raw data
has-emoji - Check whether a string has any emoji