Last active
August 29, 2015 14:07
-
-
Save jasdeepkhalsa/bd43d31ccc0c30789076 to your computer and use it in GitHub Desktop.
Helper functions to assess if a string is valid or invalid, and if it matches a particular type or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ifValid(str) | |
{ | |
if(str !== "" && str !== 0 && str !== '0' && str !== false && str !== 'false' && str !== 'undefined' && str !== undefined && str !== null) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
function ifInvalid(str) | |
{ | |
if(str === "" || str === 0 || str === '0' || str === false || str === 'false' || str === 'undefined' || str === undefined || str === null) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
// Requires checkType fn | |
function ifType(obj, type) | |
{ | |
var obj = obj, | |
type = type, | |
check = checkType; | |
if(check(obj) === type) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
// Requires checkType fn | |
function ifNotType(obj, type) | |
{ | |
var obj = obj, | |
type = type, | |
check = checkType; | |
if(check(obj) !== type) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
function checkType(obj) | |
{ | |
var type = Object.prototype.toString.call(obj); | |
switch(type) | |
{ | |
case '[object Number]': | |
return 'number'; | |
case '[object String]': | |
return 'string'; | |
case '[object Boolean]': | |
return 'boolean'; | |
case '[object RegExp]': | |
return 'regexp'; | |
case '[object Function]': | |
return 'function'; | |
case '[object Array]': | |
return 'array'; | |
case '[object Object]': | |
return 'object'; | |
case '[object Date]': | |
return 'date'; | |
case '[object Math]': | |
return 'math'; | |
case '[object Null]': | |
return 'null'; | |
case '[object Undefined]': | |
return 'undefined'; | |
case '[object global]': | |
return 'global'; | |
default: | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please do not use directly in production, modify and then use - as the ifValid and ifInvalid functions are opinionated to suit a particular use case