Last active
September 27, 2015 20:58
-
-
Save marlun78/1330519 to your computer and use it in GitHub Desktop.
Unsorted JavaScript stuff
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
/** | |
* 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