Skip to content

Commit

Permalink
separate options parsing for byteTrace and visitTrace functions
Browse files Browse the repository at this point in the history
Signed-off-by: alexzurbonsen <[email protected]>
  • Loading branch information
alexzurbonsen committed Sep 7, 2024
1 parent 870e621 commit 8692e57
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
10 changes: 3 additions & 7 deletions src/co2.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ import OneByte from "./1byte.js";
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";
import { parseByteTraceOptions, parseVisitTraceOptions } from "./helpers/index.js"

Check failure on line 66 in src/co2.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Replace `·parseByteTraceOptions,·parseVisitTraceOptions·}·from·"./helpers/index.js"` with `⏎··parseByteTraceOptions,⏎··parseVisitTraceOptions,⏎}·from·"./helpers/index.js";`

Check failure on line 66 in src/co2.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `·parseByteTraceOptions,·parseVisitTraceOptions·}·from·"./helpers/index.js"` with `⏎··parseByteTraceOptions,⏎··parseVisitTraceOptions,⏎}·from·"./helpers/index.js";`

class CO2 {
constructor(options) {
Expand Down Expand Up @@ -152,7 +148,7 @@ 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);

Check failure on line 151 in src/co2.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Replace `options,·this.model.version,·green` with `⏎······options,⏎······this.model.version,⏎······green⏎····`

Check failure on line 151 in src/co2.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `options,·this.model.version,·green` with `⏎······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 +193,7 @@ 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);

Check failure on line 196 in src/co2.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Replace `options,·this.model.version,·green` with `⏎········options,⏎········this.model.version,⏎········green⏎······`

Check failure on line 196 in src/co2.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `options,·this.model.version,·green` with `⏎········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 @@ -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) {
Expand Down Expand Up @@ -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;
}

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

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

0 comments on commit 8692e57

Please sign in to comment.