This repository has been archived by the owner on May 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
oddslib.mjs
269 lines (234 loc) · 6.58 KB
/
oddslib.mjs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import approximateFraction from "./approx_fraction.mjs";
// Object.assign polyfill
if (typeof Object.assign != "function") {
Object.assign = function (target, varArgs) {
// .length of function is 2
"use strict";
if (target == null) {
// TypeError if undefined or null
throw new TypeError("Cannot convert undefined or null to object");
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) {
// Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
};
}
// 1.2 - 1.0 === 0.19999999999999996
// fixFloatError(1.2 - 1.0) === 0.2
var fixFloatError = function (n) {
return parseFloat(n.toPrecision(12));
};
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === "undefined" || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === "number" && exp % 1 === 0)) {
return NaN;
}
// If the value is negative...
if (value < 0) {
return -decimalAdjust(type, -value, exp);
}
// Shift
value = value.toString().split("e");
value = Math[type](+(value[0] + "e" + (value[1] ? +value[1] - exp : -exp)));
// Shift back
value = value.toString().split("e");
return +(value[0] + "e" + (value[1] ? +value[1] + exp : exp));
}
var FORMATS = {
// European/Decimal format
decimal: {
from: function (decimal) {
decimal = parseFloat(decimal);
if (decimal <= 1.0) {
throw new Error("Outside valid range.");
}
return decimal;
},
to: function () {
return this.decimalValue;
},
},
// American/Moneyline format
moneyline: {
from: function (moneyline) {
moneyline = parseFloat(moneyline);
if (moneyline >= 0) {
return moneyline / 100.0 + 1;
}
return 100 / -moneyline + 1;
},
to: function () {
if (this.decimalValue >= 2) {
return fixFloatError((this.decimalValue - 1) * 100.0);
}
return fixFloatError(-100 / (this.decimalValue - 1));
},
},
// Hong Kong format
hongKong: {
from: function (hongKong) {
hongKong = parseFloat(hongKong);
if (hongKong < 0.0) {
throw new Error("Outside valid range.");
}
return hongKong + 1.0;
},
to: function () {
return fixFloatError(this.decimalValue - 1);
},
},
// Implied probability
impliedProbability: {
from: function (ip) {
// Handle percentage string
if (typeof ip === "string" && ip.slice(-1) == "%") {
ip = parseFloat(ip) / 100.0;
} else {
ip = parseFloat(ip);
}
if (ip <= 0.0 || ip >= 1.0) {
throw new Error("Outside valid range");
}
return 1.0 / ip;
},
to: function (options) {
if (options.percentage) {
var value = fixFloatError(100.0 / this.decimalValue);
// HACK: Oddslib.prototype.to calls decimalAdjust if we return a number.
// But we need to round before adding the % symbol.
// So we do it here and return the string
if (options.precision !== null) {
value = decimalAdjust("round", value, -options.precision);
}
return value.toString() + "%";
}
return fixFloatError(1 / this.decimalValue);
},
},
// UK/Fractional format
fractional: {
from: function (n) {
// Try to split on the slash
var pieces = n.toString().split("/");
n = parseFloat(pieces[0]);
var d;
if (pieces.length === 2) {
d = parseFloat(pieces[1]);
} else if (pieces.length === 1) {
d = 1;
} else {
throw new Error("Invalid fraction");
}
if (n === 0 || d === 0 || n / d <= 0.0) {
throw new Error("Outside valid range");
}
return 1 + n / d;
},
to: function (options) {
return approximateFraction(
this.decimalValue - 1,
options.precision || 12
);
},
},
// Malay format
malay: {
from: function (malay) {
malay = parseFloat(malay);
if (malay <= -1.0 || malay > 1.0) {
throw new Error("Outside valid range.");
}
if (malay < 0) {
malay = -1 / malay;
}
return malay + 1;
},
to: function () {
if (this.decimalValue <= 2.0) {
return fixFloatError(this.decimalValue - 1);
}
return fixFloatError(-1 / (this.decimalValue - 1));
},
},
// Indonesian format
indonesian: {
from: function (indonesian) {
indonesian = parseFloat(indonesian);
if (indonesian === 0) {
throw new Error("Outside valid range.");
}
if (indonesian >= 1) {
return indonesian + 1;
}
return -1 / indonesian + 1;
},
to: function () {
if (this.decimalValue < 2.0) {
return fixFloatError(-1 / (this.decimalValue - 1));
}
return fixFloatError(this.decimalValue - 1);
},
},
};
export const Odds = (function () {
// Private constructor pattern
// from http://stackoverflow.com/a/21731713
var PublicOdds = function () {
throw new Error(
"This constructor is private, please use the from* functions"
);
};
var Odds = function (decimalValue) {
if (typeof decimalValue !== "number" || isNaN(decimalValue)) {
throw new Error("Invalid odds");
}
this.decimalValue = fixFloatError(decimalValue);
};
Odds.prototype = PublicOdds.prototype;
// Generic constructor
PublicOdds.from = function (format, value) {
if (!FORMATS.hasOwnProperty(format)) {
throw new Error("Unknown format " + format + ".");
}
var decimal = FORMATS[format].from(value);
return new Odds(decimal);
};
return PublicOdds;
})();
// Conversion API
Odds.prototype.to = function (format, options) {
if (!FORMATS.hasOwnProperty(format)) {
throw new Error("Unknown format " + format + ".");
}
options = Object.assign(
{
precision: null,
percentage: false,
},
options
);
var ret = FORMATS[format].to.call(this, options);
if (typeof ret === "number" && options.precision !== null) {
ret = decimalAdjust("round", ret, -options.precision);
}
return ret;
};
export const from = Odds.from;