@tgwf/co2 0.12.0 → 0.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/output/average-intensities-2021.js +3 -90
- package/data/output/average-intensities-2021.json +1 -776
- package/dist/cjs/1byte.js.map +1 -1
- package/dist/cjs/co2.js +44 -13
- package/dist/cjs/co2.js.map +2 -2
- package/dist/cjs/constants/index.js +5 -4
- package/dist/cjs/constants/index.js.map +3 -3
- package/dist/cjs/data/average-intensities-2021.min.js +1 -1
- package/dist/cjs/data/average-intensities-2021.min.js.map +2 -2
- package/dist/cjs/helpers/index.js +57 -27
- package/dist/cjs/helpers/index.js.map +1 -1
- package/dist/cjs/hosting-api.js +7 -2
- package/dist/cjs/hosting-api.js.map +2 -2
- package/dist/cjs/hosting-json.node.js +4 -1
- package/dist/cjs/hosting-json.node.js.map +2 -2
- package/dist/cjs/hosting-node.js +19 -4
- package/dist/cjs/hosting-node.js.map +2 -2
- package/dist/cjs/hosting.js +4 -1
- package/dist/cjs/hosting.js.map +2 -2
- package/dist/cjs/index-node.js +4 -1
- package/dist/cjs/index-node.js.map +2 -2
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/sustainable-web-design.js +32 -9
- package/dist/cjs/sustainable-web-design.js.map +2 -2
- package/dist/esm/co2.js +40 -13
- package/dist/esm/constants/index.js +1 -3
- package/dist/esm/data/average-intensities-2021.min.js +1 -1
- package/dist/esm/helpers/index.js +57 -27
- package/dist/esm/hosting-api.js +3 -2
- package/dist/esm/hosting.js +0 -1
- package/dist/esm/sustainable-web-design.js +28 -9
- package/dist/iife/index.js +13 -13
- package/dist/iife/index.js.map +3 -3
- package/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/constants/index.js +0 -2
- package/src/data/average-intensities-2021.min.js +1 -1
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hosting-json.node.js"],
|
|
4
4
|
"sourcesContent": ["\"use strict\";\n\nimport fs from \"fs\";\nimport zlib from \"zlib\";\nimport { promisify } from \"util\";\n\nconst readFile = promisify(fs.readFile);\nconst gunzip = promisify(zlib.gunzip);\n\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:hostingCache\");\n\nasync function streamToString(stream) {\n return new Promise((resolve, reject) => {\n const chunks = [];\n stream.on(\"error\", reject);\n stream.on(\"data\", (chunk) => chunks.push(chunk));\n stream.on(\"end\", () => resolve(Buffer.concat(chunks)));\n });\n}\n\nasync function getGzippedFileAsJson(jsonPath) {\n const readStream = fs.createReadStream(jsonPath);\n const text = await streamToString(readStream);\n const unzipped = await gunzip(text);\n return unzipped.toString();\n}\n\nasync function loadJSON(jsonPath) {\n const jsonBuffer = jsonPath.endsWith(\".gz\")\n ? await getGzippedFileAsJson(jsonPath)\n : await readFile(jsonPath);\n return JSON.parse(jsonBuffer);\n}\n\nasync function check(domain, db) {\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkInJSON(domain, db);\n } else {\n return checkDomainsInJSON(domain, db);\n }\n}\n\nfunction checkInJSON(domain, db) {\n if (db.indexOf(domain) > -1) {\n return true;\n }\n return false;\n}\n\nfunction greenDomainsFromResults(greenResults) {\n const entries = Object.entries(greenResults);\n const greenEntries = entries.filter(([key, val]) => val.green);\n\n return greenEntries.map(([key, val]) => val.url);\n}\n\nfunction checkDomainsInJSON(domains, db) {\n let greenDomains = [];\n\n for (let domain of domains) {\n if (db.indexOf(domain) > -1) {\n greenDomains.push(domain);\n }\n }\n return greenDomains;\n}\n\nmodule.exports = {\n check,\n loadJSON,\n greenDomainsFromResults,\n};\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAEA,gBAAe;AACf,kBAAiB;AACjB,kBAA0B;AAK1B,mBAAyB;AAHzB,MAAM,eAAW,uBAAU,UAAAA,QAAG,QAAQ;AACtC,MAA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;AAEpB,WAAS,UAAU,SAAS;AAC1B,QAAI,GAAG,QAAQ,MAAM,IAAI,IAAI;AAC3B,mBAAa,KAAK,MAAM;AAAA,IAC1B;AAAA,EACF;AACA,SAAO;AACT;AAEA,OAAO,UAAU;AAAA,EACf;AAAA,EACA;AAAA,EACA;AACF;",
|
|
6
|
+
"names": ["fs", "zlib", "debugFactory"]
|
|
7
7
|
}
|
package/dist/cjs/hosting-node.js
CHANGED
|
@@ -16,7 +16,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
20
23
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
24
|
var hosting_node_exports = {};
|
|
22
25
|
__export(hosting_node_exports, {
|
|
@@ -31,7 +34,11 @@ async function getBody(url) {
|
|
|
31
34
|
return new Promise(function(resolve, reject) {
|
|
32
35
|
const req = import_https.default.get(url, function(res) {
|
|
33
36
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
34
|
-
log(
|
|
37
|
+
log(
|
|
38
|
+
"Could not get info from the Green Web Foundation API, %s for %s",
|
|
39
|
+
res.statusCode,
|
|
40
|
+
url
|
|
41
|
+
);
|
|
35
42
|
return reject(new Error(`Status Code: ${res.statusCode}`));
|
|
36
43
|
}
|
|
37
44
|
const data = [];
|
|
@@ -54,12 +61,20 @@ function check(domain, db) {
|
|
|
54
61
|
}
|
|
55
62
|
}
|
|
56
63
|
async function checkAgainstAPI(domain) {
|
|
57
|
-
const res = JSON.parse(
|
|
64
|
+
const res = JSON.parse(
|
|
65
|
+
await getBody(`https://api.thegreenwebfoundation.org/greencheck/${domain}`)
|
|
66
|
+
);
|
|
58
67
|
return res.green;
|
|
59
68
|
}
|
|
60
69
|
async function checkDomainsAgainstAPI(domains) {
|
|
61
70
|
try {
|
|
62
|
-
const allGreenCheckResults = JSON.parse(
|
|
71
|
+
const allGreenCheckResults = JSON.parse(
|
|
72
|
+
await getBody(
|
|
73
|
+
`https://api.thegreenwebfoundation.org/v2/greencheckmulti/${JSON.stringify(
|
|
74
|
+
domains
|
|
75
|
+
)}`
|
|
76
|
+
)
|
|
77
|
+
);
|
|
63
78
|
return import_hosting_json_node.default.greenDomainsFromResults(allGreenCheckResults);
|
|
64
79
|
} catch (e) {
|
|
65
80
|
return [];
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hosting-node.js"],
|
|
4
4
|
"sourcesContent": ["/*\n\nWe have a separate node-specific hosting.js file for node.\nThis uses the node-specific APIs for making http requests,\nand doing lookups against local JSON and sqlite databases.\nThis is used in the CommonJS build of co2.js\n\nThis lets us keep the total library small, and dependencies minimal.\n*/\n\nimport https from \"https\";\n\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:hosting-node\");\n\nimport hostingJSON from \"./hosting-json.node.js\";\n\n/**\n * Accept a url and perform an http request, returning the body\n * for parsing as JSON.\n *\n * @param {string} url\n * @return {string}\n */\nasync function getBody(url) {\n return new Promise(function (resolve, reject) {\n // Do async job\n const req = https.get(url, function (res) {\n if (res.statusCode < 200 || res.statusCode >= 300) {\n log(\n \"Could not get info from the Green Web Foundation API, %s for %s\",\n res.statusCode,\n url\n );\n return reject(new Error(`Status Code: ${res.statusCode}`));\n }\n const data = [];\n\n res.on(\"data\", (chunk) => {\n data.push(chunk);\n });\n\n res.on(\"end\", () => resolve(Buffer.concat(data).toString()));\n });\n req.end();\n });\n}\n\nfunction check(domain, db) {\n if (db) {\n return hostingJSON.check(domain, db);\n }\n\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkAgainstAPI(domain);\n } else {\n return checkDomainsAgainstAPI(domain);\n }\n}\n\nasync function checkAgainstAPI(domain) {\n const res = JSON.parse(\n await getBody(`https://api.thegreenwebfoundation.org/greencheck/${domain}`)\n );\n return res.green;\n}\n\nasync function checkDomainsAgainstAPI(domains) {\n try {\n const allGreenCheckResults = JSON.parse(\n await getBody(\n `https://api.thegreenwebfoundation.org/v2/greencheckmulti/${JSON.stringify(\n domains\n )}`\n )\n );\n return hostingJSON.greenDomainsFromResults(allGreenCheckResults);\n } catch (e) {\n return [];\n }\n}\n\nasync function checkPage(pageXray, db) {\n const domains = Object.keys(pageXray.domains);\n return check(domains, db);\n}\n\nexport default {\n check,\n checkPage,\n greendomains: hostingJSON.greenDomainsFromResults,\n loadJSON: hostingJSON.loadJSON,\n};\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,mBAAkB;AAElB,mBAAyB;AAGzB,+BAAwB;AAFxB,MAAM,UAAM,aAAAA,SAAa,mBAAmB;AAW5C,eAAe,QAAQ,KAAK;AAC1B,SAAO,IAAI,QAAQ,SAAU,SAAS,QAAQ;AAE5C,UAAM,MAAM,aAAAC,QAAM,IAAI,KAAK,SAAU,KAAK;AACxC,UAAI,IAAI,aAAa,OAAO,IAAI,cAAc,KAAK;AACjD;AAAA,UACE;AAAA,UACA,IAAI;AAAA,UACJ;AAAA,QACF;AACA,eAAO,OAAO,IAAI,MAAM,gBAAgB,IAAI,YAAY,CAAC;AAAA,MAC3D;AACA,YAAM,OAAO,CAAC;AAEd,UAAI,GAAG,QAAQ,CAAC,UAAU;AACxB,aAAK,KAAK,KAAK;AAAA,MACjB,CAAC;AAED,UAAI,GAAG,OAAO,MAAM,QAAQ,OAAO,OAAO,IAAI,EAAE,SAAS,CAAC,CAAC;AAAA,IAC7D,CAAC;AACD,QAAI,IAAI;AAAA,EACV,CAAC;AACH;AAEA,SAAS,MAAM,QAAQ,IAAI;AACzB,MAAI,IAAI;AACN,WAAO,yBAAAC,QAAY,MAAM,QAAQ,EAAE;AAAA,EACrC;AAGA,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,gBAAgB,MAAM;AAAA,EAC/B,OAAO;AACL,WAAO,uBAAuB,MAAM;AAAA,EACtC;AACF;AAEA,eAAe,gBAAgB,QAAQ;AACrC,QAAM,MAAM,KAAK;AAAA,IACf,MAAM,QAAQ,oDAAoD,QAAQ;AAAA,EAC5E;AACA,SAAO,IAAI;AACb;AAEA,eAAe,uBAAuB,SAAS;AAC7C,MAAI;AACF,UAAM,uBAAuB,KAAK;AAAA,MAChC,MAAM;AAAA,QACJ,4DAA4D,KAAK;AAAA,UAC/D;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO,yBAAAA,QAAY,wBAAwB,oBAAoB;AAAA,EACjE,SAAS,GAAP;AACA,WAAO,CAAC;AAAA,EACV;AACF;AAEA,eAAe,UAAU,UAAU,IAAI;AACrC,QAAM,UAAU,OAAO,KAAK,SAAS,OAAO;AAC5C,SAAO,MAAM,SAAS,EAAE;AAC1B;AAEA,IAAO,uBAAQ;AAAA,EACb;AAAA,EACA;AAAA,EACA,cAAc,yBAAAA,QAAY;AAAA,EAC1B,UAAU,yBAAAA,QAAY;AACxB;",
|
|
6
|
+
"names": ["debugFactory", "https", "hostingJSON"]
|
|
7
7
|
}
|
package/dist/cjs/hosting.js
CHANGED
|
@@ -17,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
21
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
25
|
var hosting_exports = {};
|
|
23
26
|
__export(hosting_exports, {
|
package/dist/cjs/hosting.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hosting.js"],
|
|
4
4
|
"sourcesContent": ["\"use strict\";\n\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:hosting\");\n\nimport hostingAPI from \"./hosting-api.js\";\n\nfunction check(domain, db) {\n return hostingAPI.check(domain);\n}\n\nexport default {\n check,\n};\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAAyB;AAGzB,yBAAuB;AAFvB,MAAM,UAAM,aAAAA,SAAa,cAAc;AAIvC,SAAS,MAAM,QAAQ,IAAI;AACzB,SAAO,mBAAAC,QAAW,MAAM,MAAM;AAChC;AAEA,IAAO,kBAAQ;AAAA,EACb;AACF;",
|
|
6
|
+
"names": ["debugFactory", "hostingAPI"]
|
|
7
7
|
}
|
package/dist/cjs/index-node.js
CHANGED
|
@@ -16,7 +16,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
20
23
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
24
|
var index_node_exports = {};
|
|
22
25
|
__export(index_node_exports, {
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index-node.js"],
|
|
4
4
|
"sourcesContent": ["import co2 from \"./co2.js\";\nimport hosting from \"./hosting-node.js\";\nimport averageIntensity from \"./data/average-intensities-2021.min.js\";\nimport marginalIntensity from \"./data/marginal-intensities-2021.min.js\";\nexport { co2, hosting, averageIntensity, marginalIntensity };\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,8DAAAA;AAAA,EAAA,sBAAAC;AAAA,EAAA,mCAAAC;AAAA,EAAA,8DAAAC;AAAA;AAAA;AAAA,iBAAgB;AAChB,0BAAoB;AACpB,0CAA6B;AAC7B,2CAA8B;",
|
|
6
|
+
"names": ["averageIntensity", "co2", "hosting", "marginalIntensity"]
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -16,7 +16,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
20
23
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
24
|
var src_exports = {};
|
|
22
25
|
__export(src_exports, {
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.js"],
|
|
4
4
|
"sourcesContent": ["import co2 from \"./co2.js\";\nimport hosting from \"./hosting.js\";\nimport averageIntensity from \"./data/average-intensities-2021.min.js\";\nimport marginalIntensity from \"./data/marginal-intensities-2021.min.js\";\n\nexport { co2, hosting, averageIntensity, marginalIntensity };\nexport default { co2, hosting, averageIntensity, marginalIntensity };\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,8DAAAA;AAAA,EAAA,sBAAAC;AAAA,EAAA;AAAA,gCAAAC;AAAA,EAAA,8DAAAC;AAAA;AAAA;AAAA,iBAAgB;AAChB,qBAAoB;AACpB,0CAA6B;AAC7B,2CAA8B;AAG9B,IAAO,cAAQ,EAAE,gBAAAF,SAAK,wBAAAC,SAAS,sDAAAF,SAAkB,wDAAAG,QAAkB;",
|
|
6
|
+
"names": ["averageIntensity", "co2", "hosting", "marginalIntensity"]
|
|
7
7
|
}
|
|
@@ -17,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
21
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
25
|
var sustainable_web_design_exports = {};
|
|
23
26
|
__export(sustainable_web_design_exports, {
|
|
@@ -80,11 +83,19 @@ class SustainableWebDesign {
|
|
|
80
83
|
perByte(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
|
|
81
84
|
const energyBycomponent = this.energyPerByteByComponent(bytes, options);
|
|
82
85
|
if (typeof carbonIntensity !== "boolean") {
|
|
83
|
-
throw new Error(
|
|
86
|
+
throw new Error(
|
|
87
|
+
`perByte expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`
|
|
88
|
+
);
|
|
84
89
|
}
|
|
85
|
-
const co2ValuesbyComponent = this.co2byComponent(
|
|
90
|
+
const co2ValuesbyComponent = this.co2byComponent(
|
|
91
|
+
energyBycomponent,
|
|
92
|
+
carbonIntensity,
|
|
93
|
+
options
|
|
94
|
+
);
|
|
86
95
|
const co2Values = Object.values(co2ValuesbyComponent);
|
|
87
|
-
const co2ValuesSum = co2Values.reduce(
|
|
96
|
+
const co2ValuesSum = co2Values.reduce(
|
|
97
|
+
(prevValue, currentValue) => prevValue + currentValue
|
|
98
|
+
);
|
|
88
99
|
if (segmentResults) {
|
|
89
100
|
return { ...co2ValuesbyComponent, total: co2ValuesSum };
|
|
90
101
|
}
|
|
@@ -93,11 +104,19 @@ class SustainableWebDesign {
|
|
|
93
104
|
perVisit(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
|
|
94
105
|
const energyBycomponent = this.energyPerVisitByComponent(bytes, options);
|
|
95
106
|
if (typeof carbonIntensity !== "boolean") {
|
|
96
|
-
throw new Error(
|
|
107
|
+
throw new Error(
|
|
108
|
+
`perVisit expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`
|
|
109
|
+
);
|
|
97
110
|
}
|
|
98
|
-
const co2ValuesbyComponent = this.co2byComponent(
|
|
111
|
+
const co2ValuesbyComponent = this.co2byComponent(
|
|
112
|
+
energyBycomponent,
|
|
113
|
+
carbonIntensity,
|
|
114
|
+
options
|
|
115
|
+
);
|
|
99
116
|
const co2Values = Object.values(co2ValuesbyComponent);
|
|
100
|
-
const co2ValuesSum = co2Values.reduce(
|
|
117
|
+
const co2ValuesSum = co2Values.reduce(
|
|
118
|
+
(prevValue, currentValue) => prevValue + currentValue
|
|
119
|
+
);
|
|
101
120
|
if (segmentResults) {
|
|
102
121
|
return { ...co2ValuesbyComponent, total: co2ValuesSum };
|
|
103
122
|
}
|
|
@@ -106,7 +125,9 @@ class SustainableWebDesign {
|
|
|
106
125
|
energyPerByte(bytes) {
|
|
107
126
|
const energyByComponent = this.energyPerByteByComponent(bytes);
|
|
108
127
|
const energyValues = Object.values(energyByComponent);
|
|
109
|
-
return energyValues.reduce(
|
|
128
|
+
return energyValues.reduce(
|
|
129
|
+
(prevValue, currentValue) => prevValue + currentValue
|
|
130
|
+
);
|
|
110
131
|
}
|
|
111
132
|
energyPerVisitByComponent(bytes, options = {}, firstView = import_constants.FIRST_TIME_VIEWING_PERCENTAGE, returnView = import_constants.RETURNING_VISITOR_PERCENTAGE, dataReloadRatio = import_constants.PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD) {
|
|
112
133
|
if (options.dataReloadRatio) {
|
|
@@ -132,7 +153,9 @@ class SustainableWebDesign {
|
|
|
132
153
|
energyPerVisit(bytes) {
|
|
133
154
|
let firstVisits = 0;
|
|
134
155
|
let subsequentVisits = 0;
|
|
135
|
-
const energyBycomponent = Object.entries(
|
|
156
|
+
const energyBycomponent = Object.entries(
|
|
157
|
+
this.energyPerVisitByComponent(bytes)
|
|
158
|
+
);
|
|
136
159
|
for (const [key, val] of energyBycomponent) {
|
|
137
160
|
if (key.indexOf("first") > 0) {
|
|
138
161
|
firstVisits += val;
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/sustainable-web-design.js"],
|
|
4
4
|
"sourcesContent": ["\"use strict\";\n\n/**\n * Sustainable Web Design\n *\n * Updated calculations and figures from\n * https://sustainablewebdesign.org/calculating-digital-emissions/\n *\n */\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:sustainable-web-design\");\n\nimport {\n fileSize,\n KWH_PER_GB,\n END_USER_DEVICE_ENERGY,\n NETWORK_ENERGY,\n DATACENTER_ENERGY,\n PRODUCTION_ENERGY,\n GLOBAL_GRID_INTENSITY,\n RENEWABLES_GRID_INTENSITY,\n FIRST_TIME_VIEWING_PERCENTAGE,\n RETURNING_VISITOR_PERCENTAGE,\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD,\n} from \"./constants/index.js\";\nimport { formatNumber } from \"./helpers/index.js\";\n\nclass SustainableWebDesign {\n constructor(options) {\n this.options = options;\n }\n\n /**\n * Accept a figure for bytes transferred and return an object representing\n * the share of the total enrgy use of the entire system, broken down\n * by each corresponding system component\n *\n * @param {number} bytes - the data transferred in bytes\n * @return {object} Object containing the energy in kilowatt hours, keyed by system component\n */\n energyPerByteByComponent(bytes) {\n const transferedBytesToGb = bytes / fileSize.GIGABYTE;\n const energyUsage = transferedBytesToGb * KWH_PER_GB;\n\n // return the total energy, with breakdown by component\n return {\n consumerDeviceEnergy: energyUsage * END_USER_DEVICE_ENERGY,\n networkEnergy: energyUsage * NETWORK_ENERGY,\n productionEnergy: energyUsage * PRODUCTION_ENERGY,\n dataCenterEnergy: energyUsage * DATACENTER_ENERGY,\n };\n }\n /**\n * Accept an object keys by the different system components, and\n * return an object with the co2 figures key by the each component\n *\n * @param {object} energyByComponent - energy grouped by the four system components\n * @param {number} [carbonIntensity] - carbon intensity to apply to the datacentre values\n * @return {number} the total number in grams of CO2 equivalent emissions\n */\n co2byComponent(\n energyByComponent,\n carbonIntensity = GLOBAL_GRID_INTENSITY,\n options = {}\n ) {\n let deviceCarbonIntensity = GLOBAL_GRID_INTENSITY;\n let networkCarbonIntensity = GLOBAL_GRID_INTENSITY;\n let dataCenterCarbonIntensity = GLOBAL_GRID_INTENSITY;\n\n let globalEmissions = GLOBAL_GRID_INTENSITY;\n\n if (options?.gridIntensity) {\n const { device, network, dataCenter } = options.gridIntensity;\n\n if (device?.value) {\n deviceCarbonIntensity = device.value;\n }\n if (network?.value) {\n networkCarbonIntensity = network.value;\n }\n // If the user has set a carbon intensity value for the datacentre, then that overrides everything and is used\n if (dataCenter?.value) {\n dataCenterCarbonIntensity = dataCenter.value;\n }\n }\n\n // If the user passes in a TRUE value (green web host), then use the renewables intensity value\n if (carbonIntensity === true) {\n dataCenterCarbonIntensity = RENEWABLES_GRID_INTENSITY;\n }\n\n const returnCO2ByComponent = {};\n for (const [key, value] of Object.entries(energyByComponent)) {\n // we update the datacentre, as that's what we have information\n // about.\n if (key.startsWith(\"dataCenterEnergy\")) {\n returnCO2ByComponent[key.replace(\"Energy\", \"CO2\")] =\n value * dataCenterCarbonIntensity;\n } else if (key.startsWith(\"consumerDeviceEnergy\")) {\n returnCO2ByComponent[key.replace(\"Energy\", \"CO2\")] =\n value * deviceCarbonIntensity;\n } else if (key.startsWith(\"networkEnergy\")) {\n returnCO2ByComponent[key.replace(\"Energy\", \"CO2\")] =\n value * networkCarbonIntensity;\n } else {\n // Use the global intensity for the remaining segments\n returnCO2ByComponent[key.replace(\"Energy\", \"CO2\")] =\n value * globalEmissions;\n }\n }\n\n return returnCO2ByComponent;\n }\n\n /**\n * Accept a figure for bytes transferred and return a single figure for CO2\n * emissions. Where information exists about the origin data is being\n * fetched from, a different carbon intensity figure\n * is applied for the datacentre share of the carbon intensity.\n *\n * @param {number} bytes - the data transferred in bytes\n * @param {number} `carbonIntensity` the carbon intensity for datacentre (average figures, not marginal ones)\n * @return {number} the total number in grams of CO2 equivalent emissions\n */\n perByte(\n bytes,\n carbonIntensity = false,\n segmentResults = false,\n options = {}\n ) {\n const energyBycomponent = this.energyPerByteByComponent(bytes, options);\n\n // otherwise when faced with non numeric values throw an error\n if (typeof carbonIntensity !== \"boolean\") {\n throw new Error(\n `perByte expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`\n );\n }\n\n const co2ValuesbyComponent = this.co2byComponent(\n energyBycomponent,\n carbonIntensity,\n options\n );\n\n // pull out our values\u2026\n const co2Values = Object.values(co2ValuesbyComponent);\n const co2ValuesSum = co2Values.reduce(\n (prevValue, currentValue) => prevValue + currentValue\n );\n\n if (segmentResults) {\n return { ...co2ValuesbyComponent, total: co2ValuesSum };\n }\n\n return co2ValuesSum;\n }\n\n /**\n * Accept a figure for bytes transferred and return a single figure for CO2\n * emissions. This method applies caching assumptions from the original Sustainable Web Design model.\n *\n * @param {number} bytes - the data transferred in bytes\n * @param {number} `carbonIntensity` the carbon intensity for datacentre (average figures, not marginal ones)\n * @return {number} the total number in grams of CO2 equivalent emissions\n */\n perVisit(\n bytes,\n carbonIntensity = false,\n segmentResults = false,\n options = {}\n ) {\n const energyBycomponent = this.energyPerVisitByComponent(bytes, options);\n\n if (typeof carbonIntensity !== \"boolean\") {\n // otherwise when faced with non numeric values throw an error\n throw new Error(\n `perVisit expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`\n );\n }\n\n const co2ValuesbyComponent = this.co2byComponent(\n energyBycomponent,\n carbonIntensity,\n options\n );\n\n // pull out our values\u2026\n const co2Values = Object.values(co2ValuesbyComponent);\n const co2ValuesSum = co2Values.reduce(\n (prevValue, currentValue) => prevValue + currentValue\n );\n\n if (segmentResults) {\n return { ...co2ValuesbyComponent, total: co2ValuesSum };\n }\n\n // so we can return their sum\n return co2ValuesSum;\n }\n\n /**\n * Accept a figure for bytes transferred and return the number of kilowatt hours used\n * by the total system for this data transfer\n *\n * @param {number} bytes\n * @return {number} the number of kilowatt hours used\n */\n energyPerByte(bytes) {\n const energyByComponent = this.energyPerByteByComponent(bytes);\n\n // pull out our values\u2026\n const energyValues = Object.values(energyByComponent);\n\n // so we can return their sum\n return energyValues.reduce(\n (prevValue, currentValue) => prevValue + currentValue\n );\n }\n\n /**\n * Accept a figure for bytes transferred, and return an object containing figures\n * per system component, with the caching assumptions applied. This tries to account\n * for webpages being loaded from a cache by browsers, so if you had a thousand page views,\n * and tried to work out the energy per visit, the numbers would reflect the reduced amounts\n * of transfer.\n *\n * @param {number} bytes - the data transferred in bytes for loading a webpage\n * @param {number} firstView - what percentage of visits are loading this page for the first time\n * @param {number} returnView - what percentage of visits are loading this page for subsequent times\n * @param {number} dataReloadRatio - what percentage of a page is reloaded on each subsequent page view\n *\n * @return {object} Object containing the energy in kilowatt hours, keyed by system component\n */\n energyPerVisitByComponent(\n bytes,\n options = {},\n firstView = FIRST_TIME_VIEWING_PERCENTAGE,\n returnView = RETURNING_VISITOR_PERCENTAGE,\n dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD\n ) {\n if (options.dataReloadRatio) {\n dataReloadRatio = options.dataReloadRatio;\n }\n\n if (options.firstVisitPercentage) {\n firstView = options.firstVisitPercentage;\n }\n\n if (options.returnVisitPercentage) {\n returnView = options.returnVisitPercentage;\n }\n\n const energyBycomponent = this.energyPerByteByComponent(bytes);\n const cacheAdjustedSegmentEnergy = {};\n\n log({ energyBycomponent });\n const energyValues = Object.values(energyBycomponent);\n\n // for this, we want\n for (const [key, value] of Object.entries(energyBycomponent)) {\n // represent the first load\n cacheAdjustedSegmentEnergy[`${key} - first`] = value * firstView;\n\n // then represent the subsequent load\n cacheAdjustedSegmentEnergy[`${key} - subsequent`] =\n value * returnView * dataReloadRatio;\n }\n log({ cacheAdjustedSegmentEnergy });\n\n return cacheAdjustedSegmentEnergy;\n }\n\n /**\n * Accept a figure for bytes, and return the total figure for energy per visit\n * using the default caching assumptions for loading a single website\n *\n * @param {number} bytes\n * @return {number} the total energy use for the visit, after applying the caching assumptions\n */\n energyPerVisit(bytes) {\n // fetch the values using the default caching assumptions\n // const energyValues = Object.values(this.energyPerVisitByComponent(bytes));\n\n let firstVisits = 0;\n let subsequentVisits = 0;\n\n const energyBycomponent = Object.entries(\n this.energyPerVisitByComponent(bytes)\n );\n\n for (const [key, val] of energyBycomponent) {\n if (key.indexOf(\"first\") > 0) {\n firstVisits += val;\n }\n }\n\n for (const [key, val] of energyBycomponent) {\n if (key.indexOf(\"subsequent\") > 0) {\n subsequentVisits += val;\n }\n }\n\n return firstVisits + subsequentVisits;\n }\n\n emissionsPerVisitInGrams(\n energyPerVisit,\n carbonintensity = GLOBAL_GRID_INTENSITY\n ) {\n return formatNumber(energyPerVisit * carbonintensity);\n }\n\n annualEnergyInKwh(energyPerVisit, monthlyVisitors = 1000) {\n return energyPerVisit * monthlyVisitors * 12;\n }\n\n annualEmissionsInGrams(co2grams, monthlyVisitors = 1000) {\n return co2grams * monthlyVisitors * 12;\n }\n\n annualSegmentEnergy(annualEnergy) {\n return {\n consumerDeviceEnergy: formatNumber(annualEnergy * END_USER_DEVICE_ENERGY),\n networkEnergy: formatNumber(annualEnergy * NETWORK_ENERGY),\n dataCenterEnergy: formatNumber(annualEnergy * DATACENTER_ENERGY),\n productionEnergy: formatNumber(annualEnergy * PRODUCTION_ENERGY),\n };\n }\n}\n\nexport { SustainableWebDesign };\nexport default SustainableWebDesign;\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,mBAAyB;AAGzB,uBAYO;AACP,qBAA6B;AAf7B,MAAM,UAAM,aAAAA,SAAa,6BAA6B;AAiBtD,MAAM,qBAAqB;AAAA,EACzB,YAAY,SAAS;AACnB,SAAK,UAAU;AAAA,EACjB;AAAA,EAUA,yBAAyB,OAAO;AAC9B,UAAM,sBAAsB,QAAQ,0BAAS;AAC7C,UAAM,cAAc,sBAAsB;AAG1C,WAAO;AAAA,MACL,sBAAsB,cAAc;AAAA,MACpC,eAAe,cAAc;AAAA,MAC7B,kBAAkB,cAAc;AAAA,MAChC,kBAAkB,cAAc;AAAA,IAClC;AAAA,EACF;AAAA,EASA,eACE,mBACA,kBAAkB,wCAClB,UAAU,CAAC,GACX;AACA,QAAI,wBAAwB;AAC5B,QAAI,yBAAyB;AAC7B,QAAI,4BAA4B;AAEhC,QAAI,kBAAkB;AAEtB,QAAI,mCAAS,eAAe;AAC1B,YAAM,EAAE,QAAQ,SAAS,WAAW,IAAI,QAAQ;AAEhD,UAAI,iCAAQ,OAAO;AACjB,gCAAwB,OAA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AElC,WAAO;AAAA,EACT;AAAA,EASA,eAAe,OAAO;AAIpB,QAAI,cAAc;AAClB,QAAI,mBAAmB;AAEvB,UAAM,oBAAoB,OAAO;AAAA,MAC/B,KAAK,0BAA0B,KAAK;AAAA,IACtC;AAEA,eAAW,CAAC,KAAK,GAAG,KAAK,mBAAmB;AAC1C,UAAI,IAAI,QAAQ,OAAO,IAAI,GAAG;AAC5B,uBAAe;AAAA,MACjB;AAAA,IACF;AAEA,eAAW,CAAC,KAAK,GAAG,KAAK,mBAAmB;AAC1C,UAAI,IAAI,QAAQ,YAAY,IAAI,GAAG;AACjC,4BAAoB;AAAA,MACtB;AAAA,IACF;AAEA,WAAO,cAAc;AAAA,EACvB;AAAA,EAEA,yBACE,gBACA,kBAAkB,wCAClB;AACA,eAAO,6BAAa,iBAAiB,eAAe;AAAA,EACtD;AAAA,EAEA,kBAAkB,gBAAgB,kBAAkB,KAAM;AACxD,WAAO,iBAAiB,kBAAkB;AAAA,EAC5C;AAAA,EAEA,uBAAuB,UAAU,kBAAkB,KAAM;AACvD,WAAO,WAAW,kBAAkB;AAAA,EACtC;AAAA,EAEA,oBAAoB,cAAc;AAChC,WAAO;AAAA,MACL,0BAAsB,6BAAa,eAAe,uCAAsB;AAAA,MACxE,mBAAe,6BAAa,eAAe,+BAAc;AAAA,MACzD,sBAAkB,6BAAa,eAAe,kCAAiB;AAAA,MAC/D,sBAAkB,6BAAa,eAAe,kCAAiB;AAAA,IACjE;AAAA,EACF;AACF;AAGA,IAAO,iCAAQ;",
|
|
6
|
+
"names": ["debugFactory"]
|
|
7
7
|
}
|
package/dist/esm/co2.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
import OneByte from "./1byte.js";
|
|
3
2
|
import SustainableWebDesign from "./sustainable-web-design.js";
|
|
4
3
|
import {
|
|
@@ -14,8 +13,10 @@ class CO2 {
|
|
|
14
13
|
} else if ((options == null ? void 0 : options.model) === "swd") {
|
|
15
14
|
this.model = new SustainableWebDesign();
|
|
16
15
|
} else if (options == null ? void 0 : options.model) {
|
|
17
|
-
throw new Error(
|
|
18
|
-
|
|
16
|
+
throw new Error(
|
|
17
|
+
`"${options.model}" is not a valid model. Please use "1byte" for the OneByte model, and "swd" for the Sustainable Web Design model.
|
|
18
|
+
See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`
|
|
19
|
+
);
|
|
19
20
|
}
|
|
20
21
|
if ((options == null ? void 0 : options.results) === "segment") {
|
|
21
22
|
this.model.results = {
|
|
@@ -35,8 +36,10 @@ See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more abo
|
|
|
35
36
|
if ((_a = this.model) == null ? void 0 : _a.perVisit) {
|
|
36
37
|
return this.model.perVisit(bytes, green, this.model.results.segment);
|
|
37
38
|
} else {
|
|
38
|
-
throw new Error(
|
|
39
|
-
|
|
39
|
+
throw new Error(
|
|
40
|
+
`The perVisit() method is not supported in the model you are using. Try using perByte() instead.
|
|
41
|
+
See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`
|
|
42
|
+
);
|
|
40
43
|
}
|
|
41
44
|
}
|
|
42
45
|
perByteTrace(bytes, green = false, options = {}) {
|
|
@@ -46,7 +49,12 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
46
49
|
adjustments = parseOptions(options);
|
|
47
50
|
}
|
|
48
51
|
return {
|
|
49
|
-
co2: this.model.perByte(
|
|
52
|
+
co2: this.model.perByte(
|
|
53
|
+
bytes,
|
|
54
|
+
green,
|
|
55
|
+
this.model.results.segment,
|
|
56
|
+
adjustments
|
|
57
|
+
),
|
|
50
58
|
green,
|
|
51
59
|
variables: {
|
|
52
60
|
description: "Below are the variables used to calculate this CO2 estimate.",
|
|
@@ -69,7 +77,12 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
69
77
|
adjustments = parseOptions(options);
|
|
70
78
|
}
|
|
71
79
|
return {
|
|
72
|
-
co2: this.model.perVisit(
|
|
80
|
+
co2: this.model.perVisit(
|
|
81
|
+
bytes,
|
|
82
|
+
green,
|
|
83
|
+
this.model.results.segment,
|
|
84
|
+
adjustments
|
|
85
|
+
),
|
|
73
86
|
green,
|
|
74
87
|
variables: {
|
|
75
88
|
description: "Below are the variables used to calculate this CO2 estimate.",
|
|
@@ -87,8 +100,10 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
87
100
|
}
|
|
88
101
|
};
|
|
89
102
|
} else {
|
|
90
|
-
throw new Error(
|
|
91
|
-
|
|
103
|
+
throw new Error(
|
|
104
|
+
`The perVisitDetailed() method is not supported in the model you are using. Try using perByte() instead.
|
|
105
|
+
See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`
|
|
106
|
+
);
|
|
92
107
|
}
|
|
93
108
|
}
|
|
94
109
|
perDomain(pageXray, greenDomains) {
|
|
@@ -122,7 +137,10 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
122
137
|
for (let asset of pageXray.assets) {
|
|
123
138
|
const domain = new URL(asset.url).domain;
|
|
124
139
|
const transferSize = asset.transferSize;
|
|
125
|
-
const co2ForTransfer = this.perByte(
|
|
140
|
+
const co2ForTransfer = this.perByte(
|
|
141
|
+
transferSize,
|
|
142
|
+
greenDomains && greenDomains.indexOf(domain) > -1
|
|
143
|
+
);
|
|
126
144
|
const contentType = asset.type;
|
|
127
145
|
if (!co2PerContentType[contentType]) {
|
|
128
146
|
co2PerContentType[contentType] = { co2: 0, transferSize: 0 };
|
|
@@ -146,7 +164,10 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
146
164
|
for (let asset of pageXray.assets) {
|
|
147
165
|
const domain = new URL(asset.url).domain;
|
|
148
166
|
const transferSize = asset.transferSize;
|
|
149
|
-
const co2ForTransfer = this.perByte(
|
|
167
|
+
const co2ForTransfer = this.perByte(
|
|
168
|
+
transferSize,
|
|
169
|
+
greenDomains && greenDomains.indexOf(domain) > -1
|
|
170
|
+
);
|
|
150
171
|
allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });
|
|
151
172
|
}
|
|
152
173
|
allAssets.sort((a, b) => b.co2 - a.co2);
|
|
@@ -158,9 +179,15 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
158
179
|
const firstPartyRegEx = pageXray.firstPartyRegEx;
|
|
159
180
|
for (let d of Object.keys(pageXray.domains)) {
|
|
160
181
|
if (!d.match(firstPartyRegEx)) {
|
|
161
|
-
thirdParty += this.perByte(
|
|
182
|
+
thirdParty += this.perByte(
|
|
183
|
+
pageXray.domains[d].transferSize,
|
|
184
|
+
greenDomains && greenDomains.indexOf(d) > -1
|
|
185
|
+
);
|
|
162
186
|
} else {
|
|
163
|
-
firstParty += this.perByte(
|
|
187
|
+
firstParty += this.perByte(
|
|
188
|
+
pageXray.domains[d].transferSize,
|
|
189
|
+
greenDomains && greenDomains.indexOf(d) > -1
|
|
190
|
+
);
|
|
164
191
|
}
|
|
165
192
|
}
|
|
166
193
|
return { firstParty, thirdParty };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import fileSize from "./file-size.js";
|
|
2
|
-
import testConstants from "./test-constants.js";
|
|
3
2
|
const KWH_PER_GB = 0.81;
|
|
4
3
|
const END_USER_DEVICE_ENERGY = 0.52;
|
|
5
4
|
const NETWORK_ENERGY = 0.14;
|
|
@@ -21,6 +20,5 @@ export {
|
|
|
21
20
|
PRODUCTION_ENERGY,
|
|
22
21
|
RENEWABLES_GRID_INTENSITY,
|
|
23
22
|
RETURNING_VISITOR_PERCENTAGE,
|
|
24
|
-
fileSize
|
|
25
|
-
testConstants
|
|
23
|
+
fileSize
|
|
26
24
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const data = { "AFRICA": "
|
|
1
|
+
const data = { "AFG": "120.48", "AFRICA": "483.89", "DZA": "488.39", "ASM": "687.5", "AGO": "168.59", "ATG": "657.14", "ARG": "357.79", "ARM": "222.68", "ABW": "591.4", "ASIA": "543.26", "AUS": "531.25", "AUT": "147.57", "AZE": "532.9", "BHS": "698.11", "BHR": "489.92", "BGD": "560.51", "BRB": "644.86", "BLR": "415.77", "BEL": "156.92", "BLZ": "484.38", "BEN": "666.67", "BMU": "", "BTN": "24.44", "BOL": "306.89", "BIH": "489.07", "BWA": "794.52", "BRA": "158.59", "BRN": "658.85", "BGR": "354.33", "BFA": "611.43", "BDI": "250", "CPV": "600", "KHM": "399.31", "CMR": "278.26", "CAN": "128.46", "CYM": "684.93", "CAF": "0", "TCD": "677.42", "CHL": "416.88", "CHN": "544.36", "COL": "181.68", "COM": "714.29", "COD": "25.36", "COG": "398.01", "COK": "400", "CRI": "33.05", "CIV": "390.71", "HRV": "210.98", "CUB": "574.68", "CYP": "599.61", "CZE": "405.59", "DNK": "221.42", "DJI": "666.67", "DMA": "529.41", "DOM": "540.39", "ECU": "136.05", "EGY": "464.43", "SLV": "185.98", "GNQ": "492.96", "ERI": "688.89", "EST": "481.22", "SWZ": "189.19", "ETH": "25.19", "EU": "261.91", "EUROPE": "288.13", "FLK": "500", "FRO": "428.57", "FJI": "292.04", "FIN": "146.21", "FRA": "67.39", "GUF": "", "PYF": "471.43", "G20": "446.08", "G7": "340.97", "GAB": "397.38", "GMB": "700", "GEO": "111.55", "DEU": "365.98", "GHA": "362.94", "GIB": "", "GRC": "353.34", "GRL": "133.33", "GRD": "714.29", "GLP": "", "GUM": "670.33", "GTM": "313.02", "GIN": "208.63", "GNB": "750", "GUY": "634.15", "HTI": "606.06", "HND": "374.27", "HKG": "684.82", "HUN": "233.04", "IND": "637.18", "IDN": "623.28", "IRN": "492.68", "IRQ": "500.21", "IRL": "363.61", "ISR": "550.49", "ITA": "340.79", "JAM": "524.14", "JPN": "478.55", "JOR": "399.91", "KAZ": "656.1", "KEN": "87.07", "KIR": "666.67", "PRK": "157.79", "KWT": "489.61", "KGZ": "104.35", "LAO": "242.18", "LATIN AMERICA AND CARIBBEAN": "261.4", "LVA": "223.93", "LBN": "545.94", "LSO": "20", "LBR": "304.35", "LBY": "496.57", "LTU": "244.6", "LUX": "186.99", "MAC": "491.53", "MDG": "483.25", "MWI": "133.8", "MYS": "548.61", "MDV": "651.52", "MLI": "466.08", "MLT": "451.61", "MTQ": "", "MRT": "526.6", "MUS": "611.11", "MEX": "399.85", "MDA": "645.65", "MNG": "741.57", "MNE": "335.96", "MSR": "1000", "MAR": "610.41", "MOZ": "125.06", "MMR": "348.84", "NAM": "63.69", "NRU": "750", "NPL": "24.51", "NLD": "388.62", "NCL": "610.12", "NZL": "133.35", "NIC": "354.21", "NER": "622.22", "NGA": "377.19", "NIU": "", "NORTH AMERICA": "346.48", "MKD": "487.37", "NOR": "26.07", "OCEANIA": "474.92", "OECD": "341.29", "OMN": "488.24", "PAK": "365.19", "PSE": "465.12", "PAN": "193.92", "PNG": "526.75", "PRY": "25.49", "PER": "228.59", "PHL": "581.15", "POL": "660.12", "PRT": "219.05", "PRI": "681.47", "QAT": "489.9", "REU": "", "ROU": "255.88", "RUS": "359.94", "RWA": "294.12", "SHN": "", "KNA": "681.82", "LCA": "685.71", "SPM": "800", "VCT": "500", "WSM": "470.59", "STP": "600", "SAU": "571.34", "SEN": "502.67", "SRB": "548.94", "SYC": "615.39", "SLE": "47.62", "SGP": "488.59", "SVK": "168.28", "SVN": "238.61", "SLB": "727.27", "SOM": "634.15", "ZAF": "715.68", "KOR": "458.08", "SSD": "684.21", "ESP": "193.86", "LKA": "479.83", "SDN": "242.92", "SUR": "356.44", "SWE": "45.82", "CHE": "47.1", "SYR": "540.57", "TWN": "573.28", "TJK": "84.14", "TZA": "361.08", "THA": "506.64", "TLS": "698.11", "TGO": "507.94", "TON": "625", "TTO": "520.05", "TUN": "468.23", "TUR": "432.29", "TKM": "544.39", "TCA": "703.7", "UGA": "52.27", "UKR": "240.18", "ARE": "461.85", "GBR": "268.33", "USA": "379.38", "URY": "147.28", "UZB": "506.16", "VUT": "571.43", "VEN": "189.45", "VNM": "448.22", "VGB": "714.29", "VIR": "685.71", "ESH": "", "WORLD": "441.27", "YEM": "528.41", "ZMB": "84.7", "ZWE": "393.04" };
|
|
2
2
|
const type = "average";
|
|
3
3
|
const year = "2021";
|
|
4
4
|
var average_intensities_2021_min_default = { data, type, year };
|