Skip to content

Commit 1411d72

Browse files
committed
extracts mergePrices to utils
1 parent 4bb4964 commit 1411d72

File tree

3 files changed

+50
-46
lines changed

3 files changed

+50
-46
lines changed

apis/autoblog.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const fetch = require('node-fetch');
1919
*/
2020
const { parse } = require('node-html-parser');
2121

22-
const { fillMissingPrices, sortByDistance, sortByPrice } = require('./utils');
22+
const { fillMissingPrices, sortByDistance, sortByPrice, mergePrices } = require('./utils');
2323

2424
const BASE_URL = 'https://www.autoblog.com';
2525
const MAX_PAGE = 2;
@@ -125,31 +125,17 @@ async function getAllStations() {
125125
}
126126

127127
/**
128-
* @function mergePrices
129-
* @description Merges fuel prices of different types of gas station
128+
* @function getStationKey
129+
* @description Helper to retrieve unique station key.
130130
*
131-
* @param {Object[]} responses - List of gas stations with prices of single fuel type.
131+
* @param {Object} station - Station
132132
*
133-
* @returns {Object} Returns gas stations with merged prices and max prices per fuel type.
133+
* @returns {string} Returns unique station key.
134+
*
135+
* @see apis/README.md
134136
*/
135-
function mergePrices(responses) {
136-
const { indexedStations, maxPricesByType } = responses.reduce(({ indexedStations, maxPricesByType }, station) => {
137-
const stationKey = `${station.name}-${station.address}`;
138-
139-
if (!indexedStations[stationKey]) {
140-
indexedStations[stationKey] = station;
141-
} else {
142-
indexedStations[stationKey].prices[station.fuelType] = station.prices[station.fuelType];
143-
}
144-
145-
if (!maxPricesByType[station.fuelType] || maxPricesByType[station.fuelType] < station.prices[station.fuelType]) {
146-
maxPricesByType[station.fuelType] = station.prices[station.fuelType];
147-
}
148-
149-
return { indexedStations, maxPricesByType };
150-
}, { indexedStations: {}, maxPricesByType: {} });
151-
152-
return { stations: Object.values(indexedStations), maxPricesByType };
137+
function getStationKey(station) {
138+
return `${station.name}-${station.address}`;
153139
}
154140

155141
/**
@@ -164,7 +150,7 @@ function mergePrices(responses) {
164150
async function getData() {
165151
const responses = await getAllStations();
166152

167-
const { stations, maxPricesByType } = mergePrices(responses);
153+
const { stations, maxPricesByType } = mergePrices(responses, getStationKey);
168154

169155
stations.forEach(station => fillMissingPrices(config, station, maxPricesByType));
170156

apis/gasbuddy.js

+10-22
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const { parse } = require('node-html-parser');
2525
*/
2626
const Log = require('logger');
2727

28-
const { fillMissingPrices, sortByPrice } = require('./utils');
28+
const { fillMissingPrices, mergePrices, sortByPrice } = require('./utils');
2929

3030
const BASE_URL = 'https://www.gasbuddy.com';
3131
const TYPES = {
@@ -130,29 +130,17 @@ async function getAllStations() {
130130
}
131131

132132
/**
133-
* @function mergePrices
134-
* @description Merges fuel prices of different types of gas station
133+
* @function getStationKey
134+
* @description Helper to retrieve unique station key.
135135
*
136-
* @param {Object[]} responses - List of gas stations with prices of single fuel type.
136+
* @param {Object} station - Station
137137
*
138-
* @returns {Object} Returns gas stations with merged prices and max prices per fuel type.
138+
* @returns {string} Returns unique station key.
139+
*
140+
* @see apis/README.md
139141
*/
140-
function mergePrices(responses) {
141-
const { indexedStations, maxPricesByType } = responses.reduce(({ indexedStations, maxPricesByType }, station) => {
142-
if (!indexedStations[station.stationId]) {
143-
indexedStations[station.stationId] = station;
144-
} else {
145-
indexedStations[station.stationId].prices[station.fuelType] = station.prices[station.fuelType];
146-
}
147-
148-
if (!maxPricesByType[station.fuelType] || maxPricesByType[station.fuelType] < station.prices[station.fuelType]) {
149-
maxPricesByType[station.fuelType] = station.prices[station.fuelType];
150-
}
151-
152-
return { indexedStations, maxPricesByType };
153-
}, { indexedStations: {}, maxPricesByType: {} });
154-
155-
return { stations: Object.values(indexedStations), maxPricesByType };
142+
function getStationKey(station) {
143+
return station.stationId;
156144
}
157145

158146
/**
@@ -167,7 +155,7 @@ function mergePrices(responses) {
167155
async function getData() {
168156
const responses = await getAllStations();
169157

170-
const { stations, maxPricesByType } = mergePrices(responses);
158+
const { stations, maxPricesByType } = mergePrices(responses, getStationKey);
171159

172160
stations.forEach(station => fillMissingPrices(config, station, maxPricesByType));
173161

apis/utils/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,43 @@ function sortByPrice(config, a, b) {
7171
return 0;
7272
}
7373

74+
/**
75+
* @function mergePrices
76+
* @description Merges fuel prices of different types of gas station
77+
*
78+
* @param {Object[]} responses - List of gas stations with prices of single fuel type.
79+
* @param {function} getStationKeyFunc - Helper to retrieve unique station key.
80+
*
81+
* @returns {Object} Returns gas stations with merged prices and max prices per fuel type.
82+
*/
83+
function mergePrices(responses, getStationKeyFunc) {
84+
const { indexedStations, maxPricesByType } = responses.reduce(({ indexedStations, maxPricesByType }, station) => {
85+
const stationKey = getStationKeyFunc(station);
86+
87+
if (!indexedStations[stationKey]) {
88+
indexedStations[stationKey] = station;
89+
} else {
90+
indexedStations[stationKey].prices[station.fuelType] = station.prices[station.fuelType];
91+
}
92+
93+
if (!maxPricesByType[station.fuelType] || maxPricesByType[station.fuelType] < station.prices[station.fuelType]) {
94+
maxPricesByType[station.fuelType] = station.prices[station.fuelType];
95+
}
96+
97+
return { indexedStations, maxPricesByType };
98+
}, { indexedStations: {}, maxPricesByType: {} });
99+
100+
return { stations: Object.values(indexedStations), maxPricesByType };
101+
}
102+
74103
/**
75104
* @module apis/utils
76105
* @description Utility functions for API integrations.
77106
*/
78107
module.exports = {
79108
fillMissingPrices,
80109
filterStations,
110+
mergePrices,
81111
sortByDistance,
82112
sortByPrice
83113
};

0 commit comments

Comments
 (0)