From 0e54d67a6d32dd7accfa2075412364910bc689d2 Mon Sep 17 00:00:00 2001 From: Teppo Kurki Date: Mon, 20 Nov 2023 22:24:21 +0200 Subject: [PATCH] fix: pass null through true/mag conversions Input value null should result in null output values. Fixes #120. --- README.md | 2 +- calcs/courseOverGroundMagnetic.js | 10 ++++++++++ calcs/courseOverGroundTrue.js | 14 +++++++++++++- calcs/headingTrue.js | 11 ++++++++++- calcs/windDirectionMagnetic.js | 11 ++++++++++- 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1ddfbe4..da08b65 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ It currently calculates: * True Wind Direction (based on AWA and headingTrue) * Ground Wind Angle and Speed (based on SOG, AWA and AWS) * Magnetic Wind Direction (based on AWA and headingMagnetic) - * Magnetic Wind Direction (based on wind.directionTrue and magneticVarition) + * Magnetic Wind Direction (based on wind.directionTrue and magneticVariation) * Wind Shift (experimental) * Moon illumination and times (based on time and navigation.position) * Sunlight Times: sunrise, sunriseEnd, goldenHourEnd, solarNoon, goldenHour, sunsetStart, sunset, dusk, nauticalDusk, night, nadir, nightEnd, nauticalDawn, dawn (based on time and navigation.position) diff --git a/calcs/courseOverGroundMagnetic.js b/calcs/courseOverGroundMagnetic.js index 631f2b4..f79bf3b 100644 --- a/calcs/courseOverGroundMagnetic.js +++ b/calcs/courseOverGroundMagnetic.js @@ -19,6 +19,12 @@ module.exports = function (app, plugin) { return } } + if ( + _.isUndefined(courseOverGroundTrue) || + courseOverGroundTrue === null + ) { + return [{ path: 'navigation.courseOverGroundMagnetic', value: null }] + } var courseOverGroundMagnetic = courseOverGroundTrue - magneticVariation if (courseOverGroundMagnetic < 0) { courseOverGroundMagnetic = Math.PI * 2 + courseOverGroundMagnetic @@ -36,6 +42,10 @@ module.exports = function (app, plugin) { { input: [1.0, 0.1], expected: [{ path: 'navigation.courseOverGroundMagnetic', value: 0.9 }] + }, + { + input: [null, -0.01], + expected: [{ path: 'navigation.courseOverGroundMagnetic', value: null }] } ] } diff --git a/calcs/courseOverGroundTrue.js b/calcs/courseOverGroundTrue.js index 2589d28..e520bb9 100644 --- a/calcs/courseOverGroundTrue.js +++ b/calcs/courseOverGroundTrue.js @@ -19,6 +19,12 @@ module.exports = function (app, plugin) { return } } + if ( + _.isUndefined(courseOverGroundMagnetic) || + courseOverGroundMagnetic === null + ) { + return [{ path: 'navigation.courseOverGroundTrue', value: null }] + } var courseOverGroundTrue = courseOverGroundMagnetic + magneticVariation if (courseOverGroundTrue < 0) { courseOverGroundTrue = Math.PI * 2 + courseOverGroundTrue @@ -28,6 +34,12 @@ module.exports = function (app, plugin) { return [ { path: 'navigation.courseOverGroundTrue', value: courseOverGroundTrue } ] - } + }, + tests: [ + { + input: [null, 0.01], + expected: [{ path: 'navigation.courseOverGroundTrue', value: null }] + } + ] } } diff --git a/calcs/headingTrue.js b/calcs/headingTrue.js index 05b1fbb..946ebb6 100644 --- a/calcs/headingTrue.js +++ b/calcs/headingTrue.js @@ -16,6 +16,9 @@ module.exports = function (app, plugin) { return } } + if (_.isUndefined(heading) || heading === null) { + return [{ path: 'navigation.headingTrue', value: null }] + } var headingTrue = heading + magneticVariation if (headingTrue < 0) { headingTrue = Math.PI * 2 + headingTrue @@ -23,6 +26,12 @@ module.exports = function (app, plugin) { headingTrue = headingTrue - Math.PI * 2 } return [{ path: 'navigation.headingTrue', value: headingTrue }] - } + }, + tests: [ + { + input: [null, 0.01], + expected: [{ path: 'navigation.headingTrue', value: null }] + } + ] } } diff --git a/calcs/windDirectionMagnetic.js b/calcs/windDirectionMagnetic.js index a864b73..986f737 100644 --- a/calcs/windDirectionMagnetic.js +++ b/calcs/windDirectionMagnetic.js @@ -8,6 +8,9 @@ module.exports = function (app, plugin) { 'navigation.magneticVariation' ], calculator: function (directionTrue, magneticVariation) { + if (directionTrue === null) { + return [{ path: 'environment.wind.directionMagnetic', value: null }] + } var directionMagnetic = directionTrue - magneticVariation if (directionMagnetic < 0) { directionMagnetic = Math.PI * 2 + directionMagnetic @@ -17,6 +20,12 @@ module.exports = function (app, plugin) { return [ { path: 'environment.wind.directionMagnetic', value: directionMagnetic } ] - } + }, + tests: [ + { + input: [null, -0.01], + expected: [{ path: 'environment.wind.directionMagnetic', value: null }] + } + ] } }