Skip to content

Commit

Permalink
extracts mergePrices to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fewieden committed Aug 13, 2022
1 parent 4bb4964 commit 1411d72
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 46 deletions.
34 changes: 10 additions & 24 deletions apis/autoblog.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const fetch = require('node-fetch');
*/
const { parse } = require('node-html-parser');

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

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

/**
* @function mergePrices
* @description Merges fuel prices of different types of gas station
* @function getStationKey
* @description Helper to retrieve unique station key.
*
* @param {Object[]} responses - List of gas stations with prices of single fuel type.
* @param {Object} station - Station
*
* @returns {Object} Returns gas stations with merged prices and max prices per fuel type.
* @returns {string} Returns unique station key.
*
* @see apis/README.md
*/
function mergePrices(responses) {
const { indexedStations, maxPricesByType } = responses.reduce(({ indexedStations, maxPricesByType }, station) => {
const stationKey = `${station.name}-${station.address}`;

if (!indexedStations[stationKey]) {
indexedStations[stationKey] = station;
} else {
indexedStations[stationKey].prices[station.fuelType] = station.prices[station.fuelType];
}

if (!maxPricesByType[station.fuelType] || maxPricesByType[station.fuelType] < station.prices[station.fuelType]) {
maxPricesByType[station.fuelType] = station.prices[station.fuelType];
}

return { indexedStations, maxPricesByType };
}, { indexedStations: {}, maxPricesByType: {} });

return { stations: Object.values(indexedStations), maxPricesByType };
function getStationKey(station) {
return `${station.name}-${station.address}`;
}

/**
Expand All @@ -164,7 +150,7 @@ function mergePrices(responses) {
async function getData() {
const responses = await getAllStations();

const { stations, maxPricesByType } = mergePrices(responses);
const { stations, maxPricesByType } = mergePrices(responses, getStationKey);

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

Expand Down
32 changes: 10 additions & 22 deletions apis/gasbuddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const { parse } = require('node-html-parser');
*/
const Log = require('logger');

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

const BASE_URL = 'https://www.gasbuddy.com';
const TYPES = {
Expand Down Expand Up @@ -130,29 +130,17 @@ async function getAllStations() {
}

/**
* @function mergePrices
* @description Merges fuel prices of different types of gas station
* @function getStationKey
* @description Helper to retrieve unique station key.
*
* @param {Object[]} responses - List of gas stations with prices of single fuel type.
* @param {Object} station - Station
*
* @returns {Object} Returns gas stations with merged prices and max prices per fuel type.
* @returns {string} Returns unique station key.
*
* @see apis/README.md
*/
function mergePrices(responses) {
const { indexedStations, maxPricesByType } = responses.reduce(({ indexedStations, maxPricesByType }, station) => {
if (!indexedStations[station.stationId]) {
indexedStations[station.stationId] = station;
} else {
indexedStations[station.stationId].prices[station.fuelType] = station.prices[station.fuelType];
}

if (!maxPricesByType[station.fuelType] || maxPricesByType[station.fuelType] < station.prices[station.fuelType]) {
maxPricesByType[station.fuelType] = station.prices[station.fuelType];
}

return { indexedStations, maxPricesByType };
}, { indexedStations: {}, maxPricesByType: {} });

return { stations: Object.values(indexedStations), maxPricesByType };
function getStationKey(station) {
return station.stationId;
}

/**
Expand All @@ -167,7 +155,7 @@ function mergePrices(responses) {
async function getData() {
const responses = await getAllStations();

const { stations, maxPricesByType } = mergePrices(responses);
const { stations, maxPricesByType } = mergePrices(responses, getStationKey);

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

Expand Down
30 changes: 30 additions & 0 deletions apis/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,43 @@ function sortByPrice(config, a, b) {
return 0;
}

/**
* @function mergePrices
* @description Merges fuel prices of different types of gas station
*
* @param {Object[]} responses - List of gas stations with prices of single fuel type.
* @param {function} getStationKeyFunc - Helper to retrieve unique station key.
*
* @returns {Object} Returns gas stations with merged prices and max prices per fuel type.
*/
function mergePrices(responses, getStationKeyFunc) {
const { indexedStations, maxPricesByType } = responses.reduce(({ indexedStations, maxPricesByType }, station) => {
const stationKey = getStationKeyFunc(station);

if (!indexedStations[stationKey]) {
indexedStations[stationKey] = station;
} else {
indexedStations[stationKey].prices[station.fuelType] = station.prices[station.fuelType];
}

if (!maxPricesByType[station.fuelType] || maxPricesByType[station.fuelType] < station.prices[station.fuelType]) {
maxPricesByType[station.fuelType] = station.prices[station.fuelType];
}

return { indexedStations, maxPricesByType };
}, { indexedStations: {}, maxPricesByType: {} });

return { stations: Object.values(indexedStations), maxPricesByType };
}

/**
* @module apis/utils
* @description Utility functions for API integrations.
*/
module.exports = {
fillMissingPrices,
filterStations,
mergePrices,
sortByDistance,
sortByPrice
};

0 comments on commit 1411d72

Please sign in to comment.