Skip to content

Commit

Permalink
Move feelsLke calculation into utils and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
veeck committed Jan 15, 2023
1 parent 3a20b2e commit b328b19
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
21 changes: 1 addition & 20 deletions modules/default/weather/weatherobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,7 @@ class WeatherObject {
if (this.feelsLikeTemp) {
return this.feelsLikeTemp;
}
const windInMph = WeatherUtils.convertWind(this.windSpeed, "imperial");
const tempInF = WeatherUtils.convertTemp(this.temperature, "imperial");
let feelsLike = tempInF;

if (windInMph > 3 && tempInF < 50) {
feelsLike = Math.round(35.74 + 0.6215 * tempInF - 35.75 * Math.pow(windInMph, 0.16) + 0.4275 * tempInF * Math.pow(windInMph, 0.16));
} else if (tempInF > 80 && this.humidity > 40) {
feelsLike =
-42.379 +
2.04901523 * tempInF +
10.14333127 * this.humidity -
0.22475541 * tempInF * this.humidity -
6.83783 * Math.pow(10, -3) * tempInF * tempInF -
5.481717 * Math.pow(10, -2) * this.humidity * this.humidity +
1.22874 * Math.pow(10, -3) * tempInF * tempInF * this.humidity +
8.5282 * Math.pow(10, -4) * tempInF * this.humidity * this.humidity -
1.99 * Math.pow(10, -6) * tempInF * tempInF * this.humidity * this.humidity;
}

return ((feelsLike - 32) * 5) / 9;
return WeatherUtils.calculateFeelsLike(this.temperature, this.windSpeed, this.humidity);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions modules/default/weather/weatherutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ const WeatherUtils = {

convertWindToMs(kmh) {
return kmh * 0.27777777777778;
},

calculateFeelsLike(temperature, windSpeed, humidity) {
const windInMph = this.convertWind(windSpeed, "imperial");
const tempInF = this.convertTemp(temperature, "imperial");
let feelsLike = tempInF;

if (windInMph > 3 && tempInF < 50) {
feelsLike = Math.round(35.74 + 0.6215 * tempInF - 35.75 * Math.pow(windInMph, 0.16) + 0.4275 * tempInF * Math.pow(windInMph, 0.16));
} else if (tempInF > 80 && humidity > 40) {
feelsLike =
-42.379 +
2.04901523 * tempInF +
10.14333127 * humidity -
0.22475541 * tempInF * humidity -
6.83783 * Math.pow(10, -3) * tempInF * tempInF -
5.481717 * Math.pow(10, -2) * humidity * humidity +
1.22874 * Math.pow(10, -3) * tempInF * tempInF * humidity +
8.5282 * Math.pow(10, -4) * tempInF * humidity * humidity -
1.99 * Math.pow(10, -6) * tempInF * tempInF * humidity * humidity;
}

return ((feelsLike - 32) * 5) / 9;
}
};

Expand Down
11 changes: 10 additions & 1 deletion tests/unit/functions/weather_object_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ describe("WeatherObject", () => {
expect(weatherobject.nextSunAction()).toBe("sunset");
});

it("should return an already defined feelsLike info", () => {
weatherobject.feelsLikeTemp = "feelsLikeTempValue";
expect(weatherobject.feelsLike()).toBe("feelsLikeTempValue");
});

afterAll(() => {
moment.tz.setDefault(originalTimeZone);
});
});

describe("WeatherObject", () => {
describe("WeatherUtils", () => {
it("should convert windspeed correctly from mph to mps", () => {
expect(Math.round(WeatherUtils.convertWindToMetric(93.951324266285))).toBe(42);
});
Expand All @@ -55,4 +60,8 @@ describe("WeatherObject", () => {
it("should convert wind direction correctly from cardinal to value", () => {
expect(WeatherUtils.convertWindDirection("SSE")).toBe(157);
});

it("should return a calculated feelsLike info", () => {
expect(WeatherUtils.calculateFeelsLike(0, 20, 40)).toBe(-9.444444444444445);
});
});

0 comments on commit b328b19

Please sign in to comment.