mirror of
https://github.com/samsonjs/batteries.git
synced 2026-03-25 09:15:46 +00:00
30 lines
601 B
JavaScript
30 lines
601 B
JavaScript
// batteries
|
|
// Copyright 2010 - 2011 Sami Samhuri <sami@samhuri.net>
|
|
|
|
exports.Range = Range;
|
|
|
|
exports.extendNative = function() {
|
|
global.Range = Range;
|
|
};
|
|
|
|
function Range(start, length) {
|
|
this.start = start;
|
|
this.length = length;
|
|
};
|
|
|
|
Range.prototype.inRange = function(val) {
|
|
if (this.test) return this.test(val);
|
|
return val >= this.start && val <= this.start + this.length;
|
|
};
|
|
|
|
Range.prototype.toArray = function(nth) {
|
|
var a = []
|
|
, i = this.length
|
|
;
|
|
nth = nth || this.nth;
|
|
if (nth)
|
|
while (i--) a[i] = nth(i);
|
|
else
|
|
while (i--) a[i] = this.start + i;
|
|
return a;
|
|
};
|