-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontier_module.es6.js
171 lines (137 loc) · 4.54 KB
/
frontier_module.es6.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(function () {
'use-strict';
class FlightsAnalyzer {
constructor (rawData) {
this.deals = this._extractDeals(rawData);
this._mapDeals();
// console.log(this.deals)
}
matchDeals (origin) {
console.log(`Finding matches for ${origin}`);
const matches = {};
const dealMap = this.dealMap;
if (!dealMap[origin]) {
console.error(`Could not find deals for origin: ${origin}.`);
return;
}
const departures = dealMap[origin].departures;
let iDeparture = departures.length;
while (iDeparture--) {
const outboundTrip = departures[iDeparture];
const destinationCode = outboundTrip.ToCity;
if (!dealMap[destinationCode]) { continue; }
if (matches[destinationCode]) {
matches[destinationCode].outbound.push(outboundTrip);
if (outboundTrip.Price < matches[destinationCode].lowestOutbound) {
matches[destinationCode].lowestOutbound = outboundTrip.Price;
recalculateLowestRoundtrip(matches[destinationCode]);
}
continue;
} else {
matches[destinationCode] = {
outbound: [outboundTrip],
inbound: [],
lowestOutbound: outboundTrip.Price,
lowestInbound: Infinity,
lowestRoundTrip: Infinity
};
}
const matchObject = matches[destinationCode];
let iInbound = dealMap[outboundTrip.ToCity].departures.length;
while (iInbound--) {
const inboundTrip = dealMap[outboundTrip.ToCity].departures[iInbound];
if (inboundTrip.ToCity !== origin) { continue; }
matchObject.inbound.push(inboundTrip);
if (inboundTrip.Price < matchObject.lowestInbound) {
matchObject.lowestInbound = inboundTrip.Price;
recalculateLowestRoundtrip(matchObject);
};
}
}
// this._writeToFile(`${Date.now()}_from_${origin.replace(/ /g, "_")}`, matches, "search_results")
return matches;
function recalculateLowestRoundtrip (matchObject) {
matchObject.lowestRoundTrip = matchObject.lowestOutbound + matchObject.lowestInbound;
}
}
matchCities (city1, city2) {
const firstCityDeals = this.matchDeals(city1);
const secondCityDeals = this.matchDeals(city2);
const matches = {};
for (let destination in firstCityDeals) {
if (!secondCityDeals[destination]) { continue; }
matches[destination] = {};
matches[destination][`${city1} Flights`] = firstCityDeals[destination];
matches[destination][`${city2} Flights`] = secondCityDeals[destination];
}
return matches;
}
get citiesList () {
if (!this.cities) {
const cities = [];
for (let city in this.dealMap) {
if (!this.dealMap.hasOwnProperty(city)) {
continue;
}
cities.push(city);
}
this.cities = cities;
}
return this.cities;
}
get airportCodes () {
return this.citiesList;
}
_extractDeals (rawData) {
let resultArray = [];
// let airports = []
let iGroup = rawData.length;
while (iGroup--) {
let group = rawData[iGroup];
// airports.push(group.FromCity)
let iDeals = group.Deals.length;
while (iDeals--) {
const newDeal = group.Deals[iDeals];
newDeal.ToCity = extractIATACode(newDeal.ToCity);
newDeal.FromCity = extractIATACode(newDeal.FromCity);
resultArray.push(newDeal);
};
}
console.log(`Found ${resultArray.length} deals!`);
return resultArray;
}
_mapDeals () {
const deals = this.deals;
const links = {};
let i = deals.length;
while (i--) {
const deal = deals[i];
const originCode = deals[i].FromCity;
if (!links[originCode]) {
links[originCode] = {
departures: []
};
}
const linkObject = links[originCode];
linkObject.departures.push(deal);
}
this.dealMap = links;
}
}
function extractIATACode (airportName) {
return airportName.match(/\((...)\)/)[1];
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = FlightsAnalyzer;
} else {
/* eslint-disable */
if (typeof define === 'function' && define.amd) {
define([], function () {
return FlightsAnalyzer
})
} else {
window.FlightsAnalyzer = FlightsAnalyzer
}
/* eslint-enable */
}
})();