Skip to content

Commit 6f4b08b

Browse files
committed
pull out utility types, some naming cleanup
1 parent 8e66245 commit 6f4b08b

File tree

5 files changed

+59
-30
lines changed

5 files changed

+59
-30
lines changed

package-lock.json

+28-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"np": "^8.0.4",
6666
"pagexray": "^4.4.2",
6767
"prettier": "^2.6.2",
68+
"type-fest": "^4.14.0",
6869
"typescript": "^5.4.2"
6970
},
7071
"jest": {

src/co2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CO2 {
5353
*
5454
* @param {number} bytes
5555
* @param {boolean} green
56-
* @return {number | AdjustedCO2ByComponentWithTotal} the amount of CO2 in grammes
56+
* @return {number | CO2ByComponentAndVisitWithTotal} the amount of CO2 in grammes
5757
*/
5858
perVisit(bytes, green = false) {
5959
if ("perVisit" in this.model) {

src/sustainable-web-design.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ class SustainableWebDesign {
4848
* Accept an object keys by the different system components, and
4949
* return an object with the co2 figures key by the each component
5050
*
51-
* @template {AdjustedEnergyByComponent | EnergyByComponent} EnergyObject
52-
* @template [CO2Object=EnergyObject extends AdjustedEnergyByComponent ? AdjustedCO2ByComponent : CO2ByComponent]
51+
* @template {Record<string, number>} EnergyObject
5352
* @param {EnergyObject} energyByComponent - energy grouped by the four system components
5453
* // TODO (simon) check on this type for carbonIntensity
5554
* @param {(number | boolean)=} carbonIntensity - carbon intensity to apply to the datacentre values
5655
* @param {ModelAdjustments=} options - carbon intensity to apply to the datacentre values
57-
* @return {CO2Object} the total number in grams of CO2 equivalent emissions
56+
* @return {MapEnergyToCO2<EnergyObject>} the total number in grams of CO2 equivalent emissions
5857
*/
5958
co2byComponent(
6059
energyByComponent,
@@ -108,7 +107,7 @@ class SustainableWebDesign {
108107
}
109108
}
110109

111-
return /** @type {CO2Object} */ (returnCO2ByComponent);
110+
return /** @type {MapEnergyToCO2<EnergyObject>} */ (returnCO2ByComponent);
112111
}
113112

114113
/**
@@ -169,7 +168,7 @@ class SustainableWebDesign {
169168
* @param {boolean} carbonIntensity - a boolean indicating whether the data center is green or not
170169
* @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component
171170
* @param {ModelAdjustments=} options - an object containing the grid intensity and first/return visitor values
172-
* @return {number | AdjustedCO2ByComponentWithTotal} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component
171+
* @return {number | CO2ByComponentAndVisitWithTotal} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component
173172
*/
174173
perVisit(
175174
bytes,
@@ -239,7 +238,7 @@ class SustainableWebDesign {
239238
* @param {number=} returnView - what percentage of visits are loading this page for subsequent times
240239
* @param {number=} dataReloadRatio - what percentage of a page is reloaded on each subsequent page view
241240
*
242-
* @return {AdjustedEnergyByComponent} Object containing the energy in kilowatt hours, keyed by system component
241+
* @return {EnergyByComponentAndVisit} Object containing the energy in kilowatt hours, keyed by system component
243242
*/
244243
energyPerVisitByComponent(
245244
bytes,
@@ -277,7 +276,7 @@ class SustainableWebDesign {
277276
value * returnView * dataReloadRatio;
278277
}
279278

280-
return /** @type {AdjustedEnergyByComponent} */ (
279+
return /** @type {EnergyByComponentAndVisit} */ (
281280
cacheAdjustedSegmentEnergy
282281
);
283282
}

src/types.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
* @property {TraceResultVariables} variables - The variables used to calculate the CO2 estimate
5656
*
5757
* @typedef CO2EstimateTraceResultPerVisit
58-
* @property {number | AdjustedCO2ByComponentWithTotal} co2 - The CO2 estimate in grams/kilowatt-hour
58+
* @property {number | CO2ByComponentAndVisitWithTotal} co2 - The CO2 estimate in grams/kilowatt-hour
5959
* @property {boolean} green - Whether the domain is green or not
6060
* @property {TraceResultVariables} variables - The variables used to calculate the CO2 estimate
6161
*
@@ -81,25 +81,17 @@
8181
* @property {number} productionEnergy
8282
* @property {number} dataCenterEnergy
8383
*
84-
* @typedef {Object} AdjustedEnergyByComponent
85-
* @type {{
86-
* [K in keyof EnergyByComponent as `${K} - first`]: EnergyByComponent[K]
87-
* } & {
88-
* [K in keyof EnergyByComponent as `${K} - subsequest`]: EnergyByComponent[K]
89-
* }}
84+
* @typedef EnergyByComponentAndVisit
85+
* @type {SegmentedByVisit<EnergyByComponent>}
9086
*
9187
* @typedef CO2ByComponent
9288
* @property {number} consumerDeviceCO2
9389
* @property {number} networkCO2
9490
* @property {number} productionCO2
9591
* @property {number} dataCenterCO2
9692
*
97-
* @typedef {Object} AdjustedCO2ByComponent
98-
* @type {{
99-
* [K in keyof CO2ByComponent as `${K} - first`]: CO2ByComponent[K]
100-
* } & {
101-
* [K in keyof CO2ByComponent as `${K} - subsequest`]: CO2ByComponent[K]
102-
* }}
93+
* @typedef CO2ByComponentAndVisit
94+
* @type {SegmentedByVisit<CO2ByComponent>}
10395
*
10496
* @typedef CO2ByComponentWithTotal
10597
* @property {number} consumerDeviceCO2
@@ -108,8 +100,8 @@
108100
* @property {number} dataCenterCO2
109101
* @property {number} total
110102
*
111-
* @typedef {Object} AdjustedCO2ByComponentWithTotal
112-
* @type {AdjustedCO2ByComponent & { total: number }}
103+
* @typedef CO2ByComponentAndVisitWithTotal
104+
* @type {CO2ByComponentAndVisit & { total: number }}
113105
*
114106
* @typedef PageXRayDomain
115107
* @property {number} transferSize
@@ -146,3 +138,19 @@
146138
* @typedef MultiDomainCheckResponse
147139
* @type {Record<string, PerDomainCheckResponse>}
148140
*/
141+
142+
/**
143+
* @template {Record<string, unknown>} Object
144+
* @typedef {{
145+
* [K in Exclude<keyof Object, symbol> as `${K} - first`]: Object[K]
146+
* } & {
147+
* [K in Exclude<keyof Object, symbol> as `${K} - subsequest`]: Object[K]
148+
* }} SegmentedByVisit
149+
*/
150+
151+
/**
152+
* @template {Record<string, unknown>} Object
153+
* @typedef {{
154+
* [K in Extract<keyof Object, string> as import('type-fest').Replace<K, 'Energy', 'CO2'>]: Object[K]
155+
* }} MapEnergyToCO2
156+
*/

0 commit comments

Comments
 (0)