Skip to content

Commit eafb711

Browse files
authored
Merge pull request #118 from fewieden/develop
v2.5.0
2 parents 5a818da + bfc563c commit eafb711

12 files changed

+92
-101
lines changed

CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# MMM-Fuel Changelog
22

3+
## [2.5.0]
4+
5+
Thanks to [marcx28](https://github.com/marcx28) for their contributions.
6+
7+
### Fixed
8+
9+
* [Sorting by price](https://github.com/fewieden/MMM-Fuel/issues/110)
10+
11+
### Added
12+
13+
* Dependency: `lodash`
14+
15+
### Changed
16+
17+
* [Allow custom header text](https://github.com/fewieden/MMM-Fuel/pull/102)
18+
319
## [2.4.0]
420

521
### Added

MMM-Fuel.js

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Module.register('MMM-Fuel', {
161161

162162
return {
163163
config: this.config,
164+
header: this.data.header,
164165
priceList: this.priceList,
165166
sortByPrice: this.sortByPrice,
166167
gasStations

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ Gas Station Price Module for MagicMirror<sup>2</sup>
1313
* An installation of [MagicMirror<sup>2</sup>](https://github.com/MichMich/MagicMirror)
1414
* OPTIONAL: [Voice Control](https://github.com/fewieden/MMM-voice) and [MMM-Modal](https://github.com/fewieden/MMM-Modal)
1515
* npm
16-
* [node-fetch](https://www.npmjs.com/package/node-fetch)
16+
* [lodash](https://www.npmjs.com/package/lodash)
1717
* [moment](https://www.npmjs.com/package/moment)
18+
* [node-fetch](https://www.npmjs.com/package/node-fetch)
1819
* [node-html-parser](https://www.npmjs.com/package/node-html-parser)
1920

2021
## Installation
@@ -60,7 +61,7 @@ Gas Station Price Module for MagicMirror<sup>2</sup>
6061
| `showOpenOnly` | `false` | Boolean to show only open gas stations or all. |
6162
| `showDistance` | `true` | Boolean to show the distance to your specified position. |
6263
| `showBrand` | `false` | Boolean to show the brand instead of the name. |
63-
| `iconHeader` | `true` | Boolean to display the car icon in the header. |
64+
| `iconHeader` | `true` | Boolean to display the car icon in the header (works only if no custom header is set). |
6465
| `rotate` | `true` | Boolean to enable/disable rotation between sort by price and distance. |
6566
| `rotateInterval` | `60000` (1 min) | How fast the sorting should be switched between byPrice and byDistance. |
6667
| `updateInterval` | `900000` (15 mins) | How often should the data be fetched. **If your value is to small, you risk to get banned from the API provider. I suggest a minimum of 15mins** |

apis/autoblog.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
* @see https://github.com/fewieden/MMM-Fuel
88
*/
99

10+
/**
11+
* @external lodash
12+
* @see https://www.npmjs.com/package/lodash
13+
*/
14+
const _ = require('lodash');
15+
1016
/**
1117
* @external node-fetch
1218
* @see https://www.npmjs.com/package/node-fetch
@@ -19,7 +25,7 @@ const fetch = require('node-fetch');
1925
*/
2026
const { parse } = require('node-html-parser');
2127

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

2430
const BASE_URL = 'https://www.autoblog.com';
2531
const MAX_PAGE = 2;
@@ -156,15 +162,12 @@ async function getData() {
156162

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

159-
const stationsSortedByDistance = filteredStations.sort(sortByDistance);
160-
const stationsSortedByPrice = [...stationsSortedByDistance].sort(sortByPrice.bind(null, config));
161-
162165
return {
163166
types: ['regular', 'premium', 'mid-grade', 'diesel'],
164167
unit: 'mile',
165168
currency: 'USD',
166-
byPrice: stationsSortedByPrice,
167-
byDistance: stationsSortedByDistance
169+
byPrice: _.sortBy(filteredStations, sortByPrice.bind(null, config)),
170+
byDistance: _.sortBy(filteredStations, 'distance')
168171
};
169172
}
170173

apis/gasbuddy.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
* @see https://github.com/fewieden/MMM-Fuel
88
*/
99

10+
/**
11+
* @external lodash
12+
* @see https://www.npmjs.com/package/lodash
13+
*/
14+
const _ = require('lodash');
15+
1016
/**
1117
* @external node-fetch
1218
* @see https://www.npmjs.com/package/node-fetch
@@ -160,7 +166,7 @@ async function getData() {
160166
stations.forEach(station => fillMissingPrices(config, station, maxPricesByType));
161167

162168
// Webpage doesn't support distance (only zip code).
163-
const stationsSortedByPrice = stations.sort(sortByPrice.bind(null, config));
169+
const stationsSortedByPrice = _.sortBy(stations, sortByPrice.bind(null, config));
164170
const stationsSortedByDistance = stationsSortedByPrice;
165171

166172
return {

apis/nsw.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
* @see https://github.com/fewieden/MMM-Fuel
88
*/
99

10+
/**
11+
* @external lodash
12+
* @see https://www.npmjs.com/package/lodash
13+
*/
14+
const _ = require('lodash');
15+
1016
/**
1117
* @external node-fetch
1218
* @see https://www.npmjs.com/package/node-fetch
@@ -25,7 +31,7 @@ const moment = require('moment');
2531
*/
2632
const Log = require('logger');
2733

28-
const { filterStations, sortByDistance } = require('./utils');
34+
const { filterStations } = require('./utils');
2935

3036
const SECOND = 1000;
3137
const MINUTE = 60 * SECOND;
@@ -126,15 +132,12 @@ async function getData() {
126132

127133
stations = stations.filter(filterStations);
128134

129-
const distance = stations.slice(0);
130-
distance.sort(sortByDistance);
131-
132135
return {
133136
types: ['diesel', 'e5'],
134137
unit: 'kilometer',
135138
currency: 'AUD',
136139
byPrice: stations,
137-
byDistance: distance
140+
byDistance: _.sortBy(stations, 'distance')
138141
};
139142
}
140143

apis/spritpreisrechner.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
* @see https://github.com/fewieden/MMM-Fuel
88
*/
99

10+
/**
11+
* @external lodash
12+
* @see https://www.npmjs.com/package/lodash
13+
*/
14+
const _ = require('lodash');
15+
1016
/**
1117
* @external node-fetch
1218
* @see https://www.npmjs.com/package/node-fetch
1319
*/
1420
const fetch = require('node-fetch');
1521

16-
const { filterStations, sortByDistance } = require('./utils');
22+
const { filterStations } = require('./utils');
1723

1824
const BASE_URL = 'https://api.e-control.at/sprit/1.0';
1925
const TYPES = {
@@ -176,15 +182,12 @@ async function getData() {
176182

177183
stations = stations.filter(filterStations);
178184

179-
const distance = stations.slice(0);
180-
distance.sort(sortByDistance);
181-
182185
return {
183186
types: ['diesel', 'e5', 'gas'],
184187
unit: 'kilometer',
185188
currency: 'EUR',
186189
byPrice: stations,
187-
byDistance: distance
190+
byDistance: _.sortBy(stations, 'distance')
188191
};
189192
}
190193

apis/tankerkoenig.js

+10-46
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
* @see https://github.com/fewieden/MMM-Fuel
88
*/
99

10+
/**
11+
* @external lodash
12+
* @see https://www.npmjs.com/package/lodash
13+
*/
14+
const _ = require('lodash');
15+
1016
/**
1117
* @external node-fetch
1218
* @see https://www.npmjs.com/package/node-fetch
@@ -19,6 +25,8 @@ const fetch = require('node-fetch');
1925
*/
2026
const Log = require('logger');
2127

28+
const { sortByPrice } = require('./utils');
29+
2230
const BASE_URL = 'https://creativecommons.tankerkoenig.de/json';
2331

2432
let config;
@@ -59,44 +67,6 @@ function generateStationInfoUrl(id) {
5967
return `${BASE_URL}/detail.php?id=${id}&apikey=${config.api_key}`;
6068
}
6169

62-
/**
63-
* @function sortByPrice
64-
* @description Helper function to sort gas stations by price.
65-
*
66-
* @param {Object} a - Gas Station
67-
* @param {Object} b - Gas Station
68-
*
69-
* @returns {number} Sorting weight.
70-
*/
71-
function sortByPrice(a, b) {
72-
if (b[config.sortBy] === 0) {
73-
return Number.MIN_SAFE_INTEGER;
74-
} else if (a[config.sortBy] === 0) {
75-
return Number.MAX_SAFE_INTEGER;
76-
}
77-
78-
return a[config.sortBy] - b[config.sortBy];
79-
}
80-
81-
/**
82-
* @function sortByDistance
83-
* @description Helper function to sort gas stations by distance.
84-
*
85-
* @param {Object} a - Gas Station
86-
* @param {Object} b - Gas Station
87-
*
88-
* @returns {number} Sorting weight.
89-
*/
90-
function sortByDistance(a, b) {
91-
if (b.dist === 0) {
92-
return Number.MIN_SAFE_INTEGER;
93-
} else if (a.dist === 0) {
94-
return Number.MAX_SAFE_INTEGER;
95-
}
96-
97-
return a.dist - b.dist;
98-
}
99-
10070
/**
10171
* @function filterStations
10272
* @description Helper function to filter gas stations.
@@ -323,18 +293,12 @@ async function getData() {
323293
const stationsFiltered = stations.filter(filterStations);
324294
stationsFiltered.forEach(normalizeStations);
325295

326-
const distance = stationsFiltered.slice(0);
327-
distance.sort(sortByDistance);
328-
329-
const price = stationsFiltered.slice(0);
330-
price.sort(sortByPrice);
331-
332296
return {
333297
types: ['diesel', 'e5', 'e10'],
334298
unit: 'kilometer',
335299
currency: 'EUR',
336-
byPrice: price,
337-
byDistance: distance
300+
byPrice: _.sortBy(stationsFiltered, sortByPrice.bind(null, config)),
301+
byDistance: _.sortBy(stationsFiltered, 'dist')
338302
};
339303
}
340304

apis/utils/index.js

+15-24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
* @see https://github.com/fewieden/MMM-Fuel
88
*/
99

10+
/**
11+
* @external lodash
12+
* @see https://www.npmjs.com/package/lodash
13+
*/
14+
const _ = require('lodash');
15+
1016
/**
1117
* @function filterStations
1218
* @description Helper function to filter gas stations.
@@ -16,22 +22,9 @@
1622
* @returns {boolean} To keep or filter the station.
1723
*/
1824
function filterStations(station) {
19-
const prices = Object.keys(station.prices);
25+
const prices = _.keys(station.prices);
2026

21-
return !prices.every(type => station.prices[type] === -1);
22-
}
23-
24-
/**
25-
* @function sortByDistance
26-
* @description Helper function to sort gas stations by distance.
27-
*
28-
* @param {Object} a - Gas Station
29-
* @param {Object} b - Gas Station
30-
*
31-
* @returns {number} Sorting weight.
32-
*/
33-
function sortByDistance(a, b) {
34-
return a.distance - b.distance;
27+
return !_.every(prices, type => _.get(station, `prices.${type}`) === -1);
3528
}
3629

3730
/**
@@ -45,8 +38,8 @@ function sortByDistance(a, b) {
4538
*/
4639
function fillMissingPrices(config, station, maxPricesByType) {
4740
for (const type of config.types) {
48-
if (!station.prices[type]) {
49-
station.prices[type] = `>${maxPricesByType[type]}`;
41+
if (!_.get(station, `prices.${type}`)) {
42+
_.set(station, `prices.${type}`, `>${maxPricesByType[type]}`);
5043
}
5144
}
5245
}
@@ -60,15 +53,14 @@ function fillMissingPrices(config, station, maxPricesByType) {
6053
*
6154
* @returns {number} Sorting weight.
6255
*/
63-
function sortByPrice(config, a, b) {
64-
const aPrice = a.prices[config.sortBy];
65-
const bPrice = b.prices[config.sortBy];
56+
function sortByPrice(config, station) {
57+
const price = _.get(station, `prices.${config.sortBy}`);
6658

67-
if (!isNaN(aPrice) || !isNaN(bPrice)) {
68-
return isNaN(aPrice) ? 1 : -1;
59+
if (_.isNumber(price)) {
60+
return price;
6961
}
7062

71-
return 0;
63+
return Number.POSITIVE_INFINITY;
7264
}
7365

7466
/**
@@ -108,6 +100,5 @@ module.exports = {
108100
fillMissingPrices,
109101
filterStations,
110102
mergePrices,
111-
sortByDistance,
112103
sortByPrice
113104
};

package-lock.json

+4-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mmm-fuel",
3-
"version": "2.4.0",
3+
"version": "2.5.0",
44
"description": "Gas Station price Module for MagicMirror2",
55
"scripts": {
66
"lint": "eslint . && stylelint **/*.css",
@@ -29,6 +29,7 @@
2929
"stylelint-config-standard": "^27.0.0"
3030
},
3131
"dependencies": {
32+
"lodash": "^4.17.21",
3233
"moment": "^2.29.4",
3334
"node-fetch": "^2.6.7",
3435
"node-html-parser": "^5.4.1"

0 commit comments

Comments
 (0)