Created
June 30, 2013 10:35
-
-
Save sebmck/5894672 to your computer and use it in GitHub Desktop.
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
/** | |
* @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