In Javascript, "Everything is (or acts like) an Object" We need to write "typeis.js" due to a problem (type check) arised other than except the conveniences it is a providing to us.
Value | Native | Typeis.js |
---|---|---|
Undeclared variables | undefined | --- |
undefined | undefined | --- |
null | object | --- |
Booleans | boolean | Boolean |
Numbers | number | Number |
String | string | String |
Functions | function | Function |
Array | object | Array |
Object | object | Object |
Date | object | Date |
RegExp | object | RegExp |
Everything in JavaScript acts like an object, with the only two exceptions being null and undefined. *JavaScript Garden Typeis.js acts like an property; doesn't work null and undefined types because they doesn't have properties.
npm install typeis
bower install typeis
In Node.js:
require('typeis');
In a browser:
<script src="typeis.js"></script>
variable.typeis();
// return Array, Object, RegExp, Date etc.
Multi type validation
typeis(variable, ['array', 'object']);
// if variable is Array or Object return true otherwise false
Type valition with regex
typeis(variable, 'array|object');
// if variable is Array or object return true otherwise false
typeis(variable, '.+[yep]$');
// if variable type end of "y", "e" and "p" like Array, Date, RegExp return true otherwise false
typeis(variable, '(^(?!array|object).+)[^n]$');
// if variable is Array, Object or ending "n" like Function and Boolean return false otherwise true
function realWorld( options ){
if(typeis(options, 'object')){
//do something
} else {
//do another something
}
}
IE9 and below also support all modern browser.
Property usage warning
Update readme
Multi type validation support
variable.typeis(['array', 'object']);
// if variable is Array or Object return true otherwise false
type validation with regex support
variable.typeis('array|object');
// if variable is Array or object return true otherwise false
Typeis.js Released