forked from dvmarinoff/Auuki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
347 lines (312 loc) · 7.85 KB
/
functions.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
let sin = x => Math.sin(x);
let cos = x => Math.cos(x);
let arctan = x => Math.atan(x);
let abs = x => Math.abs(x);
let sqrt = x => Math.sqrt(x);
let round = x => Math.round(x);
let floor = x => Math.floor(x);
let ceil = x => Math.ceil(x);
let exp = x => Math.exp(x);
let sqr = x => x * x;
let avg = (x, y) => (x + y) / 2;
let format = (x, precision = 1000) => round(x * precision) / precision;
let mps = kph => format(kph / 3.6);
let kph = mps => 3.6 * mps;
let mToYd = m => 1.09361 * m;
let mpsToMph = mps => 2.23694 * mps;
let kmhToMph = kmh => 0.621371 * kmh;
let kgToLbs = kg => parseInt(2.20462 * kg);
let lbsToKg = lbs => (0.453592 * lbs);
let nextToLast = xs => xs[xs.length - 2];
const empty = (arr) => { return ( (arr === undefined) || !(arr.length > 0)); };
const delay = ms => new Promise(res => setTimeout(res, ms));
const digits = n => Math.log(n) * Math.LOG10E + 1 | 0;
const rand = (min = 0, max = 10) => Math.floor(Math.random() * (max - min + 1) + min);
let last = xs => xs[xs.length - 1];
let first = xs => xs[0];
let second = xs => xs[1];
let third = xs => xs[2];
function memberOf(xs, y) {
return xs.filter(x => x === y).length > 0;
}
function isArray(x) {
return Array.isArray(x);
}
function isObject(x) {
return typeof x === 'object' && !isArray(x);
}
function conj(target, source) {
return Object.assign(target, source);
}
function avgOfArray(xs, prop = false) {
if(prop !== false) {
return xs.reduce( (acc,v,i) => acc+(v[prop]-acc)/(i+1), 0);
} else {
return xs.reduce( (acc,v,i) => acc+(v-acc)/(i+1), 0);
}
}
function maxOfArray(xs, prop = false) {
if(prop !== false) {
return xs.reduce( (acc,v,i) => v[prop] > acc ? v[prop] : acc, 0);
} else {
return xs.reduce( (acc,v,i) => v > acc ? v : acc, 0);
}
};
function sum(xs, prop = false) {
if(prop !== false) {
return xs.reduce( (acc,v,i) => acc + v[prop], 0);
} else {
return xs.reduce( (acc,v,i) => acc + v, 0);
}
};
function splitAt(xs, at) {
let i = -1;
return xs.reduce((acc, x) => {
if((x === at) || (acc.length === 0 && x !== at)) {
acc.push([x]); i++;
} else {
acc[i].push(x);
}
return acc;
},[]);
}
function parseNumber(n, type = 'Int') {
let value = 0;
if(type === 'Int') {
value = parseInt(n || 0);
} else {
value = parseFloat(n || 0);
}
return value;
};
function fixInRange(target, min, max) {
if(target >= max) {
return max;
} else if(target < min) {
return min;
} else {
return target;
}
};
function powerToZone(value, ftp = 256) {
let name = 'one';
let hex = '#636468';
if(value < (ftp * 0.55)) {
name = 'one';
} else if(value < (ftp * 0.76)) {
name = 'two';
} else if(value < (ftp * 0.88)) {
name = 'three';
} else if(value < (ftp * 0.95)) {
name = 'four';
} else if(value < (ftp * 1.06)) {
name = 'five';
} else if (value < (ftp * 1.20)) {
name = 'six';
} else {
name = 'seven';
}
return {name: name};
}
function valueToHeight(max, value) {
return 100 * (value/max);
}
function hrToColor(value) {
let color = 'gray';
if(value < 100) {
color = 'gray';
} else if(value < 120) {
color = 'blue';
} else if(value < 160) {
color = 'green';
} else if(value < 175) {
color = 'yellow';
} else if(value < 190) {
color = 'orange';
} else {
color = 'red';
}
return color;
}
function dateToDashString(date) {
const day = (date.getDate()).toString().padStart(2, '0');
const month = (date.getMonth()+1).toString().padStart(2, '0');
const year = date.getFullYear().toString();
const hour = (date.getHours()).toString().padStart(2, '0');
const minute = (date.getMinutes()).toString().padStart(2, '0');
return `${day}-${month}-${year}-at-${hour}-${minute}h`;
}
function timeDiff(timestamp1, timestamp2) {
let difference = (timestamp1 / 1000) - (timestamp2 / 1000);
return round(abs(difference));
};
function secondsToHms(elapsed, compact = false) {
let hour = Math.floor(elapsed / 3600);
let min = Math.floor(elapsed % 3600 / 60);
let sec = elapsed % 60;
let sD = (sec < 10) ? `0${sec}` : `${sec}`;
let mD = (min < 10) ? `0${min}` : `${min}`;
let hD = (hour < 10) ? `0${hour}` : `${hour}`;
let hDs = (hour < 10) ? `${hour}` : `${hour}`;
let res = ``;
if(compact) {
if(elapsed < 3600) {
res = `${mD}:${sD}`;
} else {
res = `${hD}:${mD}:${sD}`;
}
} else {
res = `${hD}:${mD}:${sD}`;
}
return res ;
}
function formatSpeed(value, measurement = 'metric') {
if(measurement === 'imperial') {
value = `${kmhToMph(value).toFixed(1)}`;
} else {
value = `${(value).toFixed(1)}`;
}
return value;
}
function formatDistance(meters, measurement = 'metric') {
let value = `0`;
let km = (meters / 1000);
let miles = (meters / 1609.34);
let yards = mToYd(meters);
if(measurement === 'imperial') {
value = (yards < 1609.34) ? `${(mToYd(meters)).toFixed(0)} yd` : `${miles.toFixed(2)} mi`;
} else {
value = (meters < 1000) ? `${meters.toFixed(0)} m` : `${km.toFixed(2)} km`;
}
return value;
}
function toDecimalPoint (x, point = 2) {
return Number((x).toFixed(point));
}
function divisors(number) {
let divisors = [1];
for(let i=2; i < number/2; i++) {
if(number % i === 0) { divisors.push(i); }
}
return divisors;
}
function hexToString(str) {
var j;
var hexes = str.match(/.{1,4}/g) || [];
var back = "";
for(j = 0; j<hexes.length; j++) {
back += String.fromCharCode(parseInt(hexes[j], 16));
}
return back;
}
function stringToHex(str) {
var hex, i;
var result = "";
for (i=0; i<str.length; i++) {
hex = str.charCodeAt(i).toString(16);
result += ("000"+hex).slice(-4);
}
return result;
}
function hex (n) {
let h = parseInt(n).toString(16).toUpperCase();
if(h.length === 1) {
h = '0'+ h;
}
return '0x' + h;
}
function arrayToString(array) {
return String.fromCharCode.apply(String, array);
}
function dataViewToString (dataview) {
let len = dataview.byteLength;
let str = '';
for(let i = 0; i < len; i++) {
let value = dataview.getUint8(i, true);
if(value === 0) {
str += '';
} else {
str += hexToString(hex(value));
}
}
return str;
}
const nthBit = (field, bit) => (field >> bit) & 1;
const toBool = (bit) => !!(bit);
const nthBitToBool = (field, bit) => toBool(nthBit(field, bit));
function xor(view) {
let cs = 0;
for (let i=0; i < view.byteLength; i++) {
cs ^= view.getUint8(i);
}
return cs;
}
function exists(x) {
if(x === null || x === undefined) {
return false;
}
return x;
}
function isSet(x, msg = 'Does not exist!') {
if(x === null || x === undefined) {
return false;
}
return true;
}
export {
sin,
cos,
arctan,
abs,
sqr,
exp,
sqrt,
mps,
kph,
mpsToMph,
kmhToMph,
kgToLbs,
lbsToKg,
avg,
rand,
digits,
conj,
first,
second,
third,
last,
nextToLast,
empty,
avgOfArray,
maxOfArray,
sum,
splitAt,
memberOf,
delay,
parseNumber,
toDecimalPoint,
divisors,
fixInRange,
round,
floor,
ceil,
hexToString,
stringToHex,
hex,
arrayToString,
dataViewToString,
nthBit,
toBool,
nthBitToBool,
powerToZone,
hrToColor,
valueToHeight,
dateToDashString,
timeDiff,
secondsToHms,
formatDistance,
formatSpeed,
xor,
exists,
isSet
};