atriusmaps-node-sdk
Version:
This project provides an API to Atrius Personal Wayfinder maps within a Node environment. See the README.md for more information
24 lines (20 loc) • 938 B
JavaScript
;
var flightDetailsMapper = require('./flightDetailsMapper.js');
const searchFlights = (term, type, state) => {
const { index, flights, apiError } = state;
const types = type === flightDetailsMapper.FlightType.ALL ? [flightDetailsMapper.FlightType.DEPARTURE, flightDetailsMapper.FlightType.ARRIVAL] : [type];
let flightResults;
if (term) {
flightResults = types.flatMap((currentType) => {
const ids = index[currentType].search({ query: term });
return ids.map((id) => flights[currentType][String(id)]).filter((flight) => Boolean(flight));
});
} else {
flightResults = types.flatMap((currentType) => Object.values(flights[currentType]));
}
if (flightResults.length === 0 && term && /^\w\w\d{2,4}$/.test(term)) {
return searchFlights(term.replace(/(\w\w)(\d{2,4})/, "$1_$2"), type, state);
}
return { flights: flightResults, apiError };
};
exports.searchFlights = searchFlights;