/
HapilineTimetable.js
53 lines (52 loc) · 1.66 KB
/
HapilineTimetable.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
import { CSV } from "https://code4fukui.github.io/CSV/CSV.js";
import { Time } from "https://js.sabae.cc/DateTime.js";
export class HapilineTimetable {
static async create() {
const data = await CSV.fetchJSON("hapiline-timetable.csv");
const info = await CSV.fetchJSON("hapiline-station.csv");
return new HapilineTimetable(data, info);
}
constructor(data, info) {
this.data = data;
this.info = info;
}
getTrains(fromstation, tostation) {
if (fromstation == tostation) return [];
return this.data.filter(i => {
const from = i[fromstation + "_発"];
const to = i[tostation + "_着"];
if (!from || !to) return false;
const fromt = new Time(from);
const tot = new Time(to);
const d = tot.toMinutes() - fromt.toMinutes();
//console.log(fromstation, tostation, d, i.ID, i.方向);
return d >= 0 || d < -22 * 60;
});
}
getNextTrains(fromstation, tostation, maxlen = 3, nowm) {
const trs = this.getTrains(fromstation, tostation);
const now = nowm || new Time().quantizeMinutes().toMinutes();
const res = trs.filter(i => {
const from = i[fromstation + "_発"];
const fromt = new Time(from);
const d = now - fromt.toMinutes();
return d <= 0;
});
if (res.length > maxlen) res.length = maxlen;
return res;
}
getStations() {
const v = this.data[0];
const res = new Set();
for (const name in v) {
if (name.endsWith("_発") || name.endsWith("_着")) {
const st = name.substring(0, name.length - 2);
res.add(st);
}
}
return Array.from(res);
}
getStationInfo(name) {
return this.info.find(i => i.name == name);
}
};