utils.js (view raw)
1const _ = require("lodash");
2const redis = require("redis");
3const config = require("./config.json");
4const clientRedis = redis.createClient(6379, config.server.redis);
5
6const formatDepartures = dataTPG => {
7 const stopName = dataTPG.data.stop.stopName;
8 const departures = dataTPG.data.departures;
9 const text = `Prochains départs à \`${stopName}\`:\n`;
10 const content =
11 text +
12 departures
13 .filter(depart => depart.line.destinationName !== stopName)
14 .slice(0, 5)
15 .map(
16 depart =>
17 `*${depart.line.lineCode}* ${
18 depart.line.destinationName
19 } : ${formatTime(depart.waitingTime, depart.reliability)}`
20 )
21 .join("\n");
22 return content;
23};
24
25const formatTime = (value, reliability) => {
26 const num = _.toNumber(value);
27 if (_.isNaN(num)) {
28 if (value === "no more") return "*terminé*";
29 return value.replace(">", ">");
30 }
31 const rel = reliability === "F" ? "" : "~";
32 return rel + num + " min";
33};
34
35module.exports = {
36 formatDepartures,
37 formatTime,
38};