From 95dadf601d4210c37de8c4e10dc92cb9cfcfd978 Mon Sep 17 00:00:00 2001 From: Matthew Oslan Date: Thu, 27 Jun 2019 22:58:33 -0400 Subject: [PATCH] Document that AdjustmentOptions are non-nullable --- routes/adjustmentMethods/AdjustmentMethod.ts | 5 ++-- .../RainDelayAdjustmentMethod.ts | 2 +- .../ZimmermanAdjustmentMethod.ts | 26 ++++++++----------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/routes/adjustmentMethods/AdjustmentMethod.ts b/routes/adjustmentMethods/AdjustmentMethod.ts index 374f260..66c1c3a 100644 --- a/routes/adjustmentMethods/AdjustmentMethod.ts +++ b/routes/adjustmentMethods/AdjustmentMethod.ts @@ -5,9 +5,8 @@ import { WeatherProvider } from "../weatherProviders/WeatherProvider"; export interface AdjustmentMethod { /** * Calculates the percentage that should be used to scale watering time. - * @param adjustmentOptions The user-specified options for the calculation, or undefined/null if no custom values - * are to be used. No checks will be made to ensure the AdjustmentOptions are the correct type that the function - * is expecting or to ensure that any of its fields are valid. + * @param adjustmentOptions The user-specified options for the calculation. No checks will be made to ensure the + * AdjustmentOptions are the correct type that the function is expecting or to ensure that any of its fields are valid. * @param wateringData The basic weather information of the watering site. This may be undefined if an error occurred * while retrieving the data. * @param coordinates The coordinates of the watering site. diff --git a/routes/adjustmentMethods/RainDelayAdjustmentMethod.ts b/routes/adjustmentMethods/RainDelayAdjustmentMethod.ts index 56b1406..4b6a0e0 100644 --- a/routes/adjustmentMethods/RainDelayAdjustmentMethod.ts +++ b/routes/adjustmentMethods/RainDelayAdjustmentMethod.ts @@ -7,7 +7,7 @@ import { WateringData } from "../../types"; */ async function calculateRainDelayWateringScale( adjustmentOptions: RainDelayAdjustmentOptions, wateringData: WateringData | undefined ): Promise< AdjustmentMethodResponse > { const raining = wateringData && wateringData.raining; - const d = adjustmentOptions && adjustmentOptions.hasOwnProperty( "d" ) ? adjustmentOptions.d : 24; + const d = adjustmentOptions.hasOwnProperty( "d" ) ? adjustmentOptions.d : 24; return { scale: undefined, rawData: { raining: raining ? 1 : 0 }, diff --git a/routes/adjustmentMethods/ZimmermanAdjustmentMethod.ts b/routes/adjustmentMethods/ZimmermanAdjustmentMethod.ts index e5268c0..c5ed019 100644 --- a/routes/adjustmentMethods/ZimmermanAdjustmentMethod.ts +++ b/routes/adjustmentMethods/ZimmermanAdjustmentMethod.ts @@ -40,29 +40,25 @@ async function calculateZimmermanWateringScale( adjustmentOptions: ZimmermanAdju let humidityBase = 30, tempBase = 70, precipBase = 0; // Get baseline conditions for 100% water level, if provided - if ( adjustmentOptions ) { - humidityBase = adjustmentOptions.hasOwnProperty( "bh" ) ? adjustmentOptions.bh : humidityBase; - tempBase = adjustmentOptions.hasOwnProperty( "bt" ) ? adjustmentOptions.bt : tempBase; - precipBase = adjustmentOptions.hasOwnProperty( "br" ) ? adjustmentOptions.br : precipBase; - } + humidityBase = adjustmentOptions.hasOwnProperty( "bh" ) ? adjustmentOptions.bh : humidityBase; + tempBase = adjustmentOptions.hasOwnProperty( "bt" ) ? adjustmentOptions.bt : tempBase; + precipBase = adjustmentOptions.hasOwnProperty( "br" ) ? adjustmentOptions.br : precipBase; let humidityFactor = ( humidityBase - wateringData.humidity ), tempFactor = ( ( wateringData.temp - tempBase ) * 4 ), precipFactor = ( ( precipBase - wateringData.precip ) * 200 ); // Apply adjustment options, if provided, by multiplying the percentage against the factor - if ( adjustmentOptions ) { - if ( adjustmentOptions.hasOwnProperty( "h" ) ) { - humidityFactor = humidityFactor * ( adjustmentOptions.h / 100 ); - } + if ( adjustmentOptions.hasOwnProperty( "h" ) ) { + humidityFactor = humidityFactor * ( adjustmentOptions.h / 100 ); + } - if ( adjustmentOptions.hasOwnProperty( "t" ) ) { - tempFactor = tempFactor * ( adjustmentOptions.t / 100 ); - } + if ( adjustmentOptions.hasOwnProperty( "t" ) ) { + tempFactor = tempFactor * ( adjustmentOptions.t / 100 ); + } - if ( adjustmentOptions.hasOwnProperty( "r" ) ) { - precipFactor = precipFactor * ( adjustmentOptions.r / 100 ); - } + if ( adjustmentOptions.hasOwnProperty( "r" ) ) { + precipFactor = precipFactor * ( adjustmentOptions.r / 100 ); } return {