-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
97 lines (81 loc) · 2.25 KB
/
utils.ts
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
import {RawWeatherData, SunTime} from './types';
export function binarySearch(
array: any[],
element: any,
compare_fn: (a: any, b: any) => number,
): number {
let m = 0;
let n = array.length - 1;
while (m <= n) {
let k = (n + m) >> 1;
let cmp = compare_fn(element, array[k]);
if (cmp > 0) {
m = k + 1;
} else if (cmp < 0) {
n = k - 1;
} else {
return k;
}
}
return -m - 1;
}
export function binarySearchRound(
array: any[],
element: any,
compare_fn: (a: any, b: any) => number,
): number {
let targetIndex = binarySearch(array, element, compare_fn);
if (targetIndex < 0) {
targetIndex = -(targetIndex + 1);
let lowIndex = targetIndex - 1;
let highIndex = targetIndex;
let low = targetIndex === 0 ? null : array[targetIndex - 1];
let high = targetIndex === array.length ? null : array[targetIndex];
if (low == null) {
low = array[targetIndex];
lowIndex += 1;
}
if (high == null) {
high = low;
highIndex = lowIndex;
}
targetIndex =
Math.abs(high - element) < Math.abs(low - element) ? highIndex : lowIndex;
}
return targetIndex;
}
export function uuidv4() {
// @ts-expect-error
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(
c ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
).toString(16),
);
}
const MINS_TO_MS = 1000 * 60;
// Should probably return object but I'm lazy
// Math.abs(offsetSunrise - currentDate) <
// Math.abs(offsetSunset - currentDate)
// ? 'sunrise'
// : 'sunset';
export function getNearestSunEvent(
weatherData: RawWeatherData,
): [SunTime, boolean] {
const currentDate = Date.now();
const offsetSunrise = weatherData.daily.sunrise[1] * 1000 + MINS_TO_MS * 15;
const offsetSunset = weatherData.daily.sunset[1] * 1000 + MINS_TO_MS * 15;
let shouldPredictTomorrow = false;
const type =
Math.abs(offsetSunrise - currentDate) < Math.abs(offsetSunset - currentDate)
? currentDate > offsetSunrise
? 'sunset'
: 'sunrise'
: currentDate > offsetSunset
? ((): 'sunrise' => {
shouldPredictTomorrow = true;
return 'sunrise';
})()
: 'sunset';
return [type, shouldPredictTomorrow];
}