-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
100 lines (82 loc) · 2.92 KB
/
helpers.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
import { format, add } from 'date-fns';
import allTideData from './Data/tidesDataCombined.json';
export function getMonthOfTides(month) {
const tideInfo = allTideData.data[`mm_${month}`];
const expiry = Date.now() + 864000000;
const tidesWithExpiry = [...tideInfo, { expiry }];
localStorage.setItem(month, JSON.stringify(tidesWithExpiry));
return tidesWithExpiry;
}
export function buildDay(datetime = Date.now(), offset = 0) {
const calcNow = add(datetime, { days: offset });
return {
dt: calcNow,
dayOfWeek: format(calcNow, 'EEEE'),
date: format(calcNow, 'M-dd-yyyy')
};
}
export function findFutureDays(date = Date.now()) {
const isNow1 = buildDay(date, 1);
const isNow2 = buildDay(date, 2);
const isNow3 = buildDay(date, 3);
const upcoming = [isNow1, isNow2, isNow3];
return upcoming;
}
function tidesForTheMonth(monthValue) {
let monthTides;
if (!window.localStorage.getItem(monthValue)) {
monthTides = getMonthOfTides(monthValue);
} else {
monthTides = JSON.parse(window.localStorage.getItem(monthValue));
// check if month is stale
const [{ expiry = Date.now() }] = monthTides.filter(item => item.expiry);
if (Date.now() > expiry) {
monthTides = getMonthOfTides(monthValue);
}
}
const tidesWithOutExpiry = monthTides.filter(item => !item.expiry);
return tidesWithOutExpiry;
}
function comboTides(curr = {}, next = {}) {
const tidys = { data: [...curr, ...next] };
return tidys;
}
export function getTidesData(date = Date.now()) {
// find out what is 3 days from now
const isNow3 = add(date, { days: 3 });
const monthOfCurrentDate = format(date, 'yyyy-MM');
const monthOfLatestDate = format(isNow3, 'yyyy-MM');
let nextMonth = [];
// check if current month saved
const currentMonth = tidesForTheMonth(monthOfCurrentDate);
// check if you need to get next month
if (monthOfCurrentDate !== monthOfLatestDate) {
// check if latest month tides saved
nextMonth = tidesForTheMonth(monthOfLatestDate);
}
const tidesObj = comboTides(currentMonth, nextMonth);
const data = Array.from(new Set(tidesObj.data.map(a => a.eventTime)))
.map(eventTime => tidesObj.data.find(a => a.eventTime === eventTime))
.sort((a, b) => a.eventTime < b.eventTime);
return { data };
}
export function getTidesMonth(month) {
const currentMonth = tidesForTheMonth(month);
const tidesObj = { data: [...currentMonth] };
return tidesObj;
}
export function findDayTides(date, tides) {
if (!tides || !tides.data) return;
const getDayTides = tides.data
.filter(tide => {
const tideDate = format(new Date(tide.eventTime), 'M-dd-yyyy');
return tideDate === date;
})
.sort((a, b) => new Date(a.eventTime) - new Date(b.eventTime));
const finalDayTides = getDayTides.map(tides => {
const { eventTime } = tides;
const tideTime = format(new Date(eventTime), 'h:mm aaaa');
return { ...tides, tideTime };
});
return finalDayTides;
}