type-detect is a lightweight JavaScript library that provides utilities for detecting the type of JavaScript variables. It is designed to be compatible with the latest ECMAScript standards.
If you want to get more detailed type information of a variable, you can use the typeinfo library, which is built on top of type-detect.
You can install type-detect via npm
:
npm install @haixing_hu/type-detect
or via yarn
:
yarn add @haixing_hu/type-detect
The library provides the following functions for type detection:
-
isArguments(value): boolean
: whether the specified value is the JavaScript built-inarguments
object, i.e., an array-like object representing the arguments passed to a function. -
isBigInt(value): boolean
: whether the specified value is a JavaScript built-inbigint
primitive. -
isBoolean(value): boolean
: whether the specified value is a JavaScript built-inboolean
primitive orBoolean
object. -
isBuffer(value): boolean
: whether the specified value is a JavaScript built-inArrayBuffer
orSharedArrayBuffer
object. -
isBuiltInClass(Class): boolean
: whether the specified class is a JavaScript built-in class. -
isCollection(value): boolean
: whether the specified value is a JavaScript built-in collection object, i.e., aMap
orSet
object. Note that theWeakMap
andWeakSet
objects are not considered normal collection objects, and they can be detected by theisWeakCollection(value)
function. -
isConsole(value): boolean
: whether the specified value is a JavaScript built-inconsole
object. -
isCssom(value): boolean
: whether the specified value is a JavaScript built-inCSSOM
object. -
isDataView(value): boolean
: whether the specified value is a JavaScript built-inDataView
object. -
isDom(value): boolean
: whether the specified value is a JavaScript built-in DOM object. -
isError(value): boolean
: whether the specified value is an instance of the JavaScript built-inError
class or its subclass. -
isEvent(value): boolean
: whether the specified value is a JavaScript built-in event object, i.e., an instance of the JavaScript built-inEvent
class or its subclass. -
isFile(value): boolean
: whether the specified value is a JavaScript File API object, i.e., an instance of theFile
,Blob
,FileList
,FileReader
, orFileReaderSync
class. -
isGlobalObject(value): boolean
: whether the specified value is the global object. -
isIntl(value): boolean
: whether the specified value is a JavaScript built-in object under theIntl
namespace. -
isIterator(value): boolean
: whether the specified value is an iterator object, i.e., an object with anext()
method. -
isNumber(value): boolean
: whether the specified value is a JavaScript built-innumber
primitive, or aNumber
object. -
isNumeric(value): boolean
: whether the specified value is a JavaScript built-innumber
primitive, orbigint
primitive, orNumber
object. -
isString(value): boolean
: whether the specified value is a JavaScript built-instring
primitive, orString
object. -
isSymbol(value): boolean
: whether the specified value is a JavaScript built-inSymbol
primitive. -
isTypedArray(value): boolean
: whether the specified value is a JavaScript built-in typed array object. -
isWeak(value): boolean
: whether the specified value is a JavaScript built-in weak referenced object, i.e., aWeakMap
,WeakSet
, orWeakRef
object. -
isWeakCollection(value): boolean
: whether the specified value is a JavaScript built-in weak reference collection object, i.e., aWeakMap
, orWeakSet
object.
The following code shows how to use these functions:
import { isTypedArray } from '@haixing_hu/type-detect';
function foo(value) {
if (isTypedArray(value)) {
...
} else {
...
}
}
This library provides the following constants for feature detection:
-
AGGREGATEERROR_EXISTS
: whether the JavaScript built-in classAggregateError
exists. -
ARRAYBUFFER_EXISTS
: whether the JavaScript built-in classArrayBuffer
exists. -
ARRAY_ISARRAY_EXISTS
: whether the JavaScript built-in functionArray.isArray()
exists. -
ARRAY_ITERATOR_EXISTS
: whether the JavaScript built-in functionArray.prototype[Symbol.iterator]
exists. -
ASYNC_FUNCTION_EXISTS
: whether the JavaScript built-in async function exists. -
ATOMICS_EXISTS
: whether the JavaScript built-in objectAtomics
exists. -
BIGINT64ARRAY_EXISTS
: whether the JavaScript built-in classBigInt64Array
exists. -
BIGINT_EXISTS
: whether the JavaScript built-in primitivebigint
and built-in functionBigInt
exists. -
BIGUINT64ARRAY_EXISTS
: whether the JavaScript built-in classBigUint64Array
exists. -
DATAVIEW_EXISTS
: whether the JavaScript built-in classDataView
exists. -
FINALIZATIONREGISTRY_EXISTS
: whether the JavaScript built-in classFinalizationRegistry
exists. -
FLOAT32ARRAY_EXISTS
: whether the JavaScript built-in classFloat32Array
exists. -
FLOAT64ARRAY_EXISTS
: whether the JavaScript built-in classFloat64Array
exists. -
INT16ARRAY_EXISTS
: whether the JavaScript built-in classInt16Array
exists. -
INT32ARRAY_EXISTS
: whether the JavaScript built-in classInt32Array
exists. -
INT8ARRAY_EXISTS
: whether the JavaScript built-in classInt8Array
exists. -
INTERNALERROR_EXISTS
: whether the JavaScript built-in classInternalError
class exists. -
INTL_COLLATOR_EXISTS
: whether the JavaScript built-in classIntl.Collator
class exists. -
INTL_DATETIMEFORMAT_EXISTS
: whether the JavaScript built-in classIntl.DateTimeFormat
class exists. -
INTL_DISPLAYNAMES_EXISTS
: whether the JavaScript built-in classIntl.DisplayNames
class exists. -
INTL_DURATIONFORMAT_EXISTS
: whether the JavaScript built-in classIntl.DurationFormat
class exists. -
INTL_EXISTS
: whether the JavaScript built-inIntl
namespace exists. -
INTL_LISTFORMAT_EXISTS
: whether the JavaScript built-in classIntl.ListFormat
exists. -
INTL_LOCALE_EXISTS
: whether the JavaScript built-in classIntl.Locale
exists. -
INTL_NUMBERFORMAT_EXISTS
: whether the JavaScript built-in classIntl.NumberFormat
exists. -
INTL_PLURALRULES_EXISTS
: whether the JavaScript built-in classIntl.PluralRules
exists. -
INTL_RELATIVETIMEFORMAT_EXISTS
: whether the JavaScript built-in classIntl.RelativeTimeFormat
exists. -
INTL_SEGMENTER_EXISTS
: whether the JavaScript built-in classIntl.Segmenter
exists. -
INTL_SEGMENTER_ITERATOR_EXISTS
: whether the JavaScript built-in functionIntl.Segmenter.prototype[Symbol.iterator]
exists. -
MAP_ENTRIES_EXISTS
: whether the JavaScript built-in functionMap.prototype.entries()
exists. -
MAP_EXISTS
: whether the JavaScript built-in classMap
exists. -
MAP_ITERATOR_EXISTS
: whether the JavaScript built-in functionMap.prototype[Symbol.iterator]
exists. -
PROMISE_EXISTS
: whether the JavaScript built-in classPromise
exists. -
PROXY_EXISTS
: whether the JavaScript built-in classProxy
exists. -
REFLECT_EXISTS
: whether the JavaScript built-in namespaceReflect
exists. -
REGEXP_EXISTS
: whether the JavaScript built-in classRegExp
exists. -
REGEXP_ITERATOR_EXISTS
: whether the JavaScript built-in functionRegExp.prototype[Symbol.matchAll]
exists. -
SET_ENTRIES_EXISTS
: whether the JavaScript built-in functionSet.prototype.entries()
exists. -
SET_EXISTS
: whether the JavaScript built-in classSet
exists. -
SET_ITERATOR_EXISTS
: whether the JavaScript built-in functionSet.prototype[Symbol.iterator]
exists. -
SHAREDARRAYBUFFER_EXISTS
: whether the JavaScript built-in classSharedArrayBuffer
exists. -
STRING_ITERATOR_EXISTS
: whether the JavaScript built-in functionString.prototype[Symbol.iterator]
exists. -
SYMBOL_EXISTS
: whether the JavaScript built-inSymbol
exists. -
SYMBOL_ITERATOR_EXISTS
: whether the JavaScript built-in functionSymbol.prototype[Symbol.iterator]
exists. -
SYMBOL_MATCH_ALL_EXISTS
: whether the JavaScript built-in functionSymbol.prototype[Symbol.matchAll]
exists. -
SYMBOL_TO_STRING_TAG_EXISTS
: whether the JavaScript built-in functionSymbol.prototype[Symbol.toStringTag]
exists. -
UINT16ARRAY_EXISTS
: whether the JavaScript built-in classUint16Array
exists. -
UINT32ARRAY_EXISTS
: whether the JavaScript built-in classUint32Array
exists. -
UINT8ARRAY_EXISTS
: whether the JavaScript built-in classUint8Array
exists. -
UINT8CLAMPEDARRAY_EXISTS
: whether the JavaScript built-in classUint8ClampedArray
exists. -
WEAKMAP_EXISTS
: whether the JavaScript built-in classWeakMap
exists. -
WEAKREF_EXISTS
: whether the JavaScript built-in classWeakRef
exists. -
WEAKSET_EXISTS
: whether the JavaScript built-in classWeakSet
exists.
The following code shows how to use these constants:
import { WEAKMAP_EXISTS } from '@haixing_hu/type-detect';
function foo(value) {
if (WEAKMAP_EXISTS) {
...
} else {
...
}
}
This library provides the following constants for the prototypes of JavaScript built-in objects:
-
AggregateErrorPrototype
: the prototype of the JavaScript built-inAggregateError
objects, orundefined
if theAggregateError
class does not exist. -
ArrayBufferPrototype
: the prototype of the JavaScript built-inArrayBuffer
objects, orundefined
if theArrayBuffer
class does not exist. -
ArrayIteratorPrototype
: the prototype of the JavaScript built-in array iterator objects, orundefined
if the array iterator does not exist. -
BigInt64ArrayPrototype
: the prototype of the JavaScript built-inBigInt64Array
objects, orundefined
if theBigInt64Array
class does not exist. -
BigIntPrototype
: the prototype of the JavaScript built-inbigint
primitive, orundefined
if thebigint
primitive does not exist. -
BigUint64ArrayPrototype
: the prototype of the JavaScript built-inBigUint64Array
objects, orundefined
if theBigUint64Array
class does not exist. -
DataViewPrototype
: the prototype of the JavaScript built-inDataView
objects, orundefined
if theDataView
class does not exist. -
FinalizationRegistryPrototype
: the prototype of the JavaScript built-inFinalizationRegistry
objects, orundefined
if theFinalizationRegistry
class does not exist. -
Float32ArrayPrototype
: the prototype of the JavaScript built-inFloat32Array
objects, orundefined
if theFloat32Array
class does not exist. -
Float64ArrayPrototype
: the prototype of the JavaScript built-inFloat64Array
objects, orundefined
if theFloat64Array
class does not exist. -
Int16ArrayPrototype
: the prototype of the JavaScript built-inInt16Array
objects, orundefined
if theInt16Array
class does not exist. -
Int32ArrayPrototype
: the prototype of the JavaScript built-inInt32Array
objects, orundefined
if theInt32Array
class does not exist. -
Int8ArrayPrototype
: the prototype of the JavaScript built-inInt8Array
objects, orundefined
if theInt8Array
class does not exist. -
IntelSegmentIteratorPrototype
: the prototype of the JavaScript built-inIntl.SegmentIterator
objects, orundefined
if theIntl.SegmentIterator
class does not exist. -
InternalErrorPrototype
: the prototype of the JavaScript built-inInternalError
objects, orundefined
if theInternalError
class does not exist. -
IntlCollatorPrototype
: the prototype of the JavaScript built-inIntl.Collator
objects, orundefined
if theIntl.Collator
class does not exist. -
IntlDateTimeFormatPrototype
: the prototype of the JavaScript built-inIntl.DateTimeFormat
objects, orundefined
if theIntl.DateTimeFormat
class does not exist. -
IntlDisplayNamesPrototype
: the prototype of the JavaScript built-inIntl.DisplayNames
objects, orundefined
if theIntl.DisplayNames
class does not exist. -
IntlDurationFormatPrototype
: the prototype of the JavaScript built-inIntl.DurationFormat
objects, orundefined
if theIntl.DurationFormat
class does not exist. -
IntlListFormatPrototype
: the prototype of the JavaScript built-inIntl.ListFormat
objects, orundefined
if theIntl.ListFormat
class does not exist. -
IntlLocalePrototype
: the prototype of the JavaScript built-inIntl.Locale
objects, orundefined
if theIntl.Locale
class does not exist. -
IntlNumberFormatPrototype
: the prototype of the JavaScript built-inIntl.NumberFormat
objects, orundefined
if theIntl.NumberFormat
class does not exist. -
IntlPluralRulesPrototype
: the prototype of the JavaScript built-inIntl.PluralRules
objects, orundefined
if theIntl.PluralRules
class does not exist. -
IntlRelativeTimeFormatPrototype
: the prototype of the JavaScript built-inIntl.RelativeTimeFormat
objects, orundefined
if theIntl.RelativeTimeFormat
class does not exist. -
IntlSegmenterPrototype
: the prototype of the JavaScript built-inIntl.Segmenter
objects, orundefined
if theIntl.Segmenter
class does not exist. -
MapIteratorPrototype
: the prototype of the JavaScript built-inMap
iterator objects, orundefined
if theMap
iterator does not exist. -
MapPrototype
: the prototype of the JavaScript built-inMap
objects, orundefined
if theMap
class does not exist. -
PromisePrototype
: the prototype of the JavaScript built-inPromise
objects, orundefined
if thePromise
class does not exist. -
RegExpIteratorPrototype
: the prototype of the JavaScript built-inRegExp
iterator objects, orundefined
if theRegExp
iterator does not exist. -
RegExpPrototype
: the prototype of the JavaScript built-inRegExp
objects, orundefined
if theRegExp
class does not exist. -
SetIteratorPrototype
: the prototype of the JavaScript built-inSet
iterator objects, orundefined
if theSet
iterator does not exist. -
SetPrototype
: the prototype of the JavaScript built-inSet
objects, orundefined
if theSet
class does not exist. -
SharedArrayBufferPrototype
: the prototype of the JavaScript built-inSharedArrayBuffer
objects, orundefined
if theSharedArrayBuffer
class does not exist. -
StringIteratorPrototype
: the prototype of the JavaScript built-inString
iterator objects, orundefined
if theString
iterator does not exist. -
SymbolPrototype
: the prototype of the JavaScript built-inSymbol
primitives, orundefined
if theSymbol
primitive does not exist. -
Uint16ArrayPrototype
: the prototype of the JavaScript built-inUint16Array
objects, orundefined
if theUint16Array
class does not exist. -
Uint32ArrayPrototype
: the prototype of the JavaScript built-inUint32Array
objects, orundefined
if theUint32Array
class does not exist. -
Uint8ArrayPrototype
: the prototype of the JavaScript built-inUint8Array
objects, orundefined
if theUint8Array
class does not exist. -
Uint8ClampedArrayPrototype
: the prototype of the JavaScript built-inUint8ClampedArray
objects, orundefined
if theUint8ClampedArray
class does not exist. -
WeakMapPrototype
: the prototype of the JavaScript built-inWeakMap
objects, orundefined
if theWeakMap
class does not exist. -
WeakRefPrototype
: the prototype of the JavaScript built-inWeakRef
objects, orundefined
if theWeakRef
class does not exist. -
WeakSetPrototype
: the prototype of the JavaScript built-inWeakSet
objects, orundefined
if theWeakSet
class does not exist.
The following code shows how to use these constants:
import { WeakMapPrototype } from '@haixing_hu/type-detect';
function foo(value) {
const proto = Object.getPrototypeOf(value);
if (proto === WeakMapPrototype) {
...
} else {
...
}
}
One of the primary purposes of Proxy
objects in JavaScript is to allow developers
to customize the behavior of object operations, acting as a delegate for another
object (referred to as the target object). One of the key features of Proxy
is
its transparency—externally, unless the proxy object is intentionally designed
to reveal itself, it is challenging to distinguish a Proxy
object from the
target object it represents. This is largely because Proxy
can intercept and
redefine almost all fundamental operations of an object, including but not
limited to property access, assignment, and enumeration.
Therefore, when libraries like type-detect attempt to retrieve type information
of an object, the inherent transparency of Proxy
means these libraries can
only process and "see" the final outcomes of operations, without direct means
to identify whether these operations were intercepted by a Proxy
. If a
Proxy
flawlessly mimics the behavior of its target object, there exists no
reliable method to determine from the operation outcomes whether an object is
a Proxy
. In essence, the design philosophy of Proxy
aims to make it nearly
invisible to external observation, making it impossible for even libraries
specialized in fetching type information to definitively ascertain if an object
is a Proxy
, unless the proxy object deliberately exposes its identity through
certain intercepting behaviors. This design significantly enhances the power
and flexibility of Proxy
, but it also means that directly detecting Proxy
objects through external observation presents a challenge.
If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request to the GitHub repository.
type-detect is distributed under the Apache 2.0 license. See the LICENSE file for more details.