hk-bus-eta
Version:
Query the ETA (Estimated Time of Arrival) of HK Bus/Minibus/MTR/Lightrail
71 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUpcomingFerry = exports.isSafari = void 0;
exports.isSafari = (() => {
try {
return Boolean(navigator &&
navigator.userAgent &&
navigator.userAgent.includes("Safari/") &&
!(navigator.userAgent.includes("Chrome/") ||
navigator.userAgent.includes("Chromium/")));
}
catch (_a) {
return false;
}
})();
// fancy handling for timezone, not recommend to use it unless you know what's going on
// i.e, calling isHoliday(holidays, new Date()) is checking if it was being a holiday at the moment of 8 hours ago
const isHoliday = (holidays, utcDate) => {
if (utcDate.getDay() === 0)
return true;
return holidays.includes(String(utcDate.getUTCFullYear()) +
String(utcDate.getUTCMonth() + 1).padStart(2, "0") +
String(utcDate.getUTCDate()).padStart(2, "0"));
};
const getUpcomingFerry = ({ holidays, serviceDayMap, date, freq, }) => {
if (freq === null) {
return [];
}
const getEta = (serviceFreq, date) => Object.keys(serviceFreq)
.sort((a, b) => (a < b ? -1 : 1))
.reduce((acc, tp) => {
const ferryTime = new Date(date.getTime());
ferryTime.setUTCHours(parseInt(tp.slice(0, 2), 10));
ferryTime.setUTCMinutes(parseInt(tp.slice(2), 10));
if (ferryTime >= date) {
acc.push(String(ferryTime.getUTCFullYear()) +
"-" +
String(ferryTime.getUTCMonth() + 1).padStart(2, "0") +
"-" +
String(ferryTime.getUTCDate()).padStart(2, "0") +
"T" +
String(ferryTime.getUTCHours()).padStart(2, "0") +
":" +
String(ferryTime.getUTCMinutes()).padStart(2, "0") +
":00" +
".000+08:00");
}
return acc;
}, []);
// obtain all ETAs sampling for next 24 hours, fancy handling for timezone
const ret = Array(24)
.fill(0)
.reduce((acc, _, idx) => {
const refDate = new Date(date.getTime());
refDate.setUTCHours(refDate.getUTCHours() + 8 + idx);
for (let serviceKey in freq) {
if (isHoliday(holidays, refDate)) {
if (serviceDayMap[serviceKey][0] === "1") {
return acc.concat(getEta(freq[serviceKey], refDate));
}
}
else if (serviceDayMap[serviceKey][refDate.getDay()]) {
return acc.concat(getEta(freq[serviceKey], refDate)).sort();
}
}
return acc;
}, []);
return [...new Set(ret)].sort((a, b) => (a < b ? -1 : 1));
};
exports.getUpcomingFerry = getUpcomingFerry;
//# sourceMappingURL=utils.js.map