From 3d4fec756cf6d1a3bebf30684b476e7d074397ea Mon Sep 17 00:00:00 2001 From: alexzurbonsen Date: Sat, 7 Sep 2024 14:48:05 +0200 Subject: [PATCH] separate options parsing for byteTrace and visitTrace functions Signed-off-by: alexzurbonsen --- src/co2.js | 19 ++++++++---- src/helpers/index.js | 70 ++++++++++++++++++++++++++------------------ 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/co2.js b/src/co2.js index db8f598..894ef6b 100644 --- a/src/co2.js +++ b/src/co2.js @@ -64,10 +64,9 @@ import SustainableWebDesignV3 from "./sustainable-web-design-v3.js"; import SustainableWebDesignV4 from "./sustainable-web-design-v4.js"; import { - GLOBAL_GRID_INTENSITY, - RENEWABLES_GRID_INTENSITY, -} from "./constants/index.js"; -import { parseOptions } from "./helpers/index.js"; + parseByteTraceOptions, + parseVisitTraceOptions, +} from "./helpers/index.js"; class CO2 { constructor(options) { @@ -152,7 +151,11 @@ class CO2 { * @return {CO2EstimateTraceResultPerByte} the amount of CO2 in grammes */ perByteTrace(bytes, green = false, options = {}) { - const adjustments = parseOptions(options, this.model.version, green); + const adjustments = parseByteTraceOptions( + options, + this.model.version, + green + ); // Filter out the trace items that aren't relevant to this function. const { gridIntensity, ...traceVariables } = adjustments; @@ -197,7 +200,11 @@ class CO2 { */ perVisitTrace(bytes, green = false, options = {}) { if (this.model?.perVisit) { - const adjustments = parseOptions(options, this.model.version, green); + const adjustments = parseVisitTraceOptions( + options, + this.model.version, + green + ); const { gridIntensity, ...variables } = adjustments; return { diff --git a/src/helpers/index.js b/src/helpers/index.js index 8ff63f9..cdc13df 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -23,7 +23,7 @@ const formatNumber = (num) => parseFloat(num.toFixed(2)); const lessThanEqualTo = (num, limit) => num <= limit; -function parseOptions(options = {}, version = 3, green = false) { +function parseByteTraceOptions(options = {}, version = 3, green = false) { const globalGridIntensity = version === 4 ? SWDM4_GLOBAL_GRID_INTENSITY : SWDM3_GLOBAL_GRID_INTENSITY; // CHeck that it is an object @@ -131,6 +131,44 @@ function parseOptions(options = {}, version = 3, green = false) { }; } + if ( + options?.greenHostingFactor || + (options.greenHostingFactor === 0 && version === 4) + ) { + if (typeof options.greenHostingFactor === "number") { + if (options.greenHostingFactor >= 0 && options.greenHostingFactor <= 1) { + adjustments.greenHostingFactor = options.greenHostingFactor; + } else { + adjustments.greenHostingFactor = 0; + console.warn( + `The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \nFalling back to default value.` + ); + } + } else { + adjustments.greenHostingFactor = 0; + console.warn( + `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \nFalling back to default value.` + ); + } + } else if (version === 4) { + adjustments.greenHostingFactor = 0; + } + + if (green) { + adjustments.greenHostingFactor = 1; + } + + return adjustments; +} + +function parseVisitTraceOptions(options = {}, version = 3, green = false) { + // CHeck that it is an object + if (typeof options !== "object") { + throw new Error("Options must be an object"); + } + + const adjustments = parseByteTraceOptions(options, version, green); + if (options?.dataReloadRatio || options.dataReloadRatio === 0) { if (typeof options.dataReloadRatio === "number") { if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) { @@ -215,33 +253,6 @@ function parseOptions(options = {}, version = 3, green = false) { ); } - if ( - options?.greenHostingFactor || - (options.greenHostingFactor === 0 && version === 4) - ) { - if (typeof options.greenHostingFactor === "number") { - if (options.greenHostingFactor >= 0 && options.greenHostingFactor <= 1) { - adjustments.greenHostingFactor = options.greenHostingFactor; - } else { - adjustments.greenHostingFactor = 0; - console.warn( - `The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \nFalling back to default value.` - ); - } - } else { - adjustments.greenHostingFactor = 0; - console.warn( - `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \nFalling back to default value.` - ); - } - } else if (version === 4) { - adjustments.greenHostingFactor = 0; - } - - if (green) { - adjustments.greenHostingFactor = 1; - } - return adjustments; } @@ -299,7 +310,8 @@ function outputRating(co2e, swdmVersion) { export { formatNumber, - parseOptions, + parseByteTraceOptions, + parseVisitTraceOptions, getApiRequestHeaders, lessThanEqualTo, outputRating,