Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/bd43d31ccc0c30789076 to your computer and use it in GitHub Desktop.
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
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;
}
}
@jasdeepkhalsa
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment