misc updates to ox.js

This commit is contained in:
rolux 2012-01-04 13:12:48 +05:30
commit 2ef642fdeb
11 changed files with 136 additions and 93 deletions

View file

@ -2,7 +2,7 @@
/*@
Ox.asinh <f> Inverse hyperbolic sine
Strangely missing from <code>Math</code>.
Missing from <code>Math</code>.
@*/
Ox.asinh = function(x) {
// fixme: no test
@ -11,7 +11,7 @@ Ox.asinh = function(x) {
/*@
Ox.deg <f> Takes radians, returns degrees
Strangely missing from <code>Math</code>.
Missing from <code>Math</code>.
> Ox.deg(2 * Math.PI)
360
@*/
@ -55,18 +55,20 @@ Ox.limit <f> Limits a number by a given mininum and maximum
3
> Ox.limit(2, 1)
1
> Ox.limit(-1, -2)
-2
@*/
Ox.limit = function(/*num[[, min], max]*/) {
var len = arguments.length,
num = arguments[0],
min = len == 3 ? arguments[1] : 0, // fixme: should be -Infinity
min = len == 3 ? arguments[1] : -Infinity,
max = arguments[len - 1];
return Math.min(Math.max(num, min), max);
};
/*@
Ox.log <f> Returns the logarithm of a given number to a given base
Strangely missing from <code>Math</code>.
Missing from <code>Math</code>.
> Ox.log(100, 10)
2
> Ox.log(Math.E)
@ -86,13 +88,12 @@ Ox.mod <f> Modulo function
9
@*/
Ox.mod = function(num, by) {
var mod = num % by;
return mod >= 0 ? mod : mod + by;
return (num % by + by) % by;
};
/*@
Ox.rad <f> Takes degrees, returns radians
Strangely missing from <code>Math</code>.
Missing from <code>Math</code>.
> Ox.rad(360)
2 * Math.PI
@*/
@ -101,7 +102,12 @@ Ox.rad = function(deg) {
};
/*@
Ox.random <f> Returns a random integer
Ox.random <f> Returns a random integer within a given range
() -> 0 or 1
(max) -> Integer between 0 (inclusive) and max (exclusive)
(min, max) -> Integer between min (inclusive) and max (exclusive)
> [0, 1].indexOf(Ox.random()) > -1
true
> [0, 1, 2].indexOf(Ox.random(3)) > -1
true
> Ox.random(1, 2) == 1
@ -109,9 +115,9 @@ Ox.random <f> Returns a random integer
@*/
Ox.random = function() {
var len = arguments.length,
min = len == 1 ? 0 : arguments[0],
max = arguments[len - 1];
return min + parseInt(Math.random() * (max - min));
min = len == 2 ? arguments[0] : 0,
max = len ? arguments[len - 1] : 2;
return min + Math.floor(Math.random() * (max - min));
};
/*@
@ -130,7 +136,7 @@ Ox.round = function(num, dec) {
/*@
Ox.sinh <f> Hyperbolic sine
Strangely missing from <code>Math</code>.
Missing from <code>Math</code>.
@*/
Ox.sinh = function(x) {
// fixme: no test