-
Notifications
You must be signed in to change notification settings - Fork 19
/
data.js
242 lines (221 loc) · 6.84 KB
/
data.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
import _ from 'lodash';
import fs from 'fs';
import path from 'path';
import yaml from 'yaml';
import { getHours, getScore, getReview } from './data-helpers.js';
function parseFile(filename) {
const file = fs.readFileSync(filename, 'utf8');
const split = file.split('---');
const data = yaml.parse(split[1]);
const content = split.slice(2).join('---').trim();
if (content) {
data.content = content;
}
return data;
}
function filterValidFiles(filename) {
return !filename.startsWith('.');
}
export function getPlaceDescription(i18n, locale, place) {
const { __ } = i18n;
const { power, wifi, speed } = place;
const type = __({ locale, phrase: `Type: ${place.type}` });
const city = __({ locale, phrase: `Location: ${place.cityName}` });
const area = place.area ? __({ locale, phrase: `Area: ${place.area }` }) : null;
let text;
if (area) {
text = __({ locale, phrase: '{{type}} in {{area}}, {{city}}.' }, { type, area, city })
} else {
text = __({ locale, phrase: '{{type}} in {{area}}.' }, { type, area: city });
}
if (power && wifi && speed) {
text += __({ locale, phrase: ' {{speed}} Mb/s WiFi and power outlets available.' }, { speed });
} else if (power && wifi) {
text += __({ locale, phrase: ' WiFi and power outlets available.' });
} else if (power) {
text += __({ locale, phrase: ' Power outlets available.' });
} else if (wifi && speed) {
text += __({ locale, phrase: ' {{speed}} Mb/s WiFi available.' });
} else if (wifi) {
text += __({ locale, phrase: ' WiFi available.' });
}
if (place.review[locale]) {
text += ` ${place.review[locale]}`;
}
return text;
}
export function getCityDescription(i18n, locale, city) {
const { __ } = i18n;
const name = __({ locale, phrase: `City: ${city.name}` });
const country = __({ locale, phrase: `Country: ${city.country}` });
if (name == country) {
return __({
locale,
phrase: 'Explore work-friendly cafes and coworking spaces in {{name}}. Find the best places with power outlets and fast WiFi to work or study from.',
}, { name });
} else {
return __({
locale,
phrase: 'Explore work-friendly cafes and coworking spaces in {{name}}, {{country}}. Find the best places with power outlets and fast WiFi to work or study from.',
}, { name, country });
}
}
function parseCoordinates(coords) {
if (coords) {
const [lat, lng] = coords.split(',').map(e => Number(e.trim()));
return [lng, lat];
} else {
return undefined;
}
}
function getUrlFriendlyName(name) {
return name.toLowerCase()
.split(' / ').join('-')
.split(' - ').join('-')
.split(' & ').join('-')
.split('/').join('-')
.split(' ').join('-');
}
function getPlaces() {
const places = [];
const cityDirs = fs.readdirSync('data/').filter(filterValidFiles);
for (const cityId of cityDirs) {
const placeFiles = fs.readdirSync(`data/${cityId}/`).filter(filterValidFiles);
for (const placeFile of placeFiles) {
if (placeFile != 'index.md') {
try {
const placeData = parseFile(`data/${cityId}/${placeFile}`);
const name = path.basename(placeFile, '.md');
const place = Object.assign(placeData, {
id: name,
url: `/${cityId}/${name}/`,
coordinates: parseCoordinates(placeData.coordinates),
city: cityId,
file: `${cityId}/${placeFile}`,
score: getScore(placeData),
hours: getHours(placeData.hours),
review: getReview(placeData),
});
if (place.images) {
place.images = place.images.map(image => `${place.url}${image}`);
}
places.push(place);
} catch (err) {
console.log(cityId, placeFile, err.message);
throw err;
}
}
}
}
return _(places)
.orderBy('score', 'desc')
.value();
}
function getCities(places) {
const cities = {};
for (const place of places) {
const cityData = parseFile(`data/${place.city}/index.md`);
const id = place.city;
if (!cities[place.city]) {
const city = Object.assign(cityData, {
id,
url: `/${id}/`,
coordinates: parseCoordinates(cityData.coordinates),
places: [],
count: 0,
redirects: [],
});
cities[place.city] = city;
}
if (place.redirect) {
cities[place.city].redirects.push(place);
continue;
}
place.cityUrl = `/${id}/`;
place.cityName = cityData.name;
cities[place.city].places.push(place);
if (place.score && !place.closed) {
cities[place.city].count++;
}
}
return _(cities)
.values()
.orderBy(e => e.name)
.value();
}
function getAreas(places) {
const areas = {};
for (const place of places) {
if (place.area && place.content && place.images && place.score >= 3 && !place.closed) {
if (!areas[place.area]) {
const id = getUrlFriendlyName(place.area);
areas[place.area] = {
id,
name: place.area,
url: `/${place.city}/area/${id}/`,
title: `${place.area} Area`,
city: place.city,
places: [],
};
}
areas[place.area].places.push(place);
}
}
for (const place of places) {
if (areas[place.area]) {
place.areaUrl = areas[place.area].url;
}
}
return _(areas)
.values()
.orderBy('name')
.value();
}
function getStations(places) {
const stations = {};
for (const place of places) {
if (place.station && place.content && place.images && place.score >= 3 && !place.closed) {
for (const station of place.station.split(',').map(e => e.trim())) {
if (!stations[station]) {
const id = getUrlFriendlyName(station);
stations[station] = {
id,
name: station,
url: `/${place.city}/station/${id}/`,
title: `${station} Station`,
city: place.city,
places: [],
}
}
stations[station].places.push(place);
}
}
}
for (const place of places) {
if (stations[place.station]) {
place.stationUrl = stations[place.station].url;
}
}
return _(stations)
.values()
.orderBy('name')
.value();
}
export function load() {
const t1 = Date.now();
const places = getPlaces();
const cities = getCities(places);
const areas = []; //getAreas(places);
const stations = []; //getStations(places);
const recent = _(places)
.filter(e => e.added && e.images?.length > 0)
.orderBy(e => e.added, 'desc')
.value();
const top = _(places)
.filter(e => !e.closed && e.images?.length > 0)
.orderBy('score', 'desc')
.value();
const t2 = Date.now();
console.log(`Loaded ${cities.length} cities, ${places.length} places, ${areas.length} areas, ${stations.length} stations in ${t2 - t1} ms`);
return { cities, places, areas, stations, recent, top };
}