A simplified way of verifying variable types.
$ npm install simpler-types
1.1.5 - Fixed NaN arrays not working.
Throws an exception anytime the input does not match the type. The input and type can objects or arrays with embedded types within them or a single type.
Type.assert(input, type);
Returns the name of the type provided.
Type.get(input);
Returns the class type of a provided variable.
Type.getType(input);
Returns true/false based on if input matches the type. The input and type can objects or arrays with embedded types within them or a single type.
Type.is(input, type);
const Type = require('simpler-types');
class MyClass {}
const myInstance = new MyClass();
Type.get(myInstance); // "MyClass"
Type.get(0); // "Number"
Type.get(''); // "String"
Type.get(false); // "Boolean"
Type.get(() => {}); // "Function"
Type.get([]); // "Array"
Type.get({}); // "Object"
Type.get(NaN); // "NaN"
Type.get(null); // "null"
Type.get(undefined); // "undefined"
Type.get(Symbol('id')); // "Symbol"
Type.getType(myInstance); // MyClass
Type.getType(0); // Number
Type.getType(''); // String
Type.getType(false); // Boolean
Type.getType(() => {}); // Function
Type.getType([]); // Array
Type.getType({}); // Object
Type.getType(NaN); // NaN
Type.getType(null); // null
Type.getType(undefined); // undefined
Type.getType(Symbol('id')); // Symbol
Type.assert(myInstance, MyClass);
Type.assert(0, Number);
Type.assert('', String);
Type.assert(false, Boolean);
Type.assert(() => {}, Function);
Type.assert([], Array);
Type.assert({}, Object);
Type.assert(NaN, NaN);
Type.assert('', NaN);
Type.assert(null, null);
Type.assert(undefined, undefined);
Type.assert(Symbol('id'), Symbol);
Type.is(myInstance, MyClass); // true
Type.is(0, Number); // true
Type.is('', String); // true
Type.is(false, Boolean); // true
Type.is(() => {}, Function); // true
Type.is([], Array); // true
Type.is({}, Object); // true
Type.is(NaN, NaN); // true
Type.is('', NaN); // true
Type.is(null, null); // true
Type.is(undefined, undefined); // true
Type.is(Symbol('id'), Symbol); // true
Type.assert(input, {
name : String,
age : Number,
address : {
street : String,
zipcode : Number,
state : String,
country : String
}
});
Type.is(input, {
name : String,
age : Number,
address : {
street : String,
zipcode : Number,
state : String,
country : String
}
});
Simpler Types is freely distributable under the terms of the MIT license.