-
Notifications
You must be signed in to change notification settings - Fork 56
/
reading.js
33 lines (30 loc) · 867 Bytes
/
reading.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
export const getReadings = async (length = 1200) => {
const current = Date.now();
const hour = 1000 * 60 * 60;
return [...new Array(length)].map((_, index) => ({
time: current - index * hour,
value: Math.random() * 0.7 + 0.4,
}));
};
export const groupByDay = (readings) => {
const groupedByDay = readings.reduce((curr, { time, value }) => {
const readingDate = new Date(time);
const day = new Date(
readingDate.getFullYear(),
readingDate.getMonth(),
readingDate.getDate()
).getTime();
if (!curr[day]) curr[day] = 0;
curr[day] += value;
return curr;
}, {});
return Object.entries(groupedByDay).map(([day, value]) => ({
time: Number(day),
value,
}));
};
export const sortByTime = (readings) => {
return [...readings].sort(
(readingA, readingB) => readingA.time - readingB.time
);
};