-
Notifications
You must be signed in to change notification settings - Fork 8
/
range.js
67 lines (57 loc) · 1.33 KB
/
range.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*<javascriptresource><menu>hide</menu></javascriptresource>*/
/**
* Returns a range from start exclusive to end exclusive.
* @param endExclusive non-decimal value.
* @return {Range}
*/
Number.prototype.until =
function(endExclusive) {
return new Range(this - 1, endExclusive - 1)
}
/**
* Creates a range from starting point to end.
* @param start {number} non-decimal value.
* @param end {number} non-decimal value.
*/
function Range(start, end) {
checkNotNull(start)
check(start.isInt())
checkNotNull(end)
check(end.isInt())
var self = this
/** @type {number} */
self.start = start
/** @type {number} */
self.startExclusive = start + 1
/** @type {number} */
self.end = end
/** @type {number} */
self.endExclusive = end + 1
/**
* Returns size from start to end.
* @return {number}
*/
self.getLength =
function() {
return end - start + 1
}
/**
* Returns true if value is within range.
* @param {number} value
* @return {boolean}
*/
self.contains =
function(value) {
return start <= value && value <= end
}
/**
* Iterate index from start to end.
* @param {function(number)} action
*/
self.forEachIndex =
function(action) {
for (var i = start; i < self.endExclusive; i++) {
action(i)
}
}
}