connection-scan-algorithm
Version:
Connection Scan Algorithm
31 lines (30 loc) • 1.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const ts_array_utils_1 = require("ts-array-utils");
/**
* Implementation of CSA that searches for journeys between a set of origin and destinations.
*/
class DepartAfterQuery {
constructor(csa, resultsFactory, filters = []) {
this.csa = csa;
this.resultsFactory = resultsFactory;
this.filters = filters;
}
/**
* Plan a journey between the origin and destination set of stops on the given date and time
*/
plan(origins, destinations, date, time) {
const originTimes = origins.reduce(ts_array_utils_1.keyValue(origin => [origin, time]), {});
const dateNumber = this.getDateNumber(date);
const dayOfWeek = date.getDay();
const results = this.csa.scan(originTimes, destinations, dateNumber, dayOfWeek);
const journeys = this.resultsFactory.getJourneys(results, destinations);
// apply each filter to the results
return this.filters.reduce((rs, filter) => filter.apply(rs), journeys);
}
getDateNumber(date) {
const str = date.toISOString();
return parseInt(str.slice(0, 4) + str.slice(5, 7) + str.slice(8, 10), 10);
}
}
exports.DepartAfterQuery = DepartAfterQuery;
;