Skip to content

Commit

Permalink
Separate options parsing for perByteTrace and perVisitTrace functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fershad authored Nov 30, 2024
2 parents 38b7849 + 4897ca2 commit 2a6369a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
19 changes: 13 additions & 6 deletions src/co2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
70 changes: 41 additions & 29 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,6 +88,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) {
Expand Down Expand Up @@ -172,33 +210,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;
}

Expand Down Expand Up @@ -256,7 +267,8 @@ function outputRating(co2e, swdmVersion) {

export {
formatNumber,
parseOptions,
parseByteTraceOptions,
parseVisitTraceOptions,
getApiRequestHeaders,
lessThanEqualTo,
outputRating,
Expand Down

0 comments on commit 2a6369a

Please sign in to comment.