Created
January 15, 2013 16:41
-
-
Save Gisleburt/4539985 to your computer and use it in GitHub Desktop.
Just some useful JavaScript functions
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
/** | |
* Useful functions | |
*/ | |
var Tools = function() {}; | |
/** | |
* Checks a value is not null or undefined | |
* @param test | |
* @return {Boolean} | |
*/ | |
Tools.isSet = function(test) { | |
return (test === null || typeof test != undefined); | |
}; | |
/** | |
* Checks if the value is a valid number | |
* @param n {*} | |
* @return {Boolean} | |
*/ | |
Tools.isNumber = function(n) { | |
return !isNaN(parseFloat(n)) && isFinite(n); | |
}; | |
/** | |
* Handles errors by chucking them at the log | |
* @param error {*} | |
* @param context {*} | |
*/ | |
Tools.handleError = function(error, context) { | |
console.log('An error occured'); | |
console.log(error); | |
console.log(context); | |
} | |
/** | |
* Returns current value if set (or not Empty if notEmpty is true) or default value otherwise. | |
* @param currentValue {*} The value to check | |
* @param defaultValue {*} The value to replace it with | |
* @param notEmpty {boolean} Set to true if the value should not be empty | |
* @return {*} | |
*/ | |
Tools.default = function(currentValue, defaultValue, notEmpty) { | |
if(!Tools.isSet(currentValue)) | |
currentValue = defaultValue; | |
if(Tools.isSet(notEmpty) && !Tools.isSet(currentValue)) | |
currentValue = defaultValue; | |
return currentValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment