Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Last active December 18, 2015 00:09
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 kentcdodds/5694667 to your computer and use it in GitHub Desktop.
Save kentcdodds/5694667 to your computer and use it in GitHub Desktop.
Utility functions. Simple functions I don't want to forget.
var capitaliseFirstLetter = function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
/*
* Detect IE. From http://stackoverflow.com/a/16657946
* ----------------------------------------------------------
* If you're not in IE (or IE version is less than 5) then:
* ie === undefined
* If you're in IE (>=5) then you can determine which version:
* ie === 7; // IE7
* Thus, to detect IE:
* if (ie) {}
* And to detect the version:
* ie === 6 // IE6
* ie > 7 // IE8, IE9, IE10 ...
* ie < 9 // Anything less than IE9
* ----------------------------------------------------------
*/
var ie = (function() {
var undef;
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null) {
rv = parseFloat( RegExp.$1 );
}
}
return ((rv > -1) ? rv : undef);
}());
var randomNumber = function(low, high) {
return Math.floor(Math.random() * high) + low;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment