/
number.js
158 lines (150 loc) · 4.24 KB
/
number.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/** section: Language
* class Number
*
* Extensions to the built-in `Number` object.
*
* Prototype extends native JavaScript numbers in order to provide:
*
* * [[ObjectRange]] compatibility, through [[Number#succ]].
* * Numerical loops with [[Number#times]].
* * Simple utility methods such as [[Number#toColorPart]] and
* [[Number#toPaddedString]].
* * Instance-method aliases of many functions in the `Math` namespace.
*
**/
Object.extend(Number.prototype, (function() {
/**
* Number#toColorPart() -> String
*
* Produces a 2-digit hexadecimal representation of the number
* (which is therefore assumed to be in the \[0..255\] range, inclusive).
* Useful for composing CSS color strings.
*
* ##### Example
*
* 10.toColorPart()
* // -> "0a"
**/
function toColorPart() {
return this.toPaddedString(2, 16);
}
/**
* Number#succ() -> Number
*
* Returns the successor of the current [[Number]], as defined by current + 1.
* Used to make numbers compatible with [[ObjectRange]].
**/
function succ() {
return this + 1;
}
/**
* Number#times(iterator[,context]) -> Number
* - iterator (Function): An iterator function to call.
* - context (Object): An optional context (`this` value) to use when
* calling `iterator`.
*
* Calls `iterator` the specified number of times, passing in a number as
* the first parameter. The number will be 0 on first call, 1 on second
* call, etc. `times` returns the number instance it was called on.
*
* ##### Example
*
* (3).times(alert);
* // -> Alerts "0", then "1", then "2"; returns 3
*
* var obj = {count: 0, total: 0};
* function add(addend) {
* ++this.count;
* this.total += addend;
* }
* (4).times(add, obj);
* // -> 4
* obj.count;
* // -> 4
* obj.total;
* // -> 6 (e.g., 0 + 1 + 2 + 3)
**/
function times(iterator, context) {
$R(0, this, true).each(iterator, context);
return this;
}
/**
* Number#toPaddedString(length[, radix]) -> String
* - length (Number): The minimum length for the resulting string.
* - radix (Number): An optional radix for the string representation,
* defaults to 10 (decimal).
*
* Returns a string representation of the number padded with leading 0s so
* that the string's length is at least equal to `length`. Takes an optional
* `radix` argument which specifies the base to use for conversion.
*
* ##### Examples
*
* (13).toPaddedString(4);
* // -> "0013"
*
* (13).toPaddedString(2);
* // -> "13"
*
* (13).toPaddedString(1);
* // -> "13"
*
* (13).toPaddedString(4, 16)
* // -> "000d"
*
* (13).toPaddedString(4, 2);
* // -> "1101"
**/
function toPaddedString(length, radix) {
var string = this.toString(radix || 10);
return '0'.times(length - string.length) + string;
}
/**
* Number#abs() -> Number
*
* Returns the absolute value of the number. Convenience method that simply
* calls `Math.abs` on this instance and returns the result.
**/
function abs() {
return Math.abs(this);
}
/**
* Number#round() -> Number
*
* Rounds the number to the nearest integer. Convenience method that simply
* calls `Math.round` on this instance and returns the result.
**/
function round() {
return Math.round(this);
}
/**
* Number#ceil() -> Number
*
* Returns the smallest integer greater than or equal to the number.
* Convenience method that simply calls `Math.ceil` on this instance and
* returns the result.
**/
function ceil() {
return Math.ceil(this);
}
/**
* Number#floor() -> Number
*
* Returns the largest integer less than or equal to the number.
* Convenience method that simply calls `Math.floor` on this instance and
* returns the result.
**/
function floor() {
return Math.floor(this);
}
return {
toColorPart: toColorPart,
succ: succ,
times: times,
toPaddedString: toPaddedString,
abs: abs,
round: round,
ceil: ceil,
floor: floor
};
})());