Skip to content

Commit be9c24a

Browse files
refactor(weather): extract cardinalToDegrees() to provider-utils.js
1 parent d54caa6 commit be9c24a

File tree

3 files changed

+33
-48
lines changed

3 files changed

+33
-48
lines changed

defaultmodules/weather/provider-utils.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,33 @@ function convertKmhToMs (kmh) {
124124
return kmh / 3.6;
125125
}
126126

127+
/**
128+
* Convert cardinal wind direction string to degrees
129+
* @param {string} direction - Cardinal direction (e.g., "N", "NNE", "SW")
130+
* @returns {number|null} Direction in degrees (0-360) or null if unknown
131+
*/
132+
function cardinalToDegrees (direction) {
133+
const directions = {
134+
N: 0,
135+
NNE: 22.5,
136+
NE: 45,
137+
ENE: 67.5,
138+
E: 90,
139+
ESE: 112.5,
140+
SE: 135,
141+
SSE: 157.5,
142+
S: 180,
143+
SSW: 202.5,
144+
SW: 225,
145+
WSW: 247.5,
146+
W: 270,
147+
WNW: 292.5,
148+
NW: 315,
149+
NNW: 337.5
150+
};
151+
return directions[direction] ?? null;
152+
}
153+
127154
/**
128155
* Validate and limit coordinate precision
129156
* @param {object} config - Configuration object with lat/lon properties
@@ -149,5 +176,6 @@ module.exports = {
149176
formatTimezoneOffset,
150177
getDateString,
151178
convertKmhToMs,
179+
cardinalToDegrees,
152180
validateCoordinates
153181
};

defaultmodules/weather/providers/weatherapi.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Log = require("logger");
2-
const { convertKmhToMs } = require("../provider-utils");
2+
const { convertKmhToMs, cardinalToDegrees } = require("../provider-utils");
33
const HTTPFetcher = require("#http_fetcher");
44

55
const WEATHER_API_BASE = "https://api.weatherapi.com/v1";
@@ -238,27 +238,6 @@ class WeatherAPIProvider {
238238
return date;
239239
}
240240

241-
#cardinalWindDirection (direction) {
242-
switch (direction) {
243-
case "NNE": return 22.5;
244-
case "NE": return 45;
245-
case "ENE": return 67.5;
246-
case "E": return 90;
247-
case "ESE": return 112.5;
248-
case "SE": return 135;
249-
case "SSE": return 157.5;
250-
case "S": return 180;
251-
case "SSW": return 202.5;
252-
case "SW": return 225;
253-
case "WSW": return 247.5;
254-
case "W": return 270;
255-
case "WNW": return 292.5;
256-
case "NW": return 315;
257-
case "NNW": return 337.5;
258-
default: return 0;
259-
}
260-
}
261-
262241
#toNumber (value) {
263242
const number = parseFloat(value);
264243
return Number.isFinite(number) ? number : null;
@@ -348,7 +327,7 @@ class WeatherAPIProvider {
348327
const windDegree = this.#toNumber(noonHour.wind_degree);
349328
weather.windFromDirection = windDegree !== null
350329
? windDegree
351-
: this.#cardinalWindDirection(noonHour?.wind_dir);
330+
: cardinalToDegrees(noonHour?.wind_dir);
352331
}
353332

354333
const sunrise = this.#parseSunDatetime(forecastDay, "sunrise");
@@ -418,7 +397,7 @@ class WeatherAPIProvider {
418397
const windDegree = this.#toNumber(hourData.wind_degree);
419398
weather.windFromDirection = windDegree !== null
420399
? windDegree
421-
: this.#cardinalWindDirection(hourData.wind_dir);
400+
: cardinalToDegrees(hourData.wind_dir);
422401

423402
weather.weatherType = this.#convertWeatherType(hourData.condition?.code, hourData.is_day === 1);
424403

defaultmodules/weather/providers/weathergov.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Log = require("logger");
2-
const { getSunTimes, isDayTime, getDateString, convertKmhToMs } = require("../provider-utils");
2+
const { getSunTimes, isDayTime, getDateString, convertKmhToMs, cardinalToDegrees } = require("../provider-utils");
33
const HTTPFetcher = require("#http_fetcher");
44

55
/**
@@ -367,7 +367,7 @@ class WeatherGovProvider {
367367
windSpeed = windSpeedStr.split(" ")[0];
368368
}
369369
weather.windSpeed = convertKmhToMs(parseFloat(windSpeed));
370-
weather.windFromDirection = this.#convertWindDirection(forecast.windDirection);
370+
weather.windFromDirection = cardinalToDegrees(forecast.windDirection);
371371
weather.temperature = forecast.temperature;
372372
weather.precipitationProbability = forecast.probabilityOfPrecipitation?.value ?? 0;
373373
weather.weatherType = this.#convertWeatherType(forecast.shortForecast, forecast.isDaytime);
@@ -378,28 +378,6 @@ class WeatherGovProvider {
378378
return hours;
379379
}
380380

381-
#convertWindDirection (direction) {
382-
const directions = {
383-
N: 0,
384-
NNE: 22.5,
385-
NE: 45,
386-
ENE: 67.5,
387-
E: 90,
388-
ESE: 112.5,
389-
SE: 135,
390-
SSE: 157.5,
391-
S: 180,
392-
SSW: 202.5,
393-
SW: 225,
394-
WSW: 247.5,
395-
W: 270,
396-
WNW: 292.5,
397-
NW: 315,
398-
NNW: 337.5
399-
};
400-
return directions[direction] ?? null;
401-
}
402-
403381
#convertWeatherType (weatherType, isDaytime) {
404382
// https://w1.weather.gov/xml/current_obs/weather.php
405383

0 commit comments

Comments
 (0)