Skip to content

Commit 432bc0e

Browse files
authored
Include all gridIntensity values in adjustments when only some are overridden
2 parents 431492d + e58125e commit 432bc0e

File tree

1 file changed

+31
-74
lines changed

1 file changed

+31
-74
lines changed

src/helpers/index.js

Lines changed: 31 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -33,96 +33,53 @@ function parseOptions(options = {}, version = 3, green = false) {
3333

3434
const adjustments = {};
3535

36-
if (options?.gridIntensity) {
37-
adjustments.gridIntensity = {};
38-
const { device, dataCenter, network } = options.gridIntensity;
39-
if (device || device === 0) {
40-
if (typeof device === "object") {
41-
if (!averageIntensity.data[device.country?.toUpperCase()]) {
42-
console.warn(
43-
`"${device.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \nFalling back to global average grid intensity.`
44-
);
45-
adjustments.gridIntensity["device"] = {
46-
value: globalGridIntensity,
47-
};
48-
}
49-
adjustments.gridIntensity["device"] = {
50-
country: device.country,
51-
value: parseFloat(
52-
averageIntensity.data[device.country?.toUpperCase()]
53-
),
54-
};
55-
} else if (typeof device === "number") {
56-
adjustments.gridIntensity["device"] = {
57-
value: device,
58-
};
59-
} else {
60-
adjustments.gridIntensity["device"] = {
61-
value: globalGridIntensity,
62-
};
63-
console.warn(
64-
`The device grid intensity must be a number or an object. You passed in a ${typeof device}. \nFalling back to global average grid intensity.`
65-
);
66-
}
67-
}
68-
if (dataCenter || dataCenter === 0) {
69-
if (typeof dataCenter === "object") {
70-
if (!averageIntensity.data[dataCenter.country?.toUpperCase()]) {
71-
console.warn(
72-
`"${dataCenter.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \nFalling back to global average grid intensity.`
73-
);
74-
adjustments.gridIntensity["dataCenter"] = {
75-
value: SWDM3_GLOBAL_GRID_INTENSITY,
76-
};
77-
}
78-
adjustments.gridIntensity["dataCenter"] = {
79-
country: dataCenter.country,
80-
value: parseFloat(
81-
averageIntensity.data[dataCenter.country?.toUpperCase()]
82-
),
83-
};
84-
} else if (typeof dataCenter === "number") {
85-
adjustments.gridIntensity["dataCenter"] = {
86-
value: dataCenter,
87-
};
88-
} else {
89-
adjustments.gridIntensity["dataCenter"] = {
90-
value: globalGridIntensity,
91-
};
92-
console.warn(
93-
`The data center grid intensity must be a number or an object. You passed in a ${typeof dataCenter}. \nFalling back to global average grid intensity.`
94-
);
95-
}
96-
}
97-
if (network || network === 0) {
98-
if (typeof network === "object") {
99-
if (!averageIntensity.data[network.country?.toUpperCase()]) {
36+
/**
37+
*
38+
* @param {string} segment The name of the segment ("device"|"dataCenter"|"network")
39+
* @param {number|object} segmentIntensity The segment intensity
40+
*/
41+
function setIntensity(segment, segmentIntensity) {
42+
if (segmentIntensity || segmentIntensity === 0) {
43+
if (typeof segmentIntensity === "object") {
44+
if (!averageIntensity.data[segmentIntensity.country?.toUpperCase()]) {
10045
console.warn(
101-
`"${network.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. Falling back to global average grid intensity. \nFalling back to global average grid intensity.`
46+
`"${segmentIntensity.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \nFalling back to global average grid intensity.`
10247
);
103-
adjustments.gridIntensity["network"] = {
48+
adjustments.gridIntensity[segment] = {
10449
value: globalGridIntensity,
10550
};
10651
}
107-
adjustments.gridIntensity["network"] = {
108-
country: network.country,
52+
adjustments.gridIntensity[segment] = {
53+
country: segmentIntensity.country,
10954
value: parseFloat(
110-
averageIntensity.data[network.country?.toUpperCase()]
55+
averageIntensity.data[segmentIntensity.country?.toUpperCase()]
11156
),
11257
};
113-
} else if (typeof network === "number") {
114-
adjustments.gridIntensity["network"] = {
115-
value: network,
58+
} else if (typeof segmentIntensity === "number") {
59+
adjustments.gridIntensity[segment] = {
60+
value: segmentIntensity,
11661
};
11762
} else {
118-
adjustments.gridIntensity["network"] = {
63+
adjustments.gridIntensity[segment] = {
11964
value: globalGridIntensity,
12065
};
12166
console.warn(
122-
`The network grid intensity must be a number or an object. You passed in a ${typeof network}. \nFalling back to global average grid intensity.`
67+
`The ${segment} grid intensity must be a number or an object. You passed in a ${typeof segmentIntensity}. \nFalling back to global average grid intensity.`
12368
);
12469
}
70+
} else {
71+
adjustments.gridIntensity[segment] = {
72+
value: globalGridIntensity,
73+
};
12574
}
75+
}
76+
77+
if (options?.gridIntensity) {
78+
adjustments.gridIntensity = {};
79+
const { device, dataCenter, network } = options.gridIntensity;
80+
setIntensity("device", device);
81+
setIntensity("dataCenter", dataCenter);
82+
setIntensity("network", network);
12683
} else {
12784
adjustments.gridIntensity = {
12885
device: { value: globalGridIntensity },

0 commit comments

Comments
 (0)