forked from oftn-oswg/oftn-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (131 loc) · 3.68 KB
/
index.js
File metadata and controls
148 lines (131 loc) · 3.68 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
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
var Sol = module.exports = function Sol(arg, absolute) {
var num;
if (arguments.length === 0) {
num = (new Date().getTime() / 86400000);
} else if (Object.prototype.toString.call(arg) === "[object Date]") {
num = (arg.getTime() / 86400000);
} else if (typeof arg === "number") {
num = arg;
} else {
throw new TypeError("Sol constructor expects Date or number argument");
}
this.floating = num;
this.absolute = typeof absolute != "undefined" ? absolute : true;
};
Sol.prototype.valueOf = function() { return this.floating; };
Sol.prototype.toString = function() {
if (this.floating === Infinity) { return "∞ſ"; }
if (this.floating === -Infinity) { return "-∞ſ"; }
if (isNaN(this.floating)) { return "Invalid Sol"; }
if (this.absolute) {
var num = this.floating.toFixed(6), m = num.match(/e(\+|-)/);
if (m) return m[1] === "+" ? "∞ſ" : "-∞ſ";
var sep = num.split(".");
var sol = sep[0].replace(/(\d)(?=(\d{3})+$)/g, "$1 ")+" ſ "+sep[1].substr(0,3)+" "+sep[1].substr(3,3);
return sol;
}
var keys = [
["k", 1000000000],
["", 1000000],
["m", 1000],
["µ", 1]
];
var builder = [];
var num = this.floating*1000000;
for (var i = 0, len = keys.length; i < len; i++) {
var numspecific = 0;
if (num >= keys[i][1]) {
numspecific = num / keys[i][1] | 0;
num -= numspecific * keys[i][1];
}
if (numspecific) {
builder.push(numspecific + keys[i][0] + "ſ");
}
}
if (!builder.length) builder.push("0ſ");
return builder.join(" ");
};
Sol.prototype.toStupidString = function() {
var num = this.floating * 86400000;
if (this.absolute) {
var d = new Date();
d.setTime(num);
return d.toUTCString();
}
var keys = [
["year", 31536000000],
["month", 2678400000],
["week", 604800000],
["day", 86400000],
["hour", 3600000],
["minute", 60000],
["second", 1000],
["millisecond", 1]
];
var builder = [];
for (var i = 0, len = keys.length; i < len; i++) {
var numspecific = 0;
while (num >= keys[i][1]) {
numspecific++;
num -= keys[i][1];
}
if (numspecific) {
builder.push(numspecific + " " + keys[i][0] +
(numspecific === 1?"":"s"));
}
}
if (!builder.length) builder.push("Zero.");
return builder.join(", ");
};
Sol.parseStupid = function(text, absolute) {
var t = 0, m;
if (absolute) {
return new Sol(new Date(text));
}
m = text.match(/([0-9.]+)\s*eternity/);
if (m) {
if (m[1] != 0) {
this.floating = m[1]*Infinity;
return;
};
}
m = text.match(/([0-9.]+)\s*y/); if (m) { t += m[1]*31536000000; }
m = text.match(/([0-9.]+)\s*mo/); if (m) { t += m[1]*2678400000; }
m = text.match(/([0-9.]+)\s*w/); if (m) { t += m[1]*604800000; }
m = text.match(/([0-9.]+)\s*d/); if (m) { t += m[1]*86400000; }
m = text.match(/([0-9.]+)\s*h/); if (m) { t += m[1]*3600000; }
m = text.match(/([0-9.]+)\s*m(?:$|[^os])/); if (m) { t += m[1]*60000; }
m = text.match(/([0-9.]+)\s*s/); if (m) { t += m[1]*1000; }
m = text.match(/([0-9.]+)\s*ms/); if (m) { t += m[1]*1; }
return new Sol(t / 86400000, false);
};
Sol.parseSol = function(text, absolute) {
if (absolute) {
text = text.replace(/[ſS]/g, '.').replace(/[^\d.]/g, '');
return new Sol(parseFloat(text));
}
var units = {
kS: 1000000000,
hS: 100000000,
daS: 10000000,
S: 1000000,
dS: 100000,
cS: 10000,
mS: 1000,
µS: 1
};
var t = 0;
text = text.replace(/[ ,ſ]/g, function(ch) {
return ch === "ſ" ? "S" : "";
});
matches = text.match(/([\d.]+)\s*(k|h|da|d|c|m|µ|)\s*S/gi);
if (matches) {
for (var i = 0, len = matches.length; i < len; i++) {
var modifier = matches[i];
var value = parseInt(modifier, 10);
var unit = modifier.match(/(k|h|da|d|c|m|µ|)S/)[0];
t += value * units[unit];
}
}
return new Sol(t/1000000, false);
};