From 8db1a675157fe76dac9377fed62c5bc28815e2d6 Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Sun, 3 May 2026 10:36:47 +0200 Subject: [PATCH 1/3] refactor(weather + compliments): remove unnecessary conditionals --- defaultmodules/compliments/compliments.js | 2 +- defaultmodules/weather/weatherutils.js | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/defaultmodules/compliments/compliments.js b/defaultmodules/compliments/compliments.js index 2ded7c191b..b591780630 100644 --- a/defaultmodules/compliments/compliments.js +++ b/defaultmodules/compliments/compliments.js @@ -147,7 +147,7 @@ Module.register("compliments", { timeOfDay = "evening"; } - if (timeOfDay && this.config.compliments.hasOwnProperty(timeOfDay)) { + if (this.config.compliments.hasOwnProperty(timeOfDay)) { compliments = [...this.config.compliments[timeOfDay]]; } diff --git a/defaultmodules/weather/weatherutils.js b/defaultmodules/weather/weatherutils.js index 7d1436c394..99790c4f1e 100644 --- a/defaultmodules/weather/weatherutils.js +++ b/defaultmodules/weather/weatherutils.js @@ -187,14 +187,12 @@ const WeatherUtils = { let imperialWeatherObject = { ...weatherObject }; - if (imperialWeatherObject) { - if (imperialWeatherObject.feelsLikeTemp) imperialWeatherObject.feelsLikeTemp = this.convertTemp(imperialWeatherObject.feelsLikeTemp, "imperial"); - if (imperialWeatherObject.maxTemperature) imperialWeatherObject.maxTemperature = this.convertTemp(imperialWeatherObject.maxTemperature, "imperial"); - if (imperialWeatherObject.minTemperature) imperialWeatherObject.minTemperature = this.convertTemp(imperialWeatherObject.minTemperature, "imperial"); - if (imperialWeatherObject.precipitationAmount) imperialWeatherObject.precipitationAmount = this.convertPrecipitationToInch(imperialWeatherObject.precipitationAmount, imperialWeatherObject.precipitationUnits); - if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); - if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); - } + if (imperialWeatherObject.feelsLikeTemp) imperialWeatherObject.feelsLikeTemp = this.convertTemp(imperialWeatherObject.feelsLikeTemp, "imperial"); + if (imperialWeatherObject.maxTemperature) imperialWeatherObject.maxTemperature = this.convertTemp(imperialWeatherObject.maxTemperature, "imperial"); + if (imperialWeatherObject.minTemperature) imperialWeatherObject.minTemperature = this.convertTemp(imperialWeatherObject.minTemperature, "imperial"); + if (imperialWeatherObject.precipitationAmount) imperialWeatherObject.precipitationAmount = this.convertPrecipitationToInch(imperialWeatherObject.precipitationAmount, imperialWeatherObject.precipitationUnits); + if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); + if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); return imperialWeatherObject; } From 1ae16255b99a85ace3ae20d8c3252b194e1acae0 Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Sun, 3 May 2026 10:36:47 +0200 Subject: [PATCH 2/3] test(weather): add unit tests for convertWeatherObjectToImperial --- .../default/weather/weather_utils_spec.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/unit/modules/default/weather/weather_utils_spec.js b/tests/unit/modules/default/weather/weather_utils_spec.js index 5f21121994..98c64b74b0 100644 --- a/tests/unit/modules/default/weather/weather_utils_spec.js +++ b/tests/unit/modules/default/weather/weather_utils_spec.js @@ -113,4 +113,51 @@ describe("Weather utils tests", () => { } }); }); + + describe("convertWeatherObjectToImperial", () => { + it("should return null for null or empty input", () => { + expect(WeatherUtils.convertWeatherObjectToImperial(null)).toBeNull(); + expect(WeatherUtils.convertWeatherObjectToImperial({})).toBeNull(); + }); + + it("should convert 0°C correctly to 32°F", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ temperature: 0 }); + expect(result.temperature).toBe(32); + }); + + it("should convert all temperature fields", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ + temperature: 0, + feelsLikeTemp: 0, + minTemperature: -10, + maxTemperature: 10 + }); + expect(result.temperature).toBe(32); + expect(result.feelsLikeTemp).toBe(32); + expect(result.minTemperature).toBe(14); + expect(result.maxTemperature).toBe(50); + }); + + it("should convert windSpeed correctly", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ windSpeed: 10 }); + expect(result.windSpeed).toBeCloseTo(22.369, 2); + }); + + it("should convert precipitationAmount correctly", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ precipitationAmount: 25.4, precipitationUnits: "mm" }); + expect(result.precipitationAmount).toBeCloseTo(1, 5); + }); + + it("should not modify properties that are not present", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ temperature: 20 }); + expect("windSpeed" in result).toBe(false); + expect("feelsLikeTemp" in result).toBe(false); + }); + + it("should not mutate the original object", () => { + const input = { temperature: 20 }; + WeatherUtils.convertWeatherObjectToImperial(input); + expect(input.temperature).toBe(20); + }); + }); }); From b58c6ffcde0d0e40b055718a3538df1098561c34 Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Sun, 3 May 2026 10:36:48 +0200 Subject: [PATCH 3/3] fix(weather): use 'in' operator for property existence checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Falsy checks (e.g. 'if (obj.temperature)') incorrectly skip conversion when the value is 0, which is a valid temperature (0°C = 32°F). Using the 'in' operator checks for property existence instead. --- defaultmodules/weather/weatherutils.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/defaultmodules/weather/weatherutils.js b/defaultmodules/weather/weatherutils.js index 99790c4f1e..4f1abc9a4f 100644 --- a/defaultmodules/weather/weatherutils.js +++ b/defaultmodules/weather/weatherutils.js @@ -187,12 +187,12 @@ const WeatherUtils = { let imperialWeatherObject = { ...weatherObject }; - if (imperialWeatherObject.feelsLikeTemp) imperialWeatherObject.feelsLikeTemp = this.convertTemp(imperialWeatherObject.feelsLikeTemp, "imperial"); - if (imperialWeatherObject.maxTemperature) imperialWeatherObject.maxTemperature = this.convertTemp(imperialWeatherObject.maxTemperature, "imperial"); - if (imperialWeatherObject.minTemperature) imperialWeatherObject.minTemperature = this.convertTemp(imperialWeatherObject.minTemperature, "imperial"); - if (imperialWeatherObject.precipitationAmount) imperialWeatherObject.precipitationAmount = this.convertPrecipitationToInch(imperialWeatherObject.precipitationAmount, imperialWeatherObject.precipitationUnits); - if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); - if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); + if ("feelsLikeTemp" in imperialWeatherObject) imperialWeatherObject.feelsLikeTemp = this.convertTemp(imperialWeatherObject.feelsLikeTemp, "imperial"); + if ("maxTemperature" in imperialWeatherObject) imperialWeatherObject.maxTemperature = this.convertTemp(imperialWeatherObject.maxTemperature, "imperial"); + if ("minTemperature" in imperialWeatherObject) imperialWeatherObject.minTemperature = this.convertTemp(imperialWeatherObject.minTemperature, "imperial"); + if ("precipitationAmount" in imperialWeatherObject) imperialWeatherObject.precipitationAmount = this.convertPrecipitationToInch(imperialWeatherObject.precipitationAmount, imperialWeatherObject.precipitationUnits); + if ("temperature" in imperialWeatherObject) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); + if ("windSpeed" in imperialWeatherObject) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); return imperialWeatherObject; }