-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional.js
More file actions
42 lines (36 loc) · 1.04 KB
/
Copy pathfunctional.js
File metadata and controls
42 lines (36 loc) · 1.04 KB
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
module.exports = function(opts) {
opts = opts || {};
var ctable = [1000, 60, 60, 24, 365 / 12, 12];
var ntable = opts.table || ['milliseconds', 'seconds', 'minutes', 'hours', 'days', 'months', 'years'];
var convert_table = [], temp_arr;
for(var i = 0; i < ntable.length; i++) {
temp_arr = [];
temp_arr[i] = 1;
for (var j = i + 1; j < ntable.length; j++) {
temp_arr[j] = temp_arr[j-1] / ctable[j-1];
}
for (var j = i - 1; j >= 0; j--) {
temp_arr[j] = temp_arr[j+1] * ctable[j];
}
convert_table.push(temp_arr);
(function (unit) {
this[ntable[i]] = function(number) {
var result = new Number(number);
result.__unit = unit;
return result;
}
if (opts.singular === true) {
this[ntable[i]] = function(number) {
var result = new Number(number);
result.__unit = unit;
return result;
}
}
this['to_' + ntable[i]] = function(number) {
var result = new Number(number * convert_table[number.__unit || 0][unit]);
result.__unit = unit;
return result;
}
})(i);
}
}.bind(this)