Skip to content

Commit a045b14

Browse files
committed
Add support for values equal to 0 in perByteTrace and perVisitTrace
1 parent b56091a commit a045b14

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

src/co2.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ class CO2 {
122122
description:
123123
"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",
124124
network:
125-
adjustments?.gridIntensity?.network?.value || GLOBAL_GRID_INTENSITY,
125+
adjustments?.gridIntensity?.network?.value ?? GLOBAL_GRID_INTENSITY,
126126
dataCenter: green
127127
? RENEWABLES_GRID_INTENSITY
128-
: adjustments?.gridIntensity?.dataCenter?.value ||
128+
: adjustments?.gridIntensity?.dataCenter?.value ??
129129
GLOBAL_GRID_INTENSITY,
130130
production: GLOBAL_GRID_INTENSITY,
131131
device:
132-
adjustments?.gridIntensity?.device?.value || GLOBAL_GRID_INTENSITY,
132+
adjustments?.gridIntensity?.device?.value ?? GLOBAL_GRID_INTENSITY,
133133
},
134134
},
135135
};
@@ -164,20 +164,20 @@ class CO2 {
164164
description:
165165
"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",
166166
network:
167-
adjustments?.gridIntensity?.network?.value ||
167+
adjustments?.gridIntensity?.network?.value ??
168168
GLOBAL_GRID_INTENSITY,
169169
dataCenter: green
170170
? RENEWABLES_GRID_INTENSITY
171-
: adjustments?.gridIntensity?.dataCenter?.value ||
171+
: adjustments?.gridIntensity?.dataCenter?.value ??
172172
GLOBAL_GRID_INTENSITY,
173173
production: GLOBAL_GRID_INTENSITY,
174174
device:
175-
adjustments?.gridIntensity?.device?.value ||
175+
adjustments?.gridIntensity?.device?.value ??
176176
GLOBAL_GRID_INTENSITY,
177177
},
178-
dataReloadRatio: adjustments?.dataReloadRatio || 0.02,
179-
firstVisitPercentage: adjustments?.firstVisitPercentage || 0.75,
180-
returnVisitPercentage: adjustments?.returnVisitPercentage || 0.25,
178+
dataReloadRatio: adjustments?.dataReloadRatio ?? 0.02,
179+
firstVisitPercentage: adjustments?.firstVisitPercentage ?? 0.75,
180+
returnVisitPercentage: adjustments?.returnVisitPercentage ?? 0.25,
181181
},
182182
};
183183
} else {

src/co2.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,4 +855,46 @@ describe("co2", () => {
855855
).toBeLessThan(MILLION_PERVISIT_GREY);
856856
});
857857
});
858+
859+
describe("Using values equal to 0", () => {
860+
const co2 = new CO2();
861+
862+
it("expects perByteTrace to support values equal to 0", () => {
863+
const perByteTraceResult = co2.perByteTrace(1000000, false, {
864+
gridIntensity: {
865+
dataCenter: 0,
866+
network: 0,
867+
device: 0,
868+
},
869+
});
870+
const { dataCenter, network, device } =
871+
perByteTraceResult.variables.gridIntensity;
872+
expect(dataCenter).toBe(0);
873+
expect(network).toBe(0);
874+
expect(device).toBe(0);
875+
});
876+
877+
it("expects perVisitTrace to support values equal to 0", () => {
878+
const perVisitTraceResult = co2.perVisitTrace(1000000, false, {
879+
dataReloadRatio: 0,
880+
returnVisitPercentage: 0,
881+
firstVisitPercentage: 0,
882+
gridIntensity: {
883+
dataCenter: 0,
884+
network: 0,
885+
device: 0,
886+
},
887+
});
888+
const { dataReloadRatio, firstVisitPercentage, returnVisitPercentage } =
889+
perVisitTraceResult.variables;
890+
const { dataCenter, network, device } =
891+
perVisitTraceResult.variables.gridIntensity;
892+
expect(dataReloadRatio).toBe(0);
893+
expect(firstVisitPercentage).toBe(0);
894+
expect(returnVisitPercentage).toBe(0);
895+
expect(dataCenter).toBe(0);
896+
expect(network).toBe(0);
897+
expect(device).toBe(0);
898+
});
899+
});
858900
});

src/helpers/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function parseOptions(options) {
1818
if (options?.gridIntensity) {
1919
adjustments.gridIntensity = {};
2020
const { device, dataCenter, network } = options.gridIntensity;
21-
if (device) {
21+
if (device || device === 0) {
2222
if (typeof device === "object") {
2323
if (!averageIntensity.data[device.country?.toUpperCase()]) {
2424
console.warn(
@@ -47,7 +47,7 @@ function parseOptions(options) {
4747
);
4848
}
4949
}
50-
if (dataCenter) {
50+
if (dataCenter || dataCenter === 0) {
5151
if (typeof dataCenter === "object") {
5252
if (!averageIntensity.data[dataCenter.country?.toUpperCase()]) {
5353
console.warn(
@@ -76,7 +76,7 @@ function parseOptions(options) {
7676
);
7777
}
7878
}
79-
if (network) {
79+
if (network || network === 0) {
8080
if (typeof network === "object") {
8181
if (!averageIntensity.data[network.country?.toUpperCase()]) {
8282
console.warn(
@@ -107,7 +107,7 @@ function parseOptions(options) {
107107
}
108108
}
109109

110-
if (options?.dataReloadRatio) {
110+
if (options?.dataReloadRatio || options.dataReloadRatio === 0) {
111111
if (typeof options.dataReloadRatio === "number") {
112112
if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) {
113113
adjustments.dataReloadRatio = options.dataReloadRatio;
@@ -127,7 +127,7 @@ function parseOptions(options) {
127127
}
128128
}
129129

130-
if (options?.firstVisitPercentage) {
130+
if (options?.firstVisitPercentage || options.firstVisitPercentage === 0) {
131131
if (typeof options.firstVisitPercentage === "number") {
132132
if (
133133
options.firstVisitPercentage >= 0 &&
@@ -148,7 +148,7 @@ function parseOptions(options) {
148148
}
149149
}
150150

151-
if (options?.returnVisitPercentage) {
151+
if (options?.returnVisitPercentage || options.returnVisitPercentage === 0) {
152152
if (typeof options.returnVisitPercentage === "number") {
153153
if (
154154
options.returnVisitPercentage >= 0 &&

0 commit comments

Comments
 (0)