Skip to content

Instantly share code, notes, and snippets.

@sebmck
Created June 30, 2013 10:35
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 sebmck/5894672 to your computer and use it in GitHub Desktop.
Save sebmck/5894672 to your computer and use it in GitHub Desktop.
/**
* @file Functions relating to type validation.
*/
module.exports = function(_){
/**
* Check if value is an object, if not return an empty object
*
* @param {Object} obj
* @returns {Object}
*/
_.Object = function(obj){
return _.isObject(obj) ? obj : {};
};
/**
* Check if value is an array, if not return an empty array
*
* @param {Array} arr
* @returns {Array}
*/
_.Array = function(arr){
return _.isArray(arr) ? arr : [];
};
/**
* Check if value is a string, if not return an empty string
*
* @param {String} str
* @returns {String}
*/
_.String = function(str){
return String(str) || "";
};
/**
* Check if value is a number, if not then return `0`
*
* @param {Number} num
* @returns {Number}
*/
_.Number = function(num){
return Number(num) || 0;
};
/**
* Check if value is boolean, if not return `true`
*
* @param {Boolean} bool
* @returns {Boolean}
*/
_.Boolean = function(bool){
return _.isBoolean(bool) ? bool : true;
};
/**
* Description
*
* @param {Function} fn
* @returns {Function}
*/
_.Function = function(fn){
return _.isFunction(fn) ? fn : Function();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment