Skip to content

Instantly share code, notes, and snippets.

@marlun78
Last active September 27, 2015 20:58
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 marlun78/1330519 to your computer and use it in GitHub Desktop.
Save marlun78/1330519 to your computer and use it in GitHub Desktop.
Unsorted JavaScript stuff
/**
* Unsorted stuff
* Copyright (c) 2011, marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*/
(function (NS) {
if (!NS) { NS = window; }
// isNumber
NS.isNumber = function (n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
// toNumber
// Dependent on NS.isNumber
NS.toNumber = function (n) {
return NS.isNumber(n) ? Number(n) : NaN;
};
// random
NS.random = function (from, to) {
return Math.round(Math.random() * (to - from)) + from;
};
// isLeapYear
NS.isLeapYear = function (year) {
return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
};
// getRandomRGB
// Dependent on NS.random
NS.getRandomRGB = function (asStyle) {
var count = i = 2, rgb = [];
do {
rgb[count - i] = NS.random(0, 255);
} while (i -= 1)
return asStyle ? 'rgb(' + rgb.join(',') + ')' : rgb;
};
// getRandomRGBA
// Dependent on NS.getRandomRGB
NS.getRandomRGBA = function (asStyle) {
var rgba = NS.getRandomRGB();
rgba[rgba.length] = Math.random();
return asStyle ? 'rgba(' + rgba.join(',') + ')' : rgba;
};
}(/* Pass in your namespace here ... */));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment