-
Notifications
You must be signed in to change notification settings - Fork 20
/
data-helpers.js
67 lines (61 loc) · 1.53 KB
/
data-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
function avg(arr) {
let n = 0;
let t = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] != null) {
n++;
t += arr[i];
}
}
return n == 0 ? 0 : t / n;
}
export function getScore(place) {
let score = avg([place.wifi, place.power, place.vacancy, place.comfort, place.quiet, place.drinks, place.food, place.price, place.view, place.toilets]);
if (place.wifi === 0) score -= 0.2;
if (place.power === 0) score -= 0.2;
if (place.smoking) score -= 0.1;
if (place.standing_tables) score += 0.1;
if (place.outdoor_seating) score += 0.1;
if (place.cash_only) score -= 0.1;
if (place.time_limit) score -= 0.1;
return Math.max(0, Math.min(score, 5));
}
const DAYS = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
export function getHours(hours) {
if (hours) {
const res = [];
if (typeof hours === 'string') {
for (const day of DAYS) {
res.push(getTimes(hours));
}
} else {
for (const day of DAYS) {
res.push(getTimes(hours[day]));
}
}
return res;
} else {
return null;
}
}
export function getReview(place) {
if (place.review) {
return place.review;
} else if (place.content) {
return { en: place.content };
} else {
return {};
}
}
function getTimes(span) {
if (!span) return null;
const times = span.split('-');
let [open, close] = times.map((time, i) => {
let [hour, min] = time.split(':').map(Number);
return (hour * 100) + (min || 0);
});
if (close < 500) {
close += 2400;
}
return [open, close];
}