Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate options parsing for byteTrace and visitTrace functions #226

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Loading