Type detection beyond `typeof`
npm install whats-the-typetypeof returns [object Object] for almost everything beyond primitives. This module works around this drawback and returns a more accurate type.
getType() now returns values in a case sensitive manner. Using lower case in v1 was meant to be more consistent with the behavior of typeof, but it made it impossible to distinguish between SomeObject and someobject.
getType({ foo: 'bar' }) now returns PlainObject rather than object.
getType(function* ()) now returns GeneratorFunction instead of generator.
isGeneratorFunction() replaces isGenerator().
getType() now works around the issue with Function.name and returns the object type correctly. It will fall back to Object in the case of failure.
is() functions only, your code will keep on working without any changes, except for the now fixed Function.name issue [5]. As for getType() you'll need to update your code with regards to the case sensitivity [1].
is() functions; check out the complete list under _Usage_.
getType().
bash
npm install whats-the-type
`
Usage
_What's the type_ comes with one generic detector, getType() and many is functions:
- isArray()
- isAsyncFunction()
- isBigInt()
- isBlob()
- isBoolean()
- isCallable()
- isCustomElement()
- isDate()
- isElement()
- isElementCollection()
- isEmptyObject()
- isError()
- isFunction()
- isGeneratorFunction()
- isHtmlElement()
- isMap()
- isMathElement()
- isNull()
- isNumber()
- isPlainObject()
- isPromise()
- isRegExp()
- isSet()
- isString()
- isSvgElement()
- isSymbol()
- isUndefined()
- isWeakMap()
- isWeakSet()
$3
You can import all functionality at once if you want to, but this means importing loads of things you might not need. Sure, treeshaking can make up for this but you can control things as well, the choice is yours.
`javascript
// Import 'whats-the-type' if you have a bunch of different types to check. This imports all functions at once.
import detector from 'whats-the-type'; // note that whats-the-type is implemented as ESM and not in CJS
// use getType if you don't know what type to expect
import getType from 'whats-the-type/getType.js';
// use the is functions if you want to confirm if a value is of a specific type
import isString from 'whats-the-type/isString.js';
import isPlainObject from 'whats-the-type/isPlainObject.js';
`
$3
getType() returns a string such as Null, Undefined, Function, String, etc. These values mostly correspond to the value's constructor name. In other words, the function doesn't simply return object for most types but is as precise as possible. If you are reading this at NPM, head over to the full documentation to find reference tables with just about any possible return value.
`javascript
// the detector always returns a string
getType(['a', 'b', 'c']); // 'Array'
// use the import name (e.g. 'detector') if you imported everything
detector.getType(123); // 'Number'
// here are some more examples but getType() isn't limited to these
const v1 = null;
getType(v1); // "Null"
let v2;
getType(v2); // "Undefined"
const v3 = function() {};
getType(v3); // "Function", use isCallable() to cover async functions and generators as well
const v4 = async function() {};
getType(v4); // "AsyncFunction"
const v5 = function* generator(i) {};
getType(v5); // "GeneratorFunction"
const v6 = { foo: "bar" };
getType(v6); // "PlainObject", not "Object", as you may have expected
const v7 = new Map();
getType(v7); // "Map"
const v8 = new Set();
getType(v8); // "Set"
const v9 = new WeakMap();
getType(v9); // "WeakMap"
const v10 = new WeakSet();
getType(v10); // "WeakSet"
const v11 = new Date();
getType(v11); // "Date"
const v12 = new Error();
getType(v12); // "Error"
const v13 = new Promise(() => {});
getType(v13); // "Promise"
class CustomClass {}
const v14 = new CustomClass();
getType(v14); // "CustomClass"
getType(class CustomClass {}) // "Class", note the difference: CustomClass has not been initiated yet
`
$3
is functions return true if the value is of the specified type. They exist for the most common types and are explained in the docs.
`javascript
isString('a'); // true
isString(123); // false
isPlainObject({ a: 1 }); // true
isMap(new Map()); // true
isCallable(() => {}); // true
isAsyncFunction(async () => {}); // true
isGeneratorFunction(function* () {}); // true
isWeakMap(new WeakMap()); // true
isWeakSet(new WeakSet()); // true
``