diff --git a/apis/autoblog.js b/apis/autoblog.js index 3dbe957..d95d60c 100644 --- a/apis/autoblog.js +++ b/apis/autoblog.js @@ -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; @@ -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}`; } /** @@ -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)); diff --git a/apis/gasbuddy.js b/apis/gasbuddy.js index 38dd56c..c0008a7 100644 --- a/apis/gasbuddy.js +++ b/apis/gasbuddy.js @@ -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 = { @@ -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; } /** @@ -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)); diff --git a/apis/utils/index.js b/apis/utils/index.js index 0783fd2..f2b7b5b 100644 --- a/apis/utils/index.js +++ b/apis/utils/index.js @@ -71,6 +71,35 @@ 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. @@ -78,6 +107,7 @@ function sortByPrice(config, a, b) { module.exports = { fillMissingPrices, filterStations, + mergePrices, sortByDistance, sortByPrice };