Skip to content

Commit

Permalink
extracts duplicated function and removes double empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
fewieden committed Aug 13, 2022
1 parent 2cf26b7 commit 4bb4964
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 82 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ No API key required. The displayed distance is not based on your coordinates but
| `showOpenOnly` | `false` | Not supported |
| `showBrand` | `false` | Not supported |


### gasbuddy (USA and Canada only)

No API key required. The displayed distance is not based on your coordinates but on the zip code.
Expand All @@ -137,7 +136,6 @@ No API key required. The displayed distance is not based on your coordinates but
| `showOpenOnly` | `false` | Not supported |
| `showBrand` | `false` | Not supported |


### nsw (Australia NSW and TAS only)

This provider gives no information if the gas stations are open or closed.
Expand Down
45 changes: 5 additions & 40 deletions apis/autoblog.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const fetch = require('node-fetch');
*/
const { parse } = require('node-html-parser');

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

const BASE_URL = 'https://www.autoblog.com';
const MAX_PAGE = 2;

Expand Down Expand Up @@ -59,43 +61,6 @@ function mapGasStation(htmlGasStation, type) {
};
}

/**
* @function fillMissingPrices
* @description Replaces missing price information with max price for type.
*
* @param {Object} station - Gas Station
* @param {Object} maxPricesByType - Maximum price per fuel type.
*
* @returns {void}
*/
function fillMissingPrices(station, maxPricesByType) {
for (const type of config.types) {
if (!station.prices[type]) {
station.prices[type] = `>${maxPricesByType[type]}`;
}
}
}

/**
* @function sortByPrice
* @description Helper function to sort gas stations by price.
*
* @param {Object} a - Gas Station
* @param {Object} b - Gas Station
*
* @returns {number} Sorting weight.
*/
function sortByPrice(a, b) {
const aPrice = a.prices[config.sortBy];
const bPrice = b.prices[config.sortBy];

if (!isNaN(aPrice) || !isNaN(bPrice)) {
return isNaN(aPrice) ? 1 : -1;
}

return 0;
}

/**
* @function fetchPaginatedStations
* @description Paginated API requests for specified type.
Expand Down Expand Up @@ -201,12 +166,12 @@ async function getData() {

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

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

const filteredStations = stations.filter(station => station.distance <= config.radius);

const stationsSortedByDistance = filteredStations.sort((a, b) => a.distance - b.distance);
const stationsSortedByPrice = [...stationsSortedByDistance].sort(sortByPrice);
const stationsSortedByDistance = filteredStations.sort(sortByDistance);
const stationsSortedByPrice = [...stationsSortedByDistance].sort(sortByPrice.bind(null, config));

return {
types: ['regular', 'premium', 'mid-grade', 'diesel'],
Expand Down
43 changes: 4 additions & 39 deletions apis/gasbuddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const { parse } = require('node-html-parser');
*/
const Log = require('logger');

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

const BASE_URL = 'https://www.gasbuddy.com';
const TYPES = {
regular: 1,
Expand Down Expand Up @@ -70,43 +72,6 @@ function mapGasStation(htmlGasStation, type) {

}

/**
* @function fillMissingPrices
* @description Replaces missing price information with max price for type.
*
* @param {Object} station - Gas Station
* @param {Object} maxPricesByType - Maximum price per fuel type.
*
* @returns {void}
*/
function fillMissingPrices(station, maxPricesByType) {
for (const type of config.types) {
if (!station.prices[type]) {
station.prices[type] = `>${maxPricesByType[type]}`;
}
}
}

/**
* @function sortByPrice
* @description Helper function to sort gas stations by price.
*
* @param {Object} a - Gas Station
* @param {Object} b - Gas Station
*
* @returns {number} Sorting weight.
*/
function sortByPrice(a, b) {
const aPrice = a.prices[config.sortBy];
const bPrice = b.prices[config.sortBy];

if (!isNaN(aPrice) || !isNaN(bPrice)) {
return isNaN(aPrice) ? -1 : 1;
}

return 0;
}

/**
* @function fetchStations
* @description API requests for specified type.
Expand Down Expand Up @@ -204,10 +169,10 @@ async function getData() {

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

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

// Webpage doesn't support distance (only zip code).
const stationsSortedByPrice = stations.sort(sortByPrice);
const stationsSortedByPrice = stations.sort(sortByPrice.bind(null, config));
const stationsSortedByDistance = stationsSortedByPrice;

return {
Expand Down
41 changes: 40 additions & 1 deletion apis/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,50 @@ function sortByDistance(a, b) {
return a.distance - b.distance;
}

/**
* @function fillMissingPrices
* @description Replaces missing price information with max price for type.
*
* @param {Object} station - Gas Station
* @param {Object} maxPricesByType - Maximum price per fuel type.
*
* @returns {void}
*/
function fillMissingPrices(config, station, maxPricesByType) {
for (const type of config.types) {
if (!station.prices[type]) {
station.prices[type] = `>${maxPricesByType[type]}`;
}
}
}

/**
* @function sortByPrice
* @description Helper function to sort gas stations by price.
*
* @param {Object} a - Gas Station
* @param {Object} b - Gas Station
*
* @returns {number} Sorting weight.
*/
function sortByPrice(config, a, b) {
const aPrice = a.prices[config.sortBy];
const bPrice = b.prices[config.sortBy];

if (!isNaN(aPrice) || !isNaN(bPrice)) {
return isNaN(aPrice) ? 1 : -1;
}

return 0;
}

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

0 comments on commit 4bb4964

Please sign in to comment.