rnd me
10-28-2009, 04:10 AM
One thing that i've always missed in javascript was some type of range feature.
firefox has array comprehensions and generators, but most browsers don't support that, and it can't really be re-created using javascript 1.5.
the prototype library offers a Range utility, but i did not like how it requires an extra object and method call to use.
Needing something simple, fast, and flexible, i came up with the following function. It's served me well, so i thought i'd share.
Given two numbers as arguments, it returns a new range function that expects just one argument: a number.
Calling the range with a number as arguments[0] will return a Boolean indicating whether or not the number you passed is within the range's range.
function Range(n1, n2){
if(!n2){n2=0;}
if(n1>n2){n2=[n1,n1=n2][0];}
function range(n){return n>=range.min && n<=range.max;}
range.toString=function _ts(){return "( Range("+(range.min)+","+(range.max)+") )";}
range.min=n1;
range.max=n2;
return range;
}//end Range()
usage examples
test a couple numbers with a new range:
var rng=Range(10,20);
alert(rng(5))//==false
alert(rng(15))//==true
the order of the numbers given to Range does not matter:
var rng=Range(20,10);
alert(rng(5))//==false
alert(rng(15))//==true
you can change the range's min/max after creation:
var rng=Range(10,20);
alert(rng(5))//==false
rng.min=3;
alert(rng(5))//==true
range has it's own eval/parse-friendly String flavor:
var rng=Range(0, 500);
alert( String(rng) )//==="( Range(0,500) )"
you can use a new range 'on the spot' for a quick compare:
var x=15;
alert( Range(10,20)(x) )//===true
Range also works great for filtering arrays of numbers ([].filter required):
[5,10,15,99,20,25].filter( Range(10, 20) ); //===[10,15,20]
If you can think of any more uses, please don't be shy, post them here!
firefox has array comprehensions and generators, but most browsers don't support that, and it can't really be re-created using javascript 1.5.
the prototype library offers a Range utility, but i did not like how it requires an extra object and method call to use.
Needing something simple, fast, and flexible, i came up with the following function. It's served me well, so i thought i'd share.
Given two numbers as arguments, it returns a new range function that expects just one argument: a number.
Calling the range with a number as arguments[0] will return a Boolean indicating whether or not the number you passed is within the range's range.
function Range(n1, n2){
if(!n2){n2=0;}
if(n1>n2){n2=[n1,n1=n2][0];}
function range(n){return n>=range.min && n<=range.max;}
range.toString=function _ts(){return "( Range("+(range.min)+","+(range.max)+") )";}
range.min=n1;
range.max=n2;
return range;
}//end Range()
usage examples
test a couple numbers with a new range:
var rng=Range(10,20);
alert(rng(5))//==false
alert(rng(15))//==true
the order of the numbers given to Range does not matter:
var rng=Range(20,10);
alert(rng(5))//==false
alert(rng(15))//==true
you can change the range's min/max after creation:
var rng=Range(10,20);
alert(rng(5))//==false
rng.min=3;
alert(rng(5))//==true
range has it's own eval/parse-friendly String flavor:
var rng=Range(0, 500);
alert( String(rng) )//==="( Range(0,500) )"
you can use a new range 'on the spot' for a quick compare:
var x=15;
alert( Range(10,20)(x) )//===true
Range also works great for filtering arrays of numbers ([].filter required):
[5,10,15,99,20,25].filter( Range(10, 20) ); //===[10,15,20]
If you can think of any more uses, please don't be shy, post them here!