From 2474b1c06a204e6fbe4b0a1b2f93787d72b74efe Mon Sep 17 00:00:00 2001 From: Danielle Subject Date: Fri, 19 Jul 2024 11:06:23 -0400 Subject: [PATCH 1/9] Added basic error handling to data fetch in emissions script --- data/functions/generate_average_co2.js | 139 +++++++++++++------------ 1 file changed, 75 insertions(+), 64 deletions(-) diff --git a/data/functions/generate_average_co2.js b/data/functions/generate_average_co2.js index 8161c139..a5238fa1 100644 --- a/data/functions/generate_average_co2.js +++ b/data/functions/generate_average_co2.js @@ -24,79 +24,90 @@ const type = "average"; // Save a minified version of the JS file to the src/data folder (async () => { - const response = await fetch(sourceURL); - const data = await response.json(); - - // Group data by country_code - const groupedData = await data.reduce((acc, item) => { - const key = - item.country_code === "" ? item.country_or_region : item.country_code; - if (!acc[key]) { - acc[key] = []; + try { + const response = await fetch(sourceURL); + if (!response.ok) { + throw new Error(`Network response was not ok: ${response.statusText}`); } - acc[key].push(item); - return acc; - }, {}); + const data = await response.json(); + + // Group data by country_code + const groupedData = await data.reduce((acc, item) => { + const key = + item.country_code === "" ? item.country_or_region : item.country_code; + if (!acc[key]) { + acc[key] = []; + } + acc[key].push(item); + return acc; + }, {}); + + // Loop through the grouped data and find the latest year + const latestData = await Object.keys(groupedData).reduce((acc, key) => { + // Find the last year in the array with emissions intensity data + const latestYear = groupedData[key].reduce((acc, item, index) => { + if ( + item.emissions_intensity_gco2_per_kwh === null || + item.emissions_intensity_gco2_per_kwh === "" + ) { + return acc; + } + return index; + }, 0); - // Loop through the grouped data and find the latest year - const latestData = await Object.keys(groupedData).reduce((acc, key) => { - // Find the last year in the array with emissions intensity data - const latestYear = groupedData[key].reduce((acc, item, index) => { + acc[key] = groupedData[key][latestYear]; + return acc; + }, {}); + + // Loop through the data and extract the emissions intensity data + // Save it to the gridIntensityResults object with the country code as the key + Object.values(latestData).forEach((row) => { if ( - item.emissions_intensity_gco2_per_kwh === null || - item.emissions_intensity_gco2_per_kwh === "" + row.emissions_intensity_gco2_per_kwh === null || + row.emissions_intensity_gco2_per_kwh === "" ) { - return acc; + return; } - return index; - }, 0); - - acc[key] = groupedData[key][latestYear]; - return acc; - }, {}); - // Loop through the data and extract the emissions intensity data - // Save it to the gridIntensityResults object with the country code as the key - Object.values(latestData).forEach((row) => { - if ( - row.emissions_intensity_gco2_per_kwh === null || - row.emissions_intensity_gco2_per_kwh === "" - ) { - return; - } + const country = + row.country_code === "" ? row.country_or_region : row.country_code; - const country = - row.country_code === "" ? row.country_or_region : row.country_code; + gridIntensityResults[country.toUpperCase()] = + row.emissions_intensity_gco2_per_kwh; - gridIntensityResults[country.toUpperCase()] = - row.emissions_intensity_gco2_per_kwh; + generalResults[country] = { + country_code: row.country_code, + country_or_region: row.country_or_region, + year: row.year, + emissions_intensity_gco2_per_kwh: row.emissions_intensity_gco2_per_kwh, + }; + }); - generalResults[country] = { - country_code: row.country_code, - country_or_region: row.country_or_region, - year: row.year, - emissions_intensity_gco2_per_kwh: row.emissions_intensity_gco2_per_kwh, - }; - }); + // Ensure directories exist + fs.mkdirSync("data/output", { recursive: true }); + fs.mkdirSync("src/data", { recursive: true }); - // This saves the country code and emissions data only, for use in the CO2.js library - fs.writeFileSync( - "data/output/average-intensities.js", - `const data = ${JSON.stringify(gridIntensityResults, null, " ")}; -const type = "${type}"; -export { data, type }; -export default { data, type }; -` - ); - // Save a minified version to the src folder so that it can be easily imported into the library - fs.writeFileSync( - "src/data/average-intensities.min.js", - `const data = ${JSON.stringify(gridIntensityResults)}; const type = "${type}"; export { data, type }; export default { data, type };` - ); + // This saves the country code and emissions data only, for use in the CO2.js library + fs.writeFileSync( + "data/output/average-intensities.js", + `const data = ${JSON.stringify(gridIntensityResults, null, " ")}; + const type = "${type}"; + export { data, type }; + export default { data, type }; + ` + ); + // Save a minified version to the src folder so that it can be easily imported into the library + fs.writeFileSync( + "src/data/average-intensities.min.js", + `const data = ${JSON.stringify(gridIntensityResults)}; const type = "${type}"; export { data, type }; export default { data, type };` + ); - // This saves the full data set as a JSON file for reference. - fs.writeFileSync( - "data/output/average-intensities.json", - JSON.stringify(generalResults, null, " ") - ); + // This saves the full data set as a JSON file for reference. + fs.writeFileSync( + "data/output/average-intensities.json", + JSON.stringify(generalResults, null, " ") + ); + } catch (error) { + console.error("Error fetching or processing data:", error); + } })(); \ No newline at end of file From eb4e9ff2144be97906390c108e512304344d6ee2 Mon Sep 17 00:00:00 2001 From: Malay Kumar Date: Sat, 12 Oct 2024 18:58:53 +0530 Subject: [PATCH 2/9] Add JsDoc comments to generate_average_co2 --- data/functions/generate_average_co2.js | 74 +++++++++++++++++++++----- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/data/functions/generate_average_co2.js b/data/functions/generate_average_co2.js index 8161c139..afb89959 100644 --- a/data/functions/generate_average_co2.js +++ b/data/functions/generate_average_co2.js @@ -1,16 +1,46 @@ +/** + * @fileoverview This script generates average CO2 emissions intensity data for countries using the Ember API. + * It processes the data and saves it in various formats for use in the CO2.js library. + * @author Chris Adams + * @version 1.0.0 + */ + const fs = require("fs"); -// This URL from Ember returns ALL the data for the country_overview_yearly table +/** + * The URL for the Ember API that provides country overview data on a yearly basis. + * @constant {string} + */ const sourceURL = "https://ember-data-api-scg3n.ondigitalocean.app/ember/country_overview_yearly.json?_sort=rowid&_shape=array"; + + +/** + * Object to store the grid intensity results for each country. + * @type {Object.} + */ const gridIntensityResults = {}; + + +/** + * Object to store general results including additional country information. + * @type {Object.} + */ const generalResults = {}; + + +/** + * The type of intensity data being processed (average or marginal). + * @constant {string} + */ const type = "average"; /** - * This function generates the average CO2 emissions data for each country. - * It reads the data from the Ember API and saves it in the data/output folder. - * It also saves the data as a minified file in the src/data folder, so that it can be imported into the library. + * Fetches data from the Ember API, processes it to extract the latest average CO2 emissions + * intensity data for each country, and saves the results in various formats. + * @async + * @function + * @returns {Promise} */ // Use async/await @@ -27,7 +57,10 @@ const type = "average"; const response = await fetch(sourceURL); const data = await response.json(); - // Group data by country_code + /** + * Groups the API data by country code. + * @type {Object.} + */ const groupedData = await data.reduce((acc, item) => { const key = item.country_code === "" ? item.country_or_region : item.country_code; @@ -38,7 +71,10 @@ const type = "average"; return acc; }, {}); - // Loop through the grouped data and find the latest year + /** + * Extracts the latest year's data for each country. + * @type {Object.} + */ const latestData = await Object.keys(groupedData).reduce((acc, key) => { // Find the last year in the array with emissions intensity data const latestYear = groupedData[key].reduce((acc, item, index) => { @@ -79,22 +115,32 @@ const type = "average"; }; }); - // This saves the country code and emissions data only, for use in the CO2.js library + /** + * Saves the country code and emissions data for use in the CO2.js library. + * @type {void} + */ fs.writeFileSync( "data/output/average-intensities.js", - `const data = ${JSON.stringify(gridIntensityResults, null, " ")}; -const type = "${type}"; -export { data, type }; -export default { data, type }; -` + ` + const data = ${JSON.stringify(gridIntensityResults, null, " ")}; + const type = "${type}"; + export { data, type }; + export default { data, type }; + ` ); - // Save a minified version to the src folder so that it can be easily imported into the library + /** + * Saves a minified version of the data for easy import into the library. + * @type {void} + */ fs.writeFileSync( "src/data/average-intensities.min.js", `const data = ${JSON.stringify(gridIntensityResults)}; const type = "${type}"; export { data, type }; export default { data, type };` ); - // This saves the full data set as a JSON file for reference. + /** + * Saves the full data set as a JSON file for reference. + * @type {void} + */ fs.writeFileSync( "data/output/average-intensities.json", JSON.stringify(generalResults, null, " ") From 29b6755fff02875d7b360ea9eead854c72bee51e Mon Sep 17 00:00:00 2001 From: Malay Kumar Date: Sat, 12 Oct 2024 18:59:08 +0530 Subject: [PATCH 3/9] Add JsDoc comments to generate_marginal_co2 --- data/functions/generate_marginal_co2.js | 85 ++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 8 deletions(-) diff --git a/data/functions/generate_marginal_co2.js b/data/functions/generate_marginal_co2.js index c63e368d..ebf1f458 100644 --- a/data/functions/generate_marginal_co2.js +++ b/data/functions/generate_marginal_co2.js @@ -1,17 +1,71 @@ +/** + * @fileoverview This script generates marginal CO2 emissions intensity data for countries using UNFCCC data. + * It processes the data from a CSV file and saves it in various formats for use in the CO2.js library. + * @author Chris Adams + * @version 1.0.0 + */ + const fs = require("fs"); + +/** + * Reads the UNFCCC CSV file containing grid factors data. + * @type {Buffer} + */ const csv = fs.readFileSync( "data/IFI_Default_Grid_Factors_2021_v3.1_unfccc.csv" ); + + +/** + * Utility function to parse CSV rows. + * @type {function} + */ const parseCSVRow = require("../utils/parseCSVRow"); + + +/** + * Utility function to get country codes. + * @type {function} + */ const getCountryCodes = require("../utils/getCountryCodes"); + +/** + * The type of intensity data being processed (marginal). + * @constant {string} + */ const type = "marginal"; + +/** + * The source of the data (UNFCCC). + * @constant {string} + */ const source = "UNFCCC"; + +/** + * The year of the data. + * @constant {string} + */ const year = "2021"; + +/** + * Converts the CSV data to an array of strings, each representing a row. + * @type {string[]} + */ const array = csv.toString().split("\n"); -/* Store the converted result into an array */ + +/** + * Object to store the converted CSV data. + * @type {Object.} + */ const csvToJsonResult = {}; + + +/** + * Object to store the grid intensity results for each country. + * @type {Object.} + */ const gridIntensityResults = {}; /* Store the CSV column headers into seprate variable */ @@ -79,20 +133,35 @@ for (let currentArrayString of array.slice(5)) { const json = JSON.stringify(csvToJsonResult); const gridIntensityJson = JSON.stringify(gridIntensityResults); -// This saves the country code and emissions data only, for use in the CO2.js library + +/** + * Saves the country code and emissions data for use in the CO2.js library. + * @type {void} + */ fs.writeFileSync( "data/output/marginal-intensities-2021.js", - `const data = ${gridIntensityJson}; + ` + const data = ${gridIntensityJson}; const type = "${type}"; -const year = "${year}"; -export { data, type, year }; -export default { data, type, year };` + const year = "${year}"; + export { data, type, year }; + export default { data, type, year }; + ` ); -// Save a minified version to the src folder so that it can be easily imported into the library + + +/** + * Saves a minified version of the data for easy import into the library. + * @type {void} + */ fs.writeFileSync( "src/data/marginal-intensities-2021.min.js", `const data = ${gridIntensityJson}; const type = "${type}"; const year = "${year}"; export { data, type, year }; export default { data, type, year };` ); -// This saves the full data set as a JSON file for reference. + +/** + * Saves the full data set as a JSON file for reference. + * @type {void} + */ fs.writeFileSync("data/output/marginal-intensities-2021.json", json); From 6b000a02601089a14de697c8bd49c41f1ffe8056 Mon Sep 17 00:00:00 2001 From: Malay Kumar Date: Sat, 12 Oct 2024 19:01:19 +0530 Subject: [PATCH 4/9] Add JsDoc comments to average-intensities --- src/data/average-intensities.min.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/data/average-intensities.min.js b/src/data/average-intensities.min.js index 64dacd2c..51051050 100644 --- a/src/data/average-intensities.min.js +++ b/src/data/average-intensities.min.js @@ -1 +1,7 @@ +/** + * @fileoverview Minified average CO2 emissions intensity data for countries. + * @generated Generated by generate_average_co2.js + * @version 1.0.0 +*/ + const data = {"AFG":132.53,"AFRICA":547.83,"ALB":24.29,"DZA":634.61,"ASM":611.11,"AGO":174.73,"ATG":611.11,"ARG":353.96,"ARM":264.54,"ABW":561.22,"ASEAN":570.41,"ASIA":591.13,"AUS":556.3,"AUT":110.81,"AZE":671.39,"BHS":660.1,"BHR":904.62,"BGD":691.41,"BRB":605.51,"BLR":441.74,"BEL":138.11,"BLZ":225.81,"BEN":584.07,"BTN":23.33,"BOL":531.69,"BIH":601.29,"BWA":847.91,"BRA":96.4,"BRN":893.91,"BGR":335.33,"BFA":467.53,"BDI":250,"CPV":558.14,"KHM":417.71,"CMR":305.42,"CAN":165.15,"CYM":642.86,"CAF":0,"TCD":628.57,"CHL":291.11,"CHN":583.61,"COL":259.51,"COM":642.86,"COG":700,"COD":24.46,"COK":250,"CRI":53.38,"CIV":393.89,"HRV":204.96,"CUB":637.61,"CYP":534.32,"CZE":449.72,"DNK":151.65,"DJI":692.31,"DMA":529.41,"DOM":580.78,"ECU":166.91,"EGY":574.04,"SLV":224.76,"GNQ":591.84,"ERI":631.58,"EST":416.67,"SWZ":172.41,"ETH":24.64,"EU":243.93,"EUROPE":302.29,"FLK":500,"FRO":404.76,"FJI":288.46,"FIN":79.16,"FRA":56.02,"GUF":217.82,"PYF":442.86,"G20":477.89,"G7":341.44,"GAB":491.6,"GMB":666.67,"GEO":167.59,"DEU":381.41,"GHA":484,"GRC":336.57,"GRL":178.57,"GRD":640,"GLP":500,"GUM":622.86,"GTM":328.27,"GIN":236.84,"GNB":625,"GUY":640.35,"HTI":567.31,"HND":282.27,"HKG":699.5,"HUN":204.19,"ISL":27.68,"IND":713.01,"IDN":682.43,"IRN":641.73,"IRQ":688.81,"IRL":282.98,"ISR":582.93,"ITA":330.72,"JAM":555.56,"JPN":493.59,"JOR":540.92,"KAZ":821.9,"KEN":71.2,"KIR":666.67,"XKX":894.65,"KWT":649.16,"KGZ":147.29,"LAO":265.51,"LATIN AMERICA AND CARIBBEAN":256.03,"LVA":123.99,"LBN":599.01,"LSO":20,"LBR":227.85,"LBY":818.69,"LTU":160.07,"LUX":105.26,"MAC":448.98,"MDG":436.44,"MWI":66.67,"MYS":607.88,"MDV":611.77,"MLI":408,"MLT":459.14,"MTQ":523.18,"MRT":464.71,"MUS":632.48,"MEX":492.34,"MIDDLE EAST":643.04,"MDA":643.46,"MNG":775.31,"MNE":418.09,"MSR":1000,"MAR":624.45,"MOZ":135.65,"MMR":436.92,"NAM":59.26,"NRU":750,"NPL":24.44,"NLD":268.48,"NCL":660.58,"NZL":112.76,"NIC":265.12,"NER":670.89,"NGA":523.25,"NORTH AMERICA":342.95,"PRK":389.59,"MKD":556.19,"NOR":30.05,"OCEANIA":495.74,"OECD":341.27,"OMN":564.55,"PAK":440.61,"PSE":516.13,"PAN":161.68,"PNG":507.25,"PRY":24.31,"PER":266.48,"POL":661.93,"PRT":165.55,"PRI":677.96,"QAT":602.5,"REU":572.82,"ROU":240.58,"RUS":445.02,"RWA":316.33,"KNA":636.36,"LCA":666.67,"SPM":600,"VCT":529.41,"WSM":473.68,"STP":642.86,"SAU":696.31,"SEN":511.6,"SRB":647.52,"SYC":564.52,"SLE":50,"SGP":470.78,"SVK":116.77,"SVN":231.28,"SLB":700,"SOM":578.95,"ZAF":709.69,"KOR":432.11,"SSD":629.03,"ESP":174.05,"LKA":509.78,"SDN":263.16,"SUR":349.28,"SWE":40.7,"CHE":34.68,"SYR":701.66,"TWN":644.36,"TJK":116.86,"TZA":339.25,"THA":549.85,"PHL":610.74,"TGO":443.18,"TON":625,"TTO":681.53,"TUN":563.96,"TUR":464.59,"TKM":1306.03,"TCA":653.85,"UGA":44.53,"UKR":256.21,"ARE":492.7,"GBR":228.25,"USA":369.47,"URY":128.79,"UZB":1167.6,"VUT":571.43,"VEN":185.8,"VNM":472.47,"VGB":647.06,"VIR":632.35,"WORLD":481.65,"YEM":566.1,"ZMB":111.97,"ZWE":297.87}; const type = "average"; export { data, type }; export default { data, type }; \ No newline at end of file From 5e58971b57206cebb1c8c8985e94461a59b74ac5 Mon Sep 17 00:00:00 2001 From: Malay Kumar Date: Sat, 12 Oct 2024 19:01:39 +0530 Subject: [PATCH 5/9] Add JsDoc comments to marginal-intensities-2021 --- src/data/marginal-intensities-2021.min.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/data/marginal-intensities-2021.min.js b/src/data/marginal-intensities-2021.min.js index 3db75c07..988b82cc 100644 --- a/src/data/marginal-intensities-2021.min.js +++ b/src/data/marginal-intensities-2021.min.js @@ -1 +1,7 @@ +/** + * @fileoverview Minified marginal CO2 emissions intensity data for countries (2021). + * @generated Generated by generate_marginal_co2.js + * @version 1.0.0 + */ + const data = {"AFG":"414","ALB":"0","DZA":"528","ASM":"753","AND":"188","AGO":"1476","AIA":"753","ATG":"753","ARG":"478","ARM":"390","ABW":"753","AUS":"808","AUT":"242","AZE":"534","AZORES (PORTUGAL)":"753","BHS":"753","BHR":"726","BGD":"528","BRB":"749","BLR":"400","BEL":"252","BLZ":"403","BEN":"745","BMU":"753","BTN":"0","BOL":"604","BES":"753","BIH":"1197","BWA":"1486","BRA":"284","VGB":"753","BRN":"681","BGR":"911","BFA":"753","BDI":"414","KHM":"1046","CMR":"659","CAN":"372","CYM":"753","CPV":"753","CAF":"188","TCD":"753","CHANNEL ISLANDS (U.K)":"753","CHL":"657","CHN":"899","COL":"410","COM":"753","COD":"0","COG":"659","COK":"753","CRI":"108","CIV":"466","HRV":"294","CUB":"559","CUW":"876","CYP":"751","CZE":"902","DNK":"362","DJI":"753","DMA":"753","DOM":"601","ECU":"560","EGY":"554","SLV":"547","GNQ":"632","ERI":"915","EST":"1057","SWZ":"0","ETH":"0","FLK":"753","FRO":"753","FJI":"640","FIN":"267","FRA":"158","GUF":"423","PYF":"753","GAB":"946","GMB":"753","GEO":"289","DEU":"650","GHA":"495","GIB":"779","GRC":"507","GRL":"264","GRD":"753","GLP":"753","GUM":"753","GTM":"798","GIN":"753","GNB":"753","GUY":"847","HTI":"1048","HND":"662","HUN":"296","ISL":"0","IND":"951","IDN":"783","IRN":"592","IRQ":"1080","IRL":"380","IMN":"436","ISR":"394","ITA":"414","JAM":"711","JPN":"471","JOR":"529","KAZ":"797","KEN":"574","KIR":"753","PRK":"754","KOR":"555","XKX":"1145","KWT":"675","KGZ":"217","LAO":"1069","LVA":"240","LBN":"794","LSO":"0","LBR":"677","LBY":"668","LIE":"151","LTU":"211","LUX":"220","MDG":"876","MADEIRA (PORTUGAL)":"663","MWI":"489","MYS":"551","MDV":"753","MLI":"1076","MLT":"520","MHL":"753","MTQ":"753","MRT":"753","MUS":"700","MYT":"753","MEX":"531","FSM":"753","MDA":"541","MCO":"158","MNG":"1366","MNE":"899","MSR":"753","MAR":"729","MOZ":"234","MMR":"719","NAM":"355","NRU":"753","NPL":"0","NLD":"326","NCL":"779","NZL":"246","NIC":"675","NER":"772","NGA":"526","NIU":"753","MKD":"851","MNP":"753","NOR":"47","OMN":"479","PAK":"592","PLW":"753","PSE":"719","PAN":"477","PNG":"597","PRY":"0","PER":"473","PHL":"672","POL":"828","PRT":"389","PRI":"596","QAT":"503","REU":"772","ROU":"489","RUS":"476","RWA":"712","SHN":"753","KNA":"753","LCA":"753","MAF":"753","SPM":"753","VCT":"753","WSM":"753","SMR":"414","STP":"753","SAU":"592","SEN":"870","SRB":"1086","SYC":"753","SLE":"489","SGP":"379","SXM":"753","SVK":"332","SVN":"620","SLB":"753","SOM":"753","ZAF":"1070","SSD":"890","ESP":"402","LKA":"731","SDN":"736","SUR":"1029","SWE":"68","CHE":"48","SYR":"713","TWN":"484","TJK":"255","TZA":"531","THA":"450","TLS":"753","TGO":"859","TON":"753","TTO":"559","TUN":"468","TUR":"376","TKM":"927","TCA":"753","TUV":"753","UGA":"279","UKR":"768","ARE":"556","GBR":"380","USA":"416","URY":"174","UZB":"612","VUT":"753","VEN":"711","VNM":"560","VIR":"650","YEM":"807","ZMB":"416","ZWE":"1575","MEMO: EU 27":"409"}; const type = "marginal"; const year = "2021"; export { data, type, year }; export default { data, type, year }; \ No newline at end of file From 33c66d7db233efe43dbfccb237f1580bec3e8067 Mon Sep 17 00:00:00 2001 From: fershad <27988517+fershad@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:46:00 +0800 Subject: [PATCH 6/9] add note about commenting --- CONTRIBUTING.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dbc1f697..84b346d5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,17 +9,17 @@ Thank you for considering contributing to CO2.js. Open source is at the heart of **NB** Changes/commits that are not linked to an issue will not be accepted. ### New issues - - Our issue template provides you with a scaffold to follow when raising a new issue. There are three formats to choose from: - 1. *Bugs* - clearly describe the problem you're facing including steps to reproduce it. Please also be sure to include the information about the environment your code was running in (e.g. NodeJS, Deno, Edge Worker, Browser etc). - 1. *New features* - clearly describe the new feature you'd like to see added and provide a reason for why it should be added. eg what will be improved/possible as a result of making your suggested change. - 1. *Request addition of carbon estimation model to CO2.js* - please provide as much information about the model as possible including links to additional documentation and information about how the model is licensed. - +- Our issue template provides you with a scaffold to follow when raising a new issue. There are three formats to choose from: + 1. _Bugs_ - clearly describe the problem you're facing including steps to reproduce it. Please also be sure to include the information about the environment your code was running in (e.g. NodeJS, Deno, Edge Worker, Browser etc). + 1. _New features_ - clearly describe the new feature you'd like to see added and provide a reason for why it should be added. eg what will be improved/possible as a result of making your suggested change. + 1. _Request addition of carbon estimation model to CO2.js_ - please provide as much information about the model as possible including links to additional documentation and information about how the model is licensed. ## Making Changes - Fork the repository on GitHub. - Create a topic branch from where you want to base your work. This branch should usually be based off `main`. +- Remember to add JSDoc comments to any new functions or variables that you are introducing into the codebase. - Make commits of logical units. - Make commit messages clear and understandable. @@ -28,10 +28,10 @@ Thank you for considering contributing to CO2.js. Open source is at the heart of - Push the changes made in your branch to your fork of this repository. - Submit a [pull request](https://github.com/thegreenwebfoundation/co2.js/pulls) to the CO2.js repository in the `thegreenwebfoundation` organization. - When opening a new pull request, you'll see a template. Please follow it. It asks you to state: - - the type of change (choose from a list) - - link to the related issue - - what documentation needs updating as a result (choose from a list) - - what your changes are - be sure to clearly explain the changes you've made, any new files, dependencies, or network requests that have been added, and provide any additional context to help reviewers understand the changes made. + - the type of change (choose from a list) + - link to the related issue + - what documentation needs updating as a result (choose from a list) + - what your changes are - be sure to clearly explain the changes you've made, any new files, dependencies, or network requests that have been added, and provide any additional context to help reviewers understand the changes made. - Your pull request will be reviewed by a maintainer from Green Web Foundation. - After feedback has been given we expect responses within two weeks. After two weeks we may close the pull request if it isn't showing any activity. @@ -43,4 +43,4 @@ Please note that this project is released with a [Contributor Code of Conduct](h ## Thank you -Again, thank you for your contributions. We appreciate your help improving CO2.js, and we look forward to your future contributions! \ No newline at end of file +Again, thank you for your contributions. We appreciate your help improving CO2.js, and we look forward to your future contributions! From cab18cf530cb559db3c349539b3767b4318ad2d3 Mon Sep 17 00:00:00 2001 From: fershad <27988517+fershad@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:05:24 +0800 Subject: [PATCH 7/9] add comments about constants in source files --- src/data/average-intensities.min.js | 4 ++++ src/data/marginal-intensities-2021.min.js | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/data/average-intensities.min.js b/src/data/average-intensities.min.js index 51051050..601c7746 100644 --- a/src/data/average-intensities.min.js +++ b/src/data/average-intensities.min.js @@ -4,4 +4,8 @@ * @version 1.0.0 */ +/** + * @constant {Object.} data - Average CO2 emissions intensity data for various countries. + * @constant {string} type - Type of data being represented. + */ const data = {"AFG":132.53,"AFRICA":547.83,"ALB":24.29,"DZA":634.61,"ASM":611.11,"AGO":174.73,"ATG":611.11,"ARG":353.96,"ARM":264.54,"ABW":561.22,"ASEAN":570.41,"ASIA":591.13,"AUS":556.3,"AUT":110.81,"AZE":671.39,"BHS":660.1,"BHR":904.62,"BGD":691.41,"BRB":605.51,"BLR":441.74,"BEL":138.11,"BLZ":225.81,"BEN":584.07,"BTN":23.33,"BOL":531.69,"BIH":601.29,"BWA":847.91,"BRA":96.4,"BRN":893.91,"BGR":335.33,"BFA":467.53,"BDI":250,"CPV":558.14,"KHM":417.71,"CMR":305.42,"CAN":165.15,"CYM":642.86,"CAF":0,"TCD":628.57,"CHL":291.11,"CHN":583.61,"COL":259.51,"COM":642.86,"COG":700,"COD":24.46,"COK":250,"CRI":53.38,"CIV":393.89,"HRV":204.96,"CUB":637.61,"CYP":534.32,"CZE":449.72,"DNK":151.65,"DJI":692.31,"DMA":529.41,"DOM":580.78,"ECU":166.91,"EGY":574.04,"SLV":224.76,"GNQ":591.84,"ERI":631.58,"EST":416.67,"SWZ":172.41,"ETH":24.64,"EU":243.93,"EUROPE":302.29,"FLK":500,"FRO":404.76,"FJI":288.46,"FIN":79.16,"FRA":56.02,"GUF":217.82,"PYF":442.86,"G20":477.89,"G7":341.44,"GAB":491.6,"GMB":666.67,"GEO":167.59,"DEU":381.41,"GHA":484,"GRC":336.57,"GRL":178.57,"GRD":640,"GLP":500,"GUM":622.86,"GTM":328.27,"GIN":236.84,"GNB":625,"GUY":640.35,"HTI":567.31,"HND":282.27,"HKG":699.5,"HUN":204.19,"ISL":27.68,"IND":713.01,"IDN":682.43,"IRN":641.73,"IRQ":688.81,"IRL":282.98,"ISR":582.93,"ITA":330.72,"JAM":555.56,"JPN":493.59,"JOR":540.92,"KAZ":821.9,"KEN":71.2,"KIR":666.67,"XKX":894.65,"KWT":649.16,"KGZ":147.29,"LAO":265.51,"LATIN AMERICA AND CARIBBEAN":256.03,"LVA":123.99,"LBN":599.01,"LSO":20,"LBR":227.85,"LBY":818.69,"LTU":160.07,"LUX":105.26,"MAC":448.98,"MDG":436.44,"MWI":66.67,"MYS":607.88,"MDV":611.77,"MLI":408,"MLT":459.14,"MTQ":523.18,"MRT":464.71,"MUS":632.48,"MEX":492.34,"MIDDLE EAST":643.04,"MDA":643.46,"MNG":775.31,"MNE":418.09,"MSR":1000,"MAR":624.45,"MOZ":135.65,"MMR":436.92,"NAM":59.26,"NRU":750,"NPL":24.44,"NLD":268.48,"NCL":660.58,"NZL":112.76,"NIC":265.12,"NER":670.89,"NGA":523.25,"NORTH AMERICA":342.95,"PRK":389.59,"MKD":556.19,"NOR":30.05,"OCEANIA":495.74,"OECD":341.27,"OMN":564.55,"PAK":440.61,"PSE":516.13,"PAN":161.68,"PNG":507.25,"PRY":24.31,"PER":266.48,"POL":661.93,"PRT":165.55,"PRI":677.96,"QAT":602.5,"REU":572.82,"ROU":240.58,"RUS":445.02,"RWA":316.33,"KNA":636.36,"LCA":666.67,"SPM":600,"VCT":529.41,"WSM":473.68,"STP":642.86,"SAU":696.31,"SEN":511.6,"SRB":647.52,"SYC":564.52,"SLE":50,"SGP":470.78,"SVK":116.77,"SVN":231.28,"SLB":700,"SOM":578.95,"ZAF":709.69,"KOR":432.11,"SSD":629.03,"ESP":174.05,"LKA":509.78,"SDN":263.16,"SUR":349.28,"SWE":40.7,"CHE":34.68,"SYR":701.66,"TWN":644.36,"TJK":116.86,"TZA":339.25,"THA":549.85,"PHL":610.74,"TGO":443.18,"TON":625,"TTO":681.53,"TUN":563.96,"TUR":464.59,"TKM":1306.03,"TCA":653.85,"UGA":44.53,"UKR":256.21,"ARE":492.7,"GBR":228.25,"USA":369.47,"URY":128.79,"UZB":1167.6,"VUT":571.43,"VEN":185.8,"VNM":472.47,"VGB":647.06,"VIR":632.35,"WORLD":481.65,"YEM":566.1,"ZMB":111.97,"ZWE":297.87}; const type = "average"; export { data, type }; export default { data, type }; \ No newline at end of file diff --git a/src/data/marginal-intensities-2021.min.js b/src/data/marginal-intensities-2021.min.js index 988b82cc..6e0097f4 100644 --- a/src/data/marginal-intensities-2021.min.js +++ b/src/data/marginal-intensities-2021.min.js @@ -4,4 +4,9 @@ * @version 1.0.0 */ +/** + * @constant {Object.} data - Average CO2 emissions intensity data for various countries. + * @constant {string} type - Type of data being represented. + * @constant {string} year - Year for which the data was collected. + */ const data = {"AFG":"414","ALB":"0","DZA":"528","ASM":"753","AND":"188","AGO":"1476","AIA":"753","ATG":"753","ARG":"478","ARM":"390","ABW":"753","AUS":"808","AUT":"242","AZE":"534","AZORES (PORTUGAL)":"753","BHS":"753","BHR":"726","BGD":"528","BRB":"749","BLR":"400","BEL":"252","BLZ":"403","BEN":"745","BMU":"753","BTN":"0","BOL":"604","BES":"753","BIH":"1197","BWA":"1486","BRA":"284","VGB":"753","BRN":"681","BGR":"911","BFA":"753","BDI":"414","KHM":"1046","CMR":"659","CAN":"372","CYM":"753","CPV":"753","CAF":"188","TCD":"753","CHANNEL ISLANDS (U.K)":"753","CHL":"657","CHN":"899","COL":"410","COM":"753","COD":"0","COG":"659","COK":"753","CRI":"108","CIV":"466","HRV":"294","CUB":"559","CUW":"876","CYP":"751","CZE":"902","DNK":"362","DJI":"753","DMA":"753","DOM":"601","ECU":"560","EGY":"554","SLV":"547","GNQ":"632","ERI":"915","EST":"1057","SWZ":"0","ETH":"0","FLK":"753","FRO":"753","FJI":"640","FIN":"267","FRA":"158","GUF":"423","PYF":"753","GAB":"946","GMB":"753","GEO":"289","DEU":"650","GHA":"495","GIB":"779","GRC":"507","GRL":"264","GRD":"753","GLP":"753","GUM":"753","GTM":"798","GIN":"753","GNB":"753","GUY":"847","HTI":"1048","HND":"662","HUN":"296","ISL":"0","IND":"951","IDN":"783","IRN":"592","IRQ":"1080","IRL":"380","IMN":"436","ISR":"394","ITA":"414","JAM":"711","JPN":"471","JOR":"529","KAZ":"797","KEN":"574","KIR":"753","PRK":"754","KOR":"555","XKX":"1145","KWT":"675","KGZ":"217","LAO":"1069","LVA":"240","LBN":"794","LSO":"0","LBR":"677","LBY":"668","LIE":"151","LTU":"211","LUX":"220","MDG":"876","MADEIRA (PORTUGAL)":"663","MWI":"489","MYS":"551","MDV":"753","MLI":"1076","MLT":"520","MHL":"753","MTQ":"753","MRT":"753","MUS":"700","MYT":"753","MEX":"531","FSM":"753","MDA":"541","MCO":"158","MNG":"1366","MNE":"899","MSR":"753","MAR":"729","MOZ":"234","MMR":"719","NAM":"355","NRU":"753","NPL":"0","NLD":"326","NCL":"779","NZL":"246","NIC":"675","NER":"772","NGA":"526","NIU":"753","MKD":"851","MNP":"753","NOR":"47","OMN":"479","PAK":"592","PLW":"753","PSE":"719","PAN":"477","PNG":"597","PRY":"0","PER":"473","PHL":"672","POL":"828","PRT":"389","PRI":"596","QAT":"503","REU":"772","ROU":"489","RUS":"476","RWA":"712","SHN":"753","KNA":"753","LCA":"753","MAF":"753","SPM":"753","VCT":"753","WSM":"753","SMR":"414","STP":"753","SAU":"592","SEN":"870","SRB":"1086","SYC":"753","SLE":"489","SGP":"379","SXM":"753","SVK":"332","SVN":"620","SLB":"753","SOM":"753","ZAF":"1070","SSD":"890","ESP":"402","LKA":"731","SDN":"736","SUR":"1029","SWE":"68","CHE":"48","SYR":"713","TWN":"484","TJK":"255","TZA":"531","THA":"450","TLS":"753","TGO":"859","TON":"753","TTO":"559","TUN":"468","TUR":"376","TKM":"927","TCA":"753","TUV":"753","UGA":"279","UKR":"768","ARE":"556","GBR":"380","USA":"416","URY":"174","UZB":"612","VUT":"753","VEN":"711","VNM":"560","VIR":"650","YEM":"807","ZMB":"416","ZWE":"1575","MEMO: EU 27":"409"}; const type = "marginal"; const year = "2021"; export { data, type, year }; export default { data, type, year }; \ No newline at end of file From 21a1a25598ecb19f4a3cd356bad41ea91b77d51c Mon Sep 17 00:00:00 2001 From: fershad <27988517+fershad@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:11:01 +0800 Subject: [PATCH 8/9] add comments to output of generators --- data/functions/generate_average_co2.js | 21 ++++++++++++++++----- data/functions/generate_marginal_co2.js | 21 +++++++++++++-------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/data/functions/generate_average_co2.js b/data/functions/generate_average_co2.js index afb89959..3441a001 100644 --- a/data/functions/generate_average_co2.js +++ b/data/functions/generate_average_co2.js @@ -14,21 +14,18 @@ const fs = require("fs"); const sourceURL = "https://ember-data-api-scg3n.ondigitalocean.app/ember/country_overview_yearly.json?_sort=rowid&_shape=array"; - /** * Object to store the grid intensity results for each country. * @type {Object.} */ const gridIntensityResults = {}; - /** * Object to store general results including additional country information. * @type {Object.} */ const generalResults = {}; - /** * The type of intensity data being processed (average or marginal). * @constant {string} @@ -115,6 +112,17 @@ const type = "average"; }; }); + const jsDocComments = `/** + * @fileoverview Minified average CO2 emissions intensity data for countries. + * @generated Generated by generate_average_co2.js + * @version 1.0.0 +*/ + +/** + * @constant {Object.} data - Average CO2 emissions intensity data for various countries. + * @constant {string} type - Type of data being represented. + */`; + /** * Saves the country code and emissions data for use in the CO2.js library. * @type {void} @@ -134,7 +142,10 @@ const type = "average"; */ fs.writeFileSync( "src/data/average-intensities.min.js", - `const data = ${JSON.stringify(gridIntensityResults)}; const type = "${type}"; export { data, type }; export default { data, type };` + `${jsDocComments} + const data = ${JSON.stringify( + gridIntensityResults + )}; const type = "${type}"; export { data, type }; export default { data, type };` ); /** @@ -145,4 +156,4 @@ const type = "average"; "data/output/average-intensities.json", JSON.stringify(generalResults, null, " ") ); -})(); \ No newline at end of file +})(); diff --git a/data/functions/generate_marginal_co2.js b/data/functions/generate_marginal_co2.js index ebf1f458..a592ad5c 100644 --- a/data/functions/generate_marginal_co2.js +++ b/data/functions/generate_marginal_co2.js @@ -15,14 +15,12 @@ const csv = fs.readFileSync( "data/IFI_Default_Grid_Factors_2021_v3.1_unfccc.csv" ); - /** * Utility function to parse CSV rows. * @type {function} */ const parseCSVRow = require("../utils/parseCSVRow"); - /** * Utility function to get country codes. * @type {function} @@ -47,21 +45,18 @@ const source = "UNFCCC"; */ const year = "2021"; - /** * Converts the CSV data to an array of strings, each representing a row. * @type {string[]} */ const array = csv.toString().split("\n"); - /** * Object to store the converted CSV data. * @type {Object.} */ const csvToJsonResult = {}; - /** * Object to store the grid intensity results for each country. * @type {Object.} @@ -133,6 +128,17 @@ for (let currentArrayString of array.slice(5)) { const json = JSON.stringify(csvToJsonResult); const gridIntensityJson = JSON.stringify(gridIntensityResults); +const jsDocComments = `/** + * @fileoverview Minified marginal CO2 emissions intensity data for countries (2021). + * @generated Generated by generate_marginal_co2.js + * @version 1.0.0 + */ + +/** + * @constant {Object.} data - Average CO2 emissions intensity data for various countries. + * @constant {string} type - Type of data being represented. + * @constant {string} year - Year for which the data was collected. + */`; /** * Saves the country code and emissions data for use in the CO2.js library. @@ -149,17 +155,16 @@ fs.writeFileSync( ` ); - /** * Saves a minified version of the data for easy import into the library. * @type {void} */ fs.writeFileSync( "src/data/marginal-intensities-2021.min.js", - `const data = ${gridIntensityJson}; const type = "${type}"; const year = "${year}"; export { data, type, year }; export default { data, type, year };` + `${jsDocComments} + const data = ${gridIntensityJson}; const type = "${type}"; const year = "${year}"; export { data, type, year }; export default { data, type, year };` ); - /** * Saves the full data set as a JSON file for reference. * @type {void} From e535f1b214e93efb3f6804b543bf3932744824ed Mon Sep 17 00:00:00 2001 From: fershad <27988517+fershad@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:41:14 +0800 Subject: [PATCH 9/9] Squashed commit of the following: commit 668d8e4282115935ab09219a0b3469b5d45fa618 Merge: 33c66d7 21a1a25 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Wed Oct 23 10:15:46 2024 +0200 Add JSDoc comments to data generation scripts and output files commit 21a1a25598ecb19f4a3cd356bad41ea91b77d51c Author: fershad <27988517+fershad@users.noreply.github.com> Date: Wed Oct 23 16:11:01 2024 +0800 add comments to output of generators commit cab18cf530cb559db3c349539b3767b4318ad2d3 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Wed Oct 23 16:05:24 2024 +0800 add comments about constants in source files commit 33c66d7db233efe43dbfccb237f1580bec3e8067 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Mon Oct 14 15:46:00 2024 +0800 add note about commenting commit 5e58971b57206cebb1c8c8985e94461a59b74ac5 Author: Malay Kumar Date: Sat Oct 12 19:01:39 2024 +0530 Add JsDoc comments to marginal-intensities-2021 commit 6b000a02601089a14de697c8bd49c41f1ffe8056 Author: Malay Kumar Date: Sat Oct 12 19:01:19 2024 +0530 Add JsDoc comments to average-intensities commit 29b6755fff02875d7b360ea9eead854c72bee51e Author: Malay Kumar Date: Sat Oct 12 18:59:08 2024 +0530 Add JsDoc comments to generate_marginal_co2 commit eb4e9ff2144be97906390c108e512304344d6ee2 Author: Malay Kumar Date: Sat Oct 12 18:58:53 2024 +0530 Add JsDoc comments to generate_average_co2 commit c238d9694dfb0e9663f1d0351b139ef79f41ce6b Author: fershad <27988517+fershad@users.noreply.github.com> Date: Wed Sep 11 10:30:41 2024 +0800 v0.16.1 commit d5d31560da3fd4dfca9c4c99d3fab1d8f8de7335 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:44:34 2024 +0800 0.16.1 commit 4b109305ad8307e7dbb6f127e6649de169c096a1 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:43:51 2024 +0800 v0.16.1 commit 83392842051ec4eefb63c7b81e0517b93409449b Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:38:33 2024 +0800 update jsr export commit 6c0f991f887754abefffd58a6b541bdcc53bf4bd Merge: 8f9097c ed2d8ab Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:35:05 2024 +0800 Add basic JSR support commit ed2d8ab0419d66c9425c00c8ef278c08c0cb837c Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:20:27 2024 +0800 update average grid intensities commit 8f9097cc6bea15d9d4e90ab2e54fec7b157396e6 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:17:29 2024 +0800 update test constants commit ce1ada57aee36aaa58858e6a5d349aeceeb67ae1 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:11:41 2024 +0800 update version commit 2962ca1dd4aa9ecf7642efac185d638583fd702b Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:06:52 2024 +0800 create hosting module commit 2dd34ecb2505020d95c7657618912f025b7e3638 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Tue Sep 10 15:03:33 2024 +0800 add mod.ts for jsr commit 870e6211ae2ba538889eeaf6b21d14b535ac4fb4 Merge: 7adac52 87d0e62 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Fri Sep 6 14:35:09 2024 +0800 [AUTOMATED] Update average annual grid intensities commit 87d0e62ce5161850e6d02ea328b7d5c3f444a213 Author: fershad Date: Tue Sep 3 10:10:55 2024 +0000 Update average annual grid intensities commit 7adac52a77c886d281286f2a8926c61e6faba4fb Author: fershad <27988517+fershad@users.noreply.github.com> Date: Wed Aug 21 12:55:39 2024 +0800 update test constant commit 344a513b891b6b8024a89eabe1a9237c07c4699a Merge: 4541319 d080a36 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Wed Aug 14 21:36:04 2024 +0200 [AUTOMATED] Update average annual grid intensities commit d080a36544cb218810abab13d33851c28a0266c1 Author: fershad Date: Sat Aug 3 10:09:23 2024 +0000 Update average annual grid intensities commit 454131905c0684f3192dd776be994204040bc59f Merge: 8f95399 c19da59 Author: fershad <27988517+fershad@users.noreply.github.com> Date: Wed Jul 31 22:38:58 2024 +0200 Export classes inside of the individual models commit c19da5942b949a53daf5db89b84d372eed6ce7fc Author: fershad <27988517+fershad@users.noreply.github.com> Date: Thu Jul 11 22:02:57 2024 +0100 export classes inside of the CO2 class --- CONTRIBUTING.md | 20 ++--- data/functions/generate_average_co2.js | 78 ++++++++++++++++--- data/functions/generate_marginal_co2.js | 92 ++++++++++++++++++++--- data/output/average-intensities.js | 72 +++++++++--------- data/output/average-intensities.json | 76 +++++++++---------- dist/iife/index.js | 8 +- jsr.json | 4 +- mod.ts | 1 + package-lock.json | 4 +- package.json | 2 +- src/co2.js | 12 +++ src/constants/test-constants.js | 10 +-- src/data/average-intensities.min.js | 12 ++- src/data/marginal-intensities-2021.min.js | 11 +++ src/hosting.js | 8 +- src/sustainable-web-design-v3.test.js | 2 +- 16 files changed, 289 insertions(+), 123 deletions(-) create mode 100644 mod.ts diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dbc1f697..84b346d5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,17 +9,17 @@ Thank you for considering contributing to CO2.js. Open source is at the heart of **NB** Changes/commits that are not linked to an issue will not be accepted. ### New issues - - Our issue template provides you with a scaffold to follow when raising a new issue. There are three formats to choose from: - 1. *Bugs* - clearly describe the problem you're facing including steps to reproduce it. Please also be sure to include the information about the environment your code was running in (e.g. NodeJS, Deno, Edge Worker, Browser etc). - 1. *New features* - clearly describe the new feature you'd like to see added and provide a reason for why it should be added. eg what will be improved/possible as a result of making your suggested change. - 1. *Request addition of carbon estimation model to CO2.js* - please provide as much information about the model as possible including links to additional documentation and information about how the model is licensed. - +- Our issue template provides you with a scaffold to follow when raising a new issue. There are three formats to choose from: + 1. _Bugs_ - clearly describe the problem you're facing including steps to reproduce it. Please also be sure to include the information about the environment your code was running in (e.g. NodeJS, Deno, Edge Worker, Browser etc). + 1. _New features_ - clearly describe the new feature you'd like to see added and provide a reason for why it should be added. eg what will be improved/possible as a result of making your suggested change. + 1. _Request addition of carbon estimation model to CO2.js_ - please provide as much information about the model as possible including links to additional documentation and information about how the model is licensed. ## Making Changes - Fork the repository on GitHub. - Create a topic branch from where you want to base your work. This branch should usually be based off `main`. +- Remember to add JSDoc comments to any new functions or variables that you are introducing into the codebase. - Make commits of logical units. - Make commit messages clear and understandable. @@ -28,10 +28,10 @@ Thank you for considering contributing to CO2.js. Open source is at the heart of - Push the changes made in your branch to your fork of this repository. - Submit a [pull request](https://github.com/thegreenwebfoundation/co2.js/pulls) to the CO2.js repository in the `thegreenwebfoundation` organization. - When opening a new pull request, you'll see a template. Please follow it. It asks you to state: - - the type of change (choose from a list) - - link to the related issue - - what documentation needs updating as a result (choose from a list) - - what your changes are - be sure to clearly explain the changes you've made, any new files, dependencies, or network requests that have been added, and provide any additional context to help reviewers understand the changes made. + - the type of change (choose from a list) + - link to the related issue + - what documentation needs updating as a result (choose from a list) + - what your changes are - be sure to clearly explain the changes you've made, any new files, dependencies, or network requests that have been added, and provide any additional context to help reviewers understand the changes made. - Your pull request will be reviewed by a maintainer from Green Web Foundation. - After feedback has been given we expect responses within two weeks. After two weeks we may close the pull request if it isn't showing any activity. @@ -43,4 +43,4 @@ Please note that this project is released with a [Contributor Code of Conduct](h ## Thank you -Again, thank you for your contributions. We appreciate your help improving CO2.js, and we look forward to your future contributions! \ No newline at end of file +Again, thank you for your contributions. We appreciate your help improving CO2.js, and we look forward to your future contributions! diff --git a/data/functions/generate_average_co2.js b/data/functions/generate_average_co2.js index a5238fa1..331de4c1 100644 --- a/data/functions/generate_average_co2.js +++ b/data/functions/generate_average_co2.js @@ -1,16 +1,43 @@ +/** + * @fileoverview This script generates average CO2 emissions intensity data for countries using the Ember API. + * It processes the data and saves it in various formats for use in the CO2.js library. + * @author Chris Adams + * @version 1.0.0 + */ + const fs = require("fs"); -// This URL from Ember returns ALL the data for the country_overview_yearly table +/** + * The URL for the Ember API that provides country overview data on a yearly basis. + * @constant {string} + */ const sourceURL = "https://ember-data-api-scg3n.ondigitalocean.app/ember/country_overview_yearly.json?_sort=rowid&_shape=array"; + +/** + * Object to store the grid intensity results for each country. + * @type {Object.} + */ const gridIntensityResults = {}; + +/** + * Object to store general results including additional country information. + * @type {Object.} + */ const generalResults = {}; + +/** + * The type of intensity data being processed (average or marginal). + * @constant {string} + */ const type = "average"; /** - * This function generates the average CO2 emissions data for each country. - * It reads the data from the Ember API and saves it in the data/output folder. - * It also saves the data as a minified file in the src/data folder, so that it can be imported into the library. + * Fetches data from the Ember API, processes it to extract the latest average CO2 emissions + * intensity data for each country, and saves the results in various formats. + * @async + * @function + * @returns {Promise} */ // Use async/await @@ -31,7 +58,10 @@ const type = "average"; } const data = await response.json(); - // Group data by country_code + /** + * Groups the API data by country code. + * @type {Object.} + */ const groupedData = await data.reduce((acc, item) => { const key = item.country_code === "" ? item.country_or_region : item.country_code; @@ -42,7 +72,10 @@ const type = "average"; return acc; }, {}); - // Loop through the grouped data and find the latest year + /** + * Extracts the latest year's data for each country. + * @type {Object.} + */ const latestData = await Object.keys(groupedData).reduce((acc, key) => { // Find the last year in the array with emissions intensity data const latestYear = groupedData[key].reduce((acc, item, index) => { @@ -87,7 +120,20 @@ const type = "average"; fs.mkdirSync("data/output", { recursive: true }); fs.mkdirSync("src/data", { recursive: true }); - // This saves the country code and emissions data only, for use in the CO2.js library + const jsDocComments = `/** + * @fileoverview Minified average CO2 emissions intensity data for countries. + * @generated Generated by generate_average_co2.js + * @version 1.0.0 +*/ +/** + * @constant {Object.} data - Average CO2 emissions intensity data for various countries. + * @constant {string} type - Type of data being represented. + */`; + + /** + * Saves the country code and emissions data for use in the CO2.js library. + * @type {void} + */ fs.writeFileSync( "data/output/average-intensities.js", `const data = ${JSON.stringify(gridIntensityResults, null, " ")}; @@ -96,13 +142,23 @@ const type = "average"; export default { data, type }; ` ); - // Save a minified version to the src folder so that it can be easily imported into the library + + /** + * Saves a minified version of the data for easy import into the library. + * @type {void} + */ fs.writeFileSync( "src/data/average-intensities.min.js", - `const data = ${JSON.stringify(gridIntensityResults)}; const type = "${type}"; export { data, type }; export default { data, type };` + `${jsDocComments} + const data = ${JSON.stringify( + gridIntensityResults + )}; const type = "${type}"; export { data, type }; export default { data, type };` ); - // This saves the full data set as a JSON file for reference. + /** + * Saves the full data set as a JSON file for reference. + * @type {void} + */ fs.writeFileSync( "data/output/average-intensities.json", JSON.stringify(generalResults, null, " ") @@ -110,4 +166,4 @@ const type = "average"; } catch (error) { console.error("Error fetching or processing data:", error); } -})(); \ No newline at end of file +})(); diff --git a/data/functions/generate_marginal_co2.js b/data/functions/generate_marginal_co2.js index c63e368d..a592ad5c 100644 --- a/data/functions/generate_marginal_co2.js +++ b/data/functions/generate_marginal_co2.js @@ -1,17 +1,66 @@ +/** + * @fileoverview This script generates marginal CO2 emissions intensity data for countries using UNFCCC data. + * It processes the data from a CSV file and saves it in various formats for use in the CO2.js library. + * @author Chris Adams + * @version 1.0.0 + */ + const fs = require("fs"); + +/** + * Reads the UNFCCC CSV file containing grid factors data. + * @type {Buffer} + */ const csv = fs.readFileSync( "data/IFI_Default_Grid_Factors_2021_v3.1_unfccc.csv" ); + +/** + * Utility function to parse CSV rows. + * @type {function} + */ const parseCSVRow = require("../utils/parseCSVRow"); + +/** + * Utility function to get country codes. + * @type {function} + */ const getCountryCodes = require("../utils/getCountryCodes"); + +/** + * The type of intensity data being processed (marginal). + * @constant {string} + */ const type = "marginal"; + +/** + * The source of the data (UNFCCC). + * @constant {string} + */ const source = "UNFCCC"; + +/** + * The year of the data. + * @constant {string} + */ const year = "2021"; +/** + * Converts the CSV data to an array of strings, each representing a row. + * @type {string[]} + */ const array = csv.toString().split("\n"); -/* Store the converted result into an array */ +/** + * Object to store the converted CSV data. + * @type {Object.} + */ const csvToJsonResult = {}; + +/** + * Object to store the grid intensity results for each country. + * @type {Object.} + */ const gridIntensityResults = {}; /* Store the CSV column headers into seprate variable */ @@ -79,20 +128,45 @@ for (let currentArrayString of array.slice(5)) { const json = JSON.stringify(csvToJsonResult); const gridIntensityJson = JSON.stringify(gridIntensityResults); -// This saves the country code and emissions data only, for use in the CO2.js library +const jsDocComments = `/** + * @fileoverview Minified marginal CO2 emissions intensity data for countries (2021). + * @generated Generated by generate_marginal_co2.js + * @version 1.0.0 + */ + +/** + * @constant {Object.} data - Average CO2 emissions intensity data for various countries. + * @constant {string} type - Type of data being represented. + * @constant {string} year - Year for which the data was collected. + */`; + +/** + * Saves the country code and emissions data for use in the CO2.js library. + * @type {void} + */ fs.writeFileSync( "data/output/marginal-intensities-2021.js", - `const data = ${gridIntensityJson}; + ` + const data = ${gridIntensityJson}; const type = "${type}"; -const year = "${year}"; -export { data, type, year }; -export default { data, type, year };` + const year = "${year}"; + export { data, type, year }; + export default { data, type, year }; + ` ); -// Save a minified version to the src folder so that it can be easily imported into the library + +/** + * Saves a minified version of the data for easy import into the library. + * @type {void} + */ fs.writeFileSync( "src/data/marginal-intensities-2021.min.js", - `const data = ${gridIntensityJson}; const type = "${type}"; const year = "${year}"; export { data, type, year }; export default { data, type, year };` + `${jsDocComments} + const data = ${gridIntensityJson}; const type = "${type}"; const year = "${year}"; export { data, type, year }; export default { data, type, year };` ); -// This saves the full data set as a JSON file for reference. +/** + * Saves the full data set as a JSON file for reference. + * @type {void} + */ fs.writeFileSync("data/output/marginal-intensities-2021.json", json); diff --git a/data/output/average-intensities.js b/data/output/average-intensities.js index 34781ca5..2907e010 100644 --- a/data/output/average-intensities.js +++ b/data/output/average-intensities.js @@ -1,17 +1,17 @@ const data = { AFG: 132.53, - AFRICA: 544.76, + AFRICA: 547.83, ALB: 24.29, DZA: 634.61, ASM: 611.11, AGO: 174.73, ATG: 611.11, - ARG: 354.1, + ARG: 353.96, ARM: 264.54, ABW: 561.22, - ASEAN: 571.29, - ASIA: 590.05, - AUS: 548.65, + ASEAN: 570.41, + ASIA: 591.13, + AUS: 556.3, AUT: 110.81, AZE: 671.39, BHS: 660.1, @@ -26,7 +26,7 @@ const data = { BOL: 531.69, BIH: 601.29, BWA: 847.91, - BRA: 98.31, + BRA: 96.4, BRN: 893.91, BGR: 335.33, BFA: 467.53, @@ -34,12 +34,12 @@ const data = { CPV: 558.14, KHM: 417.71, CMR: 305.42, - CAN: 171.12, + CAN: 165.15, CYM: 642.86, CAF: 0, TCD: 628.57, CHL: 291.11, - CHN: 582.26, + CHN: 583.61, COL: 259.51, COM: 642.86, COG: 700, @@ -56,15 +56,15 @@ const data = { DMA: 529.41, DOM: 580.78, ECU: 166.91, - EGY: 569.59, + EGY: 574.04, SLV: 224.76, GNQ: 591.84, ERI: 631.58, EST: 416.67, SWZ: 172.41, ETH: 24.64, - EU: 243.94, - EUROPE: 301.99, + EU: 243.93, + EUROPE: 302.29, FLK: 500, FRO: 404.76, FJI: 288.46, @@ -72,12 +72,12 @@ const data = { FRA: 56.02, GUF: 217.82, PYF: 442.86, - G20: 477.15, - G7: 341.04, + G20: 477.89, + G7: 341.44, GAB: 491.6, GMB: 666.67, GEO: 167.59, - DEU: 381.16, + DEU: 381.41, GHA: 484, GRC: 336.57, GRL: 178.57, @@ -93,24 +93,24 @@ const data = { HKG: 699.5, HUN: 204.19, ISL: 27.68, - IND: 713.44, + IND: 713.01, IDN: 682.43, - IRN: 655.13, + IRN: 641.73, IRQ: 688.81, IRL: 282.98, ISR: 582.93, ITA: 330.72, JAM: 555.56, - JPN: 485.39, + JPN: 493.59, JOR: 540.92, - KAZ: 821.39, + KAZ: 821.9, KEN: 71.2, KIR: 666.67, XKX: 894.65, KWT: 649.16, KGZ: 147.29, LAO: 265.51, - "LATIN AMERICA AND CARIBBEAN": 260.09, + "LATIN AMERICA AND CARIBBEAN": 256.03, LVA: 123.99, LBN: 599.01, LSO: 20, @@ -121,15 +121,15 @@ const data = { MAC: 448.98, MDG: 436.44, MWI: 66.67, - MYS: 605.32, + MYS: 607.88, MDV: 611.77, MLI: 408, MLT: 459.14, MTQ: 523.18, MRT: 464.71, MUS: 632.48, - MEX: 507.25, - "MIDDLE EAST": 657.52, + MEX: 492.34, + "MIDDLE EAST": 643.04, MDA: 643.46, MNG: 775.31, MNE: 418.09, @@ -146,12 +146,12 @@ const data = { NIC: 265.12, NER: 670.89, NGA: 523.25, - "NORTH AMERICA": 343.66, + "NORTH AMERICA": 342.95, PRK: 389.59, MKD: 556.19, NOR: 30.05, - OCEANIA: 489.59, - OECD: 341.19, + OCEANIA: 495.74, + OECD: 341.27, OMN: 564.55, PAK: 440.61, PSE: 516.13, @@ -165,7 +165,7 @@ const data = { QAT: 602.5, REU: 572.82, ROU: 240.58, - RUS: 441.04, + RUS: 445.02, RWA: 316.33, KNA: 636.36, LCA: 666.67, @@ -173,7 +173,7 @@ const data = { VCT: 529.41, WSM: 473.68, STP: 642.86, - SAU: 706.79, + SAU: 696.31, SEN: 511.6, SRB: 647.52, SYC: 564.52, @@ -183,8 +183,8 @@ const data = { SVN: 231.28, SLB: 700, SOM: 578.95, - ZAF: 707.69, - KOR: 430.57, + ZAF: 709.69, + KOR: 432.11, SSD: 629.03, ESP: 174.05, LKA: 509.78, @@ -193,10 +193,10 @@ const data = { SWE: 40.7, CHE: 34.68, SYR: 701.66, - TWN: 650.73, + TWN: 644.36, TJK: 116.86, TZA: 339.25, - THA: 549.61, + THA: 549.85, PHL: 610.74, TGO: 443.18, TON: 625, @@ -206,18 +206,18 @@ const data = { TKM: 1306.03, TCA: 653.85, UGA: 44.53, - UKR: 259.69, - ARE: 561.14, - GBR: 237.59, + UKR: 256.21, + ARE: 492.7, + GBR: 228.25, USA: 369.47, URY: 128.79, UZB: 1167.6, VUT: 571.43, VEN: 185.8, - VNM: 475.45, + VNM: 472.47, VGB: 647.06, VIR: 632.35, - WORLD: 481.46, + WORLD: 481.65, YEM: 566.1, ZMB: 111.97, ZWE: 297.87, diff --git a/data/output/average-intensities.json b/data/output/average-intensities.json index e8082152..cbeabd11 100644 --- a/data/output/average-intensities.json +++ b/data/output/average-intensities.json @@ -9,7 +9,7 @@ "country_code": "", "country_or_region": "Africa", "year": 2023, - "emissions_intensity_gco2_per_kwh": 544.76 + "emissions_intensity_gco2_per_kwh": 547.83 }, "ALB": { "country_code": "ALB", @@ -45,7 +45,7 @@ "country_code": "ARG", "country_or_region": "Argentina", "year": 2023, - "emissions_intensity_gco2_per_kwh": 354.1 + "emissions_intensity_gco2_per_kwh": 353.96 }, "ARM": { "country_code": "ARM", @@ -63,19 +63,19 @@ "country_code": "", "country_or_region": "ASEAN", "year": 2023, - "emissions_intensity_gco2_per_kwh": 571.29 + "emissions_intensity_gco2_per_kwh": 570.41 }, "Asia": { "country_code": "", "country_or_region": "Asia", "year": 2023, - "emissions_intensity_gco2_per_kwh": 590.05 + "emissions_intensity_gco2_per_kwh": 591.13 }, "AUS": { "country_code": "AUS", "country_or_region": "Australia", "year": 2023, - "emissions_intensity_gco2_per_kwh": 548.65 + "emissions_intensity_gco2_per_kwh": 556.3 }, "AUT": { "country_code": "AUT", @@ -165,7 +165,7 @@ "country_code": "BRA", "country_or_region": "Brazil", "year": 2023, - "emissions_intensity_gco2_per_kwh": 98.31 + "emissions_intensity_gco2_per_kwh": 96.4 }, "BRN": { "country_code": "BRN", @@ -213,7 +213,7 @@ "country_code": "CAN", "country_or_region": "Canada", "year": 2023, - "emissions_intensity_gco2_per_kwh": 171.12 + "emissions_intensity_gco2_per_kwh": 165.15 }, "CYM": { "country_code": "CYM", @@ -243,7 +243,7 @@ "country_code": "CHN", "country_or_region": "China", "year": 2023, - "emissions_intensity_gco2_per_kwh": 582.26 + "emissions_intensity_gco2_per_kwh": 583.61 }, "COL": { "country_code": "COL", @@ -345,7 +345,7 @@ "country_code": "EGY", "country_or_region": "Egypt", "year": 2023, - "emissions_intensity_gco2_per_kwh": 569.59 + "emissions_intensity_gco2_per_kwh": 574.04 }, "SLV": { "country_code": "SLV", @@ -387,13 +387,13 @@ "country_code": "", "country_or_region": "EU", "year": 2023, - "emissions_intensity_gco2_per_kwh": 243.94 + "emissions_intensity_gco2_per_kwh": 243.93 }, "Europe": { "country_code": "", "country_or_region": "Europe", "year": 2023, - "emissions_intensity_gco2_per_kwh": 301.99 + "emissions_intensity_gco2_per_kwh": 302.29 }, "FLK": { "country_code": "FLK", @@ -441,13 +441,13 @@ "country_code": "", "country_or_region": "G20", "year": 2023, - "emissions_intensity_gco2_per_kwh": 477.15 + "emissions_intensity_gco2_per_kwh": 477.89 }, "G7": { "country_code": "", "country_or_region": "G7", "year": 2023, - "emissions_intensity_gco2_per_kwh": 341.04 + "emissions_intensity_gco2_per_kwh": 341.44 }, "GAB": { "country_code": "GAB", @@ -471,7 +471,7 @@ "country_code": "DEU", "country_or_region": "Germany", "year": 2023, - "emissions_intensity_gco2_per_kwh": 381.16 + "emissions_intensity_gco2_per_kwh": 381.41 }, "GHA": { "country_code": "GHA", @@ -567,7 +567,7 @@ "country_code": "IND", "country_or_region": "India", "year": 2023, - "emissions_intensity_gco2_per_kwh": 713.44 + "emissions_intensity_gco2_per_kwh": 713.01 }, "IDN": { "country_code": "IDN", @@ -579,7 +579,7 @@ "country_code": "IRN", "country_or_region": "Iran", "year": 2023, - "emissions_intensity_gco2_per_kwh": 655.13 + "emissions_intensity_gco2_per_kwh": 641.73 }, "IRQ": { "country_code": "IRQ", @@ -615,7 +615,7 @@ "country_code": "JPN", "country_or_region": "Japan", "year": 2023, - "emissions_intensity_gco2_per_kwh": 485.39 + "emissions_intensity_gco2_per_kwh": 493.59 }, "JOR": { "country_code": "JOR", @@ -627,7 +627,7 @@ "country_code": "KAZ", "country_or_region": "Kazakhstan", "year": 2023, - "emissions_intensity_gco2_per_kwh": 821.39 + "emissions_intensity_gco2_per_kwh": 821.9 }, "KEN": { "country_code": "KEN", @@ -669,7 +669,7 @@ "country_code": "", "country_or_region": "Latin America and Caribbean", "year": 2023, - "emissions_intensity_gco2_per_kwh": 260.09 + "emissions_intensity_gco2_per_kwh": 256.03 }, "LVA": { "country_code": "LVA", @@ -735,7 +735,7 @@ "country_code": "MYS", "country_or_region": "Malaysia", "year": 2023, - "emissions_intensity_gco2_per_kwh": 605.32 + "emissions_intensity_gco2_per_kwh": 607.88 }, "MDV": { "country_code": "MDV", @@ -777,13 +777,13 @@ "country_code": "MEX", "country_or_region": "Mexico", "year": 2023, - "emissions_intensity_gco2_per_kwh": 507.25 + "emissions_intensity_gco2_per_kwh": 492.34 }, "Middle East": { "country_code": "", "country_or_region": "Middle East", "year": 2023, - "emissions_intensity_gco2_per_kwh": 657.52 + "emissions_intensity_gco2_per_kwh": 643.04 }, "MDA": { "country_code": "MDA", @@ -885,7 +885,7 @@ "country_code": "", "country_or_region": "North America", "year": 2023, - "emissions_intensity_gco2_per_kwh": 343.66 + "emissions_intensity_gco2_per_kwh": 342.95 }, "PRK": { "country_code": "PRK", @@ -909,13 +909,13 @@ "country_code": "", "country_or_region": "Oceania", "year": 2023, - "emissions_intensity_gco2_per_kwh": 489.59 + "emissions_intensity_gco2_per_kwh": 495.74 }, "OECD": { "country_code": "", "country_or_region": "OECD", "year": 2023, - "emissions_intensity_gco2_per_kwh": 341.19 + "emissions_intensity_gco2_per_kwh": 341.27 }, "OMN": { "country_code": "OMN", @@ -999,7 +999,7 @@ "country_code": "RUS", "country_or_region": "Russia", "year": 2023, - "emissions_intensity_gco2_per_kwh": 441.04 + "emissions_intensity_gco2_per_kwh": 445.02 }, "RWA": { "country_code": "RWA", @@ -1046,8 +1046,8 @@ "SAU": { "country_code": "SAU", "country_or_region": "Saudi Arabia", - "year": 2022, - "emissions_intensity_gco2_per_kwh": 706.79 + "year": 2023, + "emissions_intensity_gco2_per_kwh": 696.31 }, "SEN": { "country_code": "SEN", @@ -1107,13 +1107,13 @@ "country_code": "ZAF", "country_or_region": "South Africa", "year": 2023, - "emissions_intensity_gco2_per_kwh": 707.69 + "emissions_intensity_gco2_per_kwh": 709.69 }, "KOR": { "country_code": "KOR", "country_or_region": "South Korea", "year": 2023, - "emissions_intensity_gco2_per_kwh": 430.57 + "emissions_intensity_gco2_per_kwh": 432.11 }, "SSD": { "country_code": "SSD", @@ -1167,7 +1167,7 @@ "country_code": "TWN", "country_or_region": "Taiwan", "year": 2023, - "emissions_intensity_gco2_per_kwh": 650.73 + "emissions_intensity_gco2_per_kwh": 644.36 }, "TJK": { "country_code": "TJK", @@ -1185,7 +1185,7 @@ "country_code": "THA", "country_or_region": "Thailand", "year": 2023, - "emissions_intensity_gco2_per_kwh": 549.61 + "emissions_intensity_gco2_per_kwh": 549.85 }, "PHL": { "country_code": "PHL", @@ -1245,19 +1245,19 @@ "country_code": "UKR", "country_or_region": "Ukraine", "year": 2022, - "emissions_intensity_gco2_per_kwh": 259.69 + "emissions_intensity_gco2_per_kwh": 256.21 }, "ARE": { "country_code": "ARE", "country_or_region": "United Arab Emirates", - "year": 2022, - "emissions_intensity_gco2_per_kwh": 561.14 + "year": 2023, + "emissions_intensity_gco2_per_kwh": 492.7 }, "GBR": { "country_code": "GBR", "country_or_region": "United Kingdom", "year": 2023, - "emissions_intensity_gco2_per_kwh": 237.59 + "emissions_intensity_gco2_per_kwh": 228.25 }, "USA": { "country_code": "USA", @@ -1293,7 +1293,7 @@ "country_code": "VNM", "country_or_region": "Viet Nam", "year": 2023, - "emissions_intensity_gco2_per_kwh": 475.45 + "emissions_intensity_gco2_per_kwh": 472.47 }, "VGB": { "country_code": "VGB", @@ -1311,7 +1311,7 @@ "country_code": "", "country_or_region": "World", "year": 2023, - "emissions_intensity_gco2_per_kwh": 481.46 + "emissions_intensity_gco2_per_kwh": 481.65 }, "YEM": { "country_code": "YEM", diff --git a/dist/iife/index.js b/dist/iife/index.js index a62b72d6..6c4addb1 100644 --- a/dist/iife/index.js +++ b/dist/iife/index.js @@ -1,4 +1,4 @@ -var co2=(()=>{var ae=Object.create;var P=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var oe=Object.getOwnPropertyNames;var se=Object.getPrototypeOf,ce=Object.prototype.hasOwnProperty;var le=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),Ee=(t,e)=>{for(var r in e)P(t,r,{get:e[r],enumerable:!0})},j=(t,e,r,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of oe(e))!ce.call(t,n)&&n!==r&&P(t,n,{get:()=>e[n],enumerable:!(a=ie(e,n))||a.enumerable});return t};var ue=(t,e,r)=>(r=t!=null?ae(se(t)):{},j(e||!t||!t.__esModule?P(r,"default",{value:t,enumerable:!0}):r,t)),de=t=>j(P({},"__esModule",{value:!0}),t);var te=le((ct,ee)=>{"use strict";async function me(t,e){return typeof t=="string"?Oe(t,e):Pe(t,e)}function Oe(t,e){return e.indexOf(t)>-1}function ye(t){return Object.entries(t).filter(([a,n])=>n.green).map(([a,n])=>n.url)}function Pe(t,e){let r=[];for(let a of t)e.indexOf(a)>-1&&r.push(a);return r}function Ge(t,e){return typeof t=="string"?z(t,e):Se(t,e)}function z(t,e){return e.indexOf(t)>-1?t:{url:t,green:!1}}function Se(t,e){let r={};for(let a of t)r[a]=z(a,e);return r}ee.exports={check:me,greenDomainsFromResults:ye,find:Ge}});var we={};Ee(we,{averageIntensity:()=>R,co2:()=>U,default:()=>Me,hosting:()=>K,marginalIntensity:()=>k});var B=4883333333333333e-25;var D=class{constructor(e){this.allowRatings=!1,this.options=e,this.KWH_PER_BYTE_FOR_NETWORK=B}perByte(e,r){if(e<1)return 0;if(r){let n=e*72e-12*0,i=e*B*475;return n+i}let a=72e-12+B;return e*a*519}};var x=D;var N={GIGABYTE:1e9};var Re={AFG:132.53,AFRICA:545.15,ALB:24.29,DZA:634.61,ASM:611.11,AGO:174.73,ATG:611.11,ARG:354.1,ARM:264.54,ABW:561.22,ASEAN:569.86,ASIA:589.78,AUS:548.69,AUT:110.81,AZE:671.39,BHS:660.1,BHR:904.62,BGD:691.41,BRB:605.51,BLR:441.74,BEL:138.11,BLZ:225.81,BEN:584.07,BTN:23.33,BOL:531.69,BIH:601.29,BWA:847.91,BRA:98.32,BRN:893.91,BGR:335.33,BFA:467.53,BDI:250,CPV:558.14,KHM:417.71,CMR:305.42,CAN:171.12,CYM:642.86,CAF:0,TCD:628.57,CHL:291.11,CHN:582.29,COL:259.51,COM:642.86,COG:700,COD:24.46,COK:250,CRI:53.38,CIV:393.89,HRV:204.96,CUB:637.61,CYP:534.32,CZE:449.72,DNK:151.65,DJI:692.31,DMA:529.41,DOM:580.78,ECU:166.91,EGY:570.31,SLV:224.76,GNQ:591.84,ERI:631.58,EST:416.67,SWZ:172.41,ETH:24.64,EU:243.83,EUROPE:300.24,FLK:500,FRO:404.76,FJI:288.46,FIN:79.16,FRA:56.02,GUF:217.82,PYF:442.86,G20:477.08,G7:341.01,GAB:491.6,GMB:666.67,GEO:167.59,DEU:380.95,GHA:484,GRC:336.57,GRL:178.57,GRD:640,GLP:500,GUM:622.86,GTM:328.27,GIN:236.84,GNB:625,GUY:640.35,HTI:567.31,HND:282.27,HKG:699.5,HUN:204.19,ISL:27.68,IND:713.44,IDN:675.93,IRN:655.13,IRQ:688.81,IRL:290.81,ISR:582.93,ITA:330.72,JAM:555.56,JPN:485.39,JOR:540.92,KAZ:821.39,KEN:70.49,KIR:666.67,XKX:894.65,KWT:649.16,KGZ:147.29,LAO:265.51,"LATIN AMERICA AND CARIBBEAN":258.18,LVA:123.2,LBN:599.01,LSO:20,LBR:227.85,LBY:818.69,LTU:160.07,LUX:105.26,MAC:448.98,MDG:436.44,MWI:66.67,MYS:605.32,MDV:611.77,MLI:408,MLT:459.14,MTQ:523.18,MRT:464.71,MUS:632.48,MEX:507.25,"MIDDLE EAST":657.56,MDA:643.46,MNG:775.31,MNE:418.09,MSR:1e3,MAR:630.01,MOZ:135.65,MMR:398.9,NAM:59.26,NRU:750,NPL:24.44,NLD:267.62,NCL:660.58,NZL:112.76,NIC:265.12,NER:670.89,NGA:523.25,"NORTH AMERICA":343.66,PRK:389.59,MKD:556.19,NOR:30.08,OCEANIA:489.62,OECD:341.16,OMN:564.64,PAK:440.61,PSE:516.13,PAN:161.68,PNG:507.25,PRY:23.76,PER:266.48,POL:661.93,PRT:165.55,PRI:676.19,QAT:602.5,REU:572.82,ROU:240.58,RUS:441.04,RWA:316.33,KNA:636.36,LCA:666.67,SPM:600,VCT:529.41,WSM:473.68,STP:642.86,SAU:706.79,SEN:511.6,SRB:647.52,SYC:564.52,SLE:50,SGP:470.78,SVK:116.77,SVN:231.28,SLB:700,SOM:578.95,ZAF:707.69,KOR:430.57,SSD:629.03,ESP:174.05,LKA:509.78,SDN:263.16,SUR:349.28,SWE:40.7,CHE:34.68,SYR:701.66,TWN:642.38,TJK:116.86,TZA:339.25,THA:549.61,PHL:610.74,TGO:443.18,TON:625,TTO:681.53,TUN:563.96,TUR:464.59,TKM:1306.03,TCA:653.85,UGA:44.53,UKR:259.69,ARE:561.14,GBR:237.59,USA:369.47,URY:128.79,UZB:1167.6,VUT:571.43,VEN:185.8,VNM:475.45,VGB:647.06,VIR:632.35,WORLD:480.79,YEM:566.1,ZMB:111.97,ZWE:297.87},ge="average";var R={data:Re,type:ge};var Z=.81,p=.52,b=.14,L=.15,V=.19,g=R.data.WORLD,$=50,_=.75,C=.25,A=.02,G={OPERATIONAL_KWH_PER_GB_DATACENTER:.055,OPERATIONAL_KWH_PER_GB_NETWORK:.059,OPERATIONAL_KWH_PER_GB_DEVICE:.08,EMBODIED_KWH_PER_GB_DATACENTER:.012,EMBODIED_KWH_PER_GB_NETWORK:.013,EMBODIED_KWH_PER_GB_DEVICE:.081,GLOBAL_GRID_INTENSITY:494},J={FIFTH_PERCENTILE:.095,TENTH_PERCENTILE:.186,TWENTIETH_PERCENTILE:.341,THIRTIETH_PERCENTILE:.493,FORTIETH_PERCENTILE:.656,FIFTIETH_PERCENTILE:.846},T={FIFTH_PERCENTILE:.04,TENTH_PERCENTILE:.079,TWENTIETH_PERCENTILE:.145,THIRTIETH_PERCENTILE:.209,FORTIETH_PERCENTILE:.278,FIFTIETH_PERCENTILE:.359};var fe=G.GLOBAL_GRID_INTENSITY,O=t=>parseFloat(t.toFixed(2)),m=(t,e)=>t<=e;function v(t={},e=3,r=!1){let a=e===4?fe:g;if(typeof t!="object")throw new Error("Options must be an object");let n={};if(t?.gridIntensity){n.gridIntensity={};let{device:i,dataCenter:o,network:s}=t.gridIntensity;(i||i===0)&&(typeof i=="object"?(R.data[i.country?.toUpperCase()]||(console.warn(`"${i.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code. +var co2=(()=>{var ae=Object.create;var P=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var oe=Object.getOwnPropertyNames;var se=Object.getPrototypeOf,ce=Object.prototype.hasOwnProperty;var le=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),Ee=(t,e)=>{for(var r in e)P(t,r,{get:e[r],enumerable:!0})},Z=(t,e,r,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of oe(e))!ce.call(t,n)&&n!==r&&P(t,n,{get:()=>e[n],enumerable:!(a=ie(e,n))||a.enumerable});return t};var ue=(t,e,r)=>(r=t!=null?ae(se(t)):{},Z(e||!t||!t.__esModule?P(r,"default",{value:t,enumerable:!0}):r,t)),de=t=>Z(P({},"__esModule",{value:!0}),t);var te=le((ct,ee)=>{"use strict";async function me(t,e){return typeof t=="string"?Oe(t,e):Pe(t,e)}function Oe(t,e){return e.indexOf(t)>-1}function ye(t){return Object.entries(t).filter(([a,n])=>n.green).map(([a,n])=>n.url)}function Pe(t,e){let r=[];for(let a of t)e.indexOf(a)>-1&&r.push(a);return r}function Ge(t,e){return typeof t=="string"?z(t,e):Se(t,e)}function z(t,e){return e.indexOf(t)>-1?t:{url:t,green:!1}}function Se(t,e){let r={};for(let a of t)r[a]=z(a,e);return r}ee.exports={check:me,greenDomainsFromResults:ye,find:Ge}});var Me={};Ee(Me,{averageIntensity:()=>R,co2:()=>k,default:()=>we,hosting:()=>Y,marginalIntensity:()=>j});var D=4883333333333333e-25;var b=class{constructor(e){this.allowRatings=!1,this.options=e,this.KWH_PER_BYTE_FOR_NETWORK=D}perByte(e,r){if(e<1)return 0;if(r){let n=e*72e-12*0,i=e*D*475;return n+i}let a=72e-12+D;return e*a*519}};var p=b;var N={GIGABYTE:1e9};var Re={AFG:132.53,AFRICA:547.83,ALB:24.29,DZA:634.61,ASM:611.11,AGO:174.73,ATG:611.11,ARG:353.96,ARM:264.54,ABW:561.22,ASEAN:570.41,ASIA:591.13,AUS:556.3,AUT:110.81,AZE:671.39,BHS:660.1,BHR:904.62,BGD:691.41,BRB:605.51,BLR:441.74,BEL:138.11,BLZ:225.81,BEN:584.07,BTN:23.33,BOL:531.69,BIH:601.29,BWA:847.91,BRA:96.4,BRN:893.91,BGR:335.33,BFA:467.53,BDI:250,CPV:558.14,KHM:417.71,CMR:305.42,CAN:165.15,CYM:642.86,CAF:0,TCD:628.57,CHL:291.11,CHN:583.61,COL:259.51,COM:642.86,COG:700,COD:24.46,COK:250,CRI:53.38,CIV:393.89,HRV:204.96,CUB:637.61,CYP:534.32,CZE:449.72,DNK:151.65,DJI:692.31,DMA:529.41,DOM:580.78,ECU:166.91,EGY:574.04,SLV:224.76,GNQ:591.84,ERI:631.58,EST:416.67,SWZ:172.41,ETH:24.64,EU:243.93,EUROPE:302.29,FLK:500,FRO:404.76,FJI:288.46,FIN:79.16,FRA:56.02,GUF:217.82,PYF:442.86,G20:477.89,G7:341.44,GAB:491.6,GMB:666.67,GEO:167.59,DEU:381.41,GHA:484,GRC:336.57,GRL:178.57,GRD:640,GLP:500,GUM:622.86,GTM:328.27,GIN:236.84,GNB:625,GUY:640.35,HTI:567.31,HND:282.27,HKG:699.5,HUN:204.19,ISL:27.68,IND:713.01,IDN:682.43,IRN:641.73,IRQ:688.81,IRL:282.98,ISR:582.93,ITA:330.72,JAM:555.56,JPN:493.59,JOR:540.92,KAZ:821.9,KEN:71.2,KIR:666.67,XKX:894.65,KWT:649.16,KGZ:147.29,LAO:265.51,"LATIN AMERICA AND CARIBBEAN":256.03,LVA:123.99,LBN:599.01,LSO:20,LBR:227.85,LBY:818.69,LTU:160.07,LUX:105.26,MAC:448.98,MDG:436.44,MWI:66.67,MYS:607.88,MDV:611.77,MLI:408,MLT:459.14,MTQ:523.18,MRT:464.71,MUS:632.48,MEX:492.34,"MIDDLE EAST":643.04,MDA:643.46,MNG:775.31,MNE:418.09,MSR:1e3,MAR:624.45,MOZ:135.65,MMR:436.92,NAM:59.26,NRU:750,NPL:24.44,NLD:268.48,NCL:660.58,NZL:112.76,NIC:265.12,NER:670.89,NGA:523.25,"NORTH AMERICA":342.95,PRK:389.59,MKD:556.19,NOR:30.05,OCEANIA:495.74,OECD:341.27,OMN:564.55,PAK:440.61,PSE:516.13,PAN:161.68,PNG:507.25,PRY:24.31,PER:266.48,POL:661.93,PRT:165.55,PRI:677.96,QAT:602.5,REU:572.82,ROU:240.58,RUS:445.02,RWA:316.33,KNA:636.36,LCA:666.67,SPM:600,VCT:529.41,WSM:473.68,STP:642.86,SAU:696.31,SEN:511.6,SRB:647.52,SYC:564.52,SLE:50,SGP:470.78,SVK:116.77,SVN:231.28,SLB:700,SOM:578.95,ZAF:709.69,KOR:432.11,SSD:629.03,ESP:174.05,LKA:509.78,SDN:263.16,SUR:349.28,SWE:40.7,CHE:34.68,SYR:701.66,TWN:644.36,TJK:116.86,TZA:339.25,THA:549.85,PHL:610.74,TGO:443.18,TON:625,TTO:681.53,TUN:563.96,TUR:464.59,TKM:1306.03,TCA:653.85,UGA:44.53,UKR:256.21,ARE:492.7,GBR:228.25,USA:369.47,URY:128.79,UZB:1167.6,VUT:571.43,VEN:185.8,VNM:472.47,VGB:647.06,VIR:632.35,WORLD:481.65,YEM:566.1,ZMB:111.97,ZWE:297.87},ge="average";var R={data:Re,type:ge};var $=.81,V=.52,L=.14,v=.15,w=.19,g=R.data.WORLD,J=50,_=.75,C=.25,A=.02,G={OPERATIONAL_KWH_PER_GB_DATACENTER:.055,OPERATIONAL_KWH_PER_GB_NETWORK:.059,OPERATIONAL_KWH_PER_GB_DEVICE:.08,EMBODIED_KWH_PER_GB_DATACENTER:.012,EMBODIED_KWH_PER_GB_NETWORK:.013,EMBODIED_KWH_PER_GB_DEVICE:.081,GLOBAL_GRID_INTENSITY:494},X={FIFTH_PERCENTILE:.095,TENTH_PERCENTILE:.186,TWENTIETH_PERCENTILE:.341,THIRTIETH_PERCENTILE:.493,FORTIETH_PERCENTILE:.656,FIFTIETH_PERCENTILE:.846},T={FIFTH_PERCENTILE:.04,TENTH_PERCENTILE:.079,TWENTIETH_PERCENTILE:.145,THIRTIETH_PERCENTILE:.209,FORTIETH_PERCENTILE:.278,FIFTIETH_PERCENTILE:.359};var fe=G.GLOBAL_GRID_INTENSITY,O=t=>parseFloat(t.toFixed(2)),m=(t,e)=>t<=e;function M(t={},e=3,r=!1){let a=e===4?fe:g;if(typeof t!="object")throw new Error("Options must be an object");let n={};if(t?.gridIntensity){n.gridIntensity={};let{device:i,dataCenter:o,network:s}=t.gridIntensity;(i||i===0)&&(typeof i=="object"?(R.data[i.country?.toUpperCase()]||(console.warn(`"${i.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code. See https://developers.thegreenwebfoundation.org/co2js/data/ for more information. Falling back to global average grid intensity.`),n.gridIntensity.device={value:a}),n.gridIntensity.device={country:i.country,value:parseFloat(R.data[i.country?.toUpperCase()])}):typeof i=="number"?n.gridIntensity.device={value:i}:(n.gridIntensity.device={value:a},console.warn(`The device grid intensity must be a number or an object. You passed in a ${typeof i}. Falling back to global average grid intensity.`))),(o||o===0)&&(typeof o=="object"?(R.data[o.country?.toUpperCase()]||(console.warn(`"${o.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code. @@ -18,10 +18,10 @@ Falling back to default value.`)):(n.returnVisitPercentage=e===3?C:0,console.war Falling back to default value.`)):(n.returnVisitPercentage=e===3?C:0,console.warn(`The returnVisitPercentage option must be a number. You passed in a ${typeof t.returnVisitPercentage}. Falling back to default value.`)),t?.greenHostingFactor||t.greenHostingFactor===0&&e===4?typeof t.greenHostingFactor=="number"?t.greenHostingFactor>=0&&t.greenHostingFactor<=1?n.greenHostingFactor=t.greenHostingFactor:(n.greenHostingFactor=0,console.warn(`The returnVisitPercentage option must be a number between 0 and 1. You passed in ${t.returnVisitPercentage}. Falling back to default value.`)):(n.greenHostingFactor=0,console.warn(`The returnVisitPercentage option must be a number. You passed in a ${typeof t.returnVisitPercentage}. -Falling back to default value.`)):e===4&&(n.greenHostingFactor=0),r&&(n.greenHostingFactor=1),n}function M(t=""){return{"User-Agent":`co2js/0.16.0 ${t}`}}function S(t,e){let{FIFTH_PERCENTILE:r,TENTH_PERCENTILE:a,TWENTIETH_PERCENTILE:n,THIRTIETH_PERCENTILE:i,FORTIETH_PERCENTILE:o,FIFTIETH_PERCENTILE:s}=J;return e===4&&(r=T.FIFTH_PERCENTILE,a=T.TENTH_PERCENTILE,n=T.TWENTIETH_PERCENTILE,i=T.THIRTIETH_PERCENTILE,o=T.FORTIETH_PERCENTILE,s=T.FIFTIETH_PERCENTILE),m(t,r)?"A+":m(t,a)?"A":m(t,n)?"B":m(t,i)?"C":m(t,o)?"D":m(t,s)?"E":"F"}var w=class{constructor(e){this.allowRatings=!0,this.options=e,this.version=3}energyPerByteByComponent(e){let a=e/N.GIGABYTE*Z;return{consumerDeviceEnergy:a*p,networkEnergy:a*b,productionEnergy:a*V,dataCenterEnergy:a*L}}co2byComponent(e,r=g,a={}){let n=g,i=g,o=g,s=g;if(a?.gridIntensity){let{device:l,network:c,dataCenter:E}=a.gridIntensity;(l?.value||l?.value===0)&&(n=l.value),(c?.value||c?.value===0)&&(i=c.value),(E?.value||E?.value===0)&&(o=E.value)}r===!0&&(o=$);let u={};for(let[l,c]of Object.entries(e))l.startsWith("dataCenterEnergy")?u[l.replace("Energy","CO2")]=c*o:l.startsWith("consumerDeviceEnergy")?u[l.replace("Energy","CO2")]=c*n:l.startsWith("networkEnergy")?u[l.replace("Energy","CO2")]=c*i:u[l.replace("Energy","CO2")]=c*s;return u}perByte(e,r=!1,a=!1,n=!1,i={}){e<1&&(e=0);let o=this.energyPerByteByComponent(e,i);if(typeof r!="boolean")throw new Error(`perByte expects a boolean for the carbon intensity value. Received: ${r}`);let s=this.co2byComponent(o,r,i),l=Object.values(s).reduce((E,d)=>E+d),c=null;return n&&(c=this.ratingScale(l)),a?n?{...s,total:l,rating:c}:{...s,total:l}:n?{total:l,rating:c}:l}perVisit(e,r=!1,a=!1,n=!1,i={}){let o=this.energyPerVisitByComponent(e,i);if(typeof r!="boolean")throw new Error(`perVisit expects a boolean for the carbon intensity value. Received: ${r}`);let s=this.co2byComponent(o,r,i),l=Object.values(s).reduce((E,d)=>E+d),c=null;return n&&(c=this.ratingScale(l)),a?n?{...s,total:l,rating:c}:{...s,total:l}:n?{total:l,rating:c}:l}energyPerByte(e){let r=this.energyPerByteByComponent(e);return Object.values(r).reduce((n,i)=>n+i)}energyPerVisitByComponent(e,r={},a=_,n=C,i=A){(r.dataReloadRatio||r.dataReloadRatio===0)&&(i=r.dataReloadRatio),(r.firstVisitPercentage||r.firstVisitPercentage===0)&&(a=r.firstVisitPercentage),(r.returnVisitPercentage||r.returnVisitPercentage===0)&&(n=r.returnVisitPercentage);let o=this.energyPerByteByComponent(e),s={},u=Object.values(o);for(let[l,c]of Object.entries(o))s[`${l} - first`]=c*a,s[`${l} - subsequent`]=c*n*i;return s}energyPerVisit(e){let r=0,a=0,n=Object.entries(this.energyPerVisitByComponent(e));for(let[i,o]of n)i.indexOf("first")>0&&(r+=o);for(let[i,o]of n)i.indexOf("subsequent")>0&&(a+=o);return r+a}emissionsPerVisitInGrams(e,r=g){return O(e*r)}annualEnergyInKwh(e,r=1e3){return e*r*12}annualEmissionsInGrams(e,r=1e3){return e*r*12}annualSegmentEnergy(e){return{consumerDeviceEnergy:O(e*p),networkEnergy:O(e*b),dataCenterEnergy:O(e*L),productionEnergy:O(e*V)}}ratingScale(e){return S(e,this.version)}};var H=w;var{OPERATIONAL_KWH_PER_GB_DATACENTER:Te,OPERATIONAL_KWH_PER_GB_NETWORK:Ie,OPERATIONAL_KWH_PER_GB_DEVICE:Ne,EMBODIED_KWH_PER_GB_DATACENTER:_e,EMBODIED_KWH_PER_GB_NETWORK:Ce,EMBODIED_KWH_PER_GB_DEVICE:Ae,GLOBAL_GRID_INTENSITY:y}=G;function X(t,e){let r=t.dataCenter+t.network+t.device,a=e.dataCenter+e.network+e.device,n=t.dataCenter+e.dataCenter,i=t.network+e.network,o=t.device+e.device;return{dataCenterOperationalCO2e:t.dataCenter,networkOperationalCO2e:t.network,consumerDeviceOperationalCO2e:t.device,dataCenterEmbodiedCO2e:e.dataCenter,networkEmbodiedCO2e:e.network,consumerDeviceEmbodiedCO2e:e.device,totalEmbodiedCO2e:a,totalOperationalCO2e:r,dataCenterCO2e:n,networkCO2e:i,consumerDeviceCO2e:o}}function Q(t,e){return t?1:e?.greenHostingFactor||e?.greenHostingFactor===0?e.greenHostingFactor:0}var F=class{constructor(e){this.allowRatings=!0,this.options=e,this.version=4}operationalEnergyPerSegment(e){let r=e/N.GIGABYTE,a=r*Te,n=r*Ie,i=r*Ne;return{dataCenter:a,network:n,device:i}}operationalEmissions(e,r={}){let{dataCenter:a,network:n,device:i}=this.operationalEnergyPerSegment(e),o=y,s=y,u=y;if(r?.gridIntensity){let{device:d,network:I,dataCenter:f}=r.gridIntensity;(d?.value||d?.value===0)&&(u=d.value),(I?.value||I?.value===0)&&(s=I.value),(f?.value||f?.value===0)&&(o=f.value)}let l=a*o,c=n*s,E=i*u;return{dataCenter:l,network:c,device:E}}embodiedEnergyPerSegment(e){let r=e/N.GIGABYTE,a=r*_e,n=r*Ce,i=r*Ae;return{dataCenter:a,network:n,device:i}}embodiedEmissions(e){let{dataCenter:r,network:a,device:n}=this.embodiedEnergyPerSegment(e),i=y,o=y,s=y,u=r*i,l=a*o,c=n*s;return{dataCenter:u,network:l,device:c}}perByte(e,r=!1,a=!1,n=!1,i={}){if(e<1)return 0;let o=this.operationalEmissions(e,i),s=this.embodiedEmissions(e),u=Q(r,i),l={dataCenter:o.dataCenter*(1-u)+s.dataCenter,network:o.network+s.network,device:o.device+s.device},c=l.dataCenter+l.network+l.device,E=null;if(n&&(E=this.ratingScale(c)),a){let d={...X(o,s)};return n?{...d,total:c,rating:E}:{...d,total:c}}return n?{total:c,rating:E}:c}perVisit(e,r=!1,a=!1,n=!1,i={}){let o=1,s=0,u=0,l=Q(r,i),c=this.operationalEmissions(e,i),E=this.embodiedEmissions(e);if(e<1)return 0;(i.firstVisitPercentage||i.firstVisitPercentage===0)&&(o=i.firstVisitPercentage),(i.returnVisitPercentage||i.returnVisitPercentage===0)&&(s=i.returnVisitPercentage),(i.dataReloadRatio||i.dataReloadRatio===0)&&(u=i.dataReloadRatio);let d=c.dataCenter*(1-l)+E.dataCenter+c.network+E.network+c.device+E.device,I=(c.dataCenter*(1-l)+E.dataCenter+c.network+E.network+c.device+E.device)*(1-u),f=d*o+I*s,h=null;if(n&&(h=this.ratingScale(f)),a){let Y={...X(c,E),firstVisitCO2e:d,returnVisitCO2e:I};return n?{...Y,total:f,rating:h}:{...Y,total:f}}return n?{total:f,rating:h}:f}ratingScale(e){return S(e,this.version)}};var q=F;var W=class{constructor(e){if(this.model=new H,e?.model==="1byte")this.model=new x;else if(e?.model==="swd")this.model=new H,e?.version===4&&(this.model=new q);else if(e?.model)throw new Error(`"${e.model}" is not a valid model. Please use "1byte" for the OneByte model, and "swd" for the Sustainable Web Design model. +Falling back to default value.`)):e===4&&(n.greenHostingFactor=0),r&&(n.greenHostingFactor=1),n}function H(t=""){return{"User-Agent":`co2js/0.16.1 ${t}`}}function S(t,e){let{FIFTH_PERCENTILE:r,TENTH_PERCENTILE:a,TWENTIETH_PERCENTILE:n,THIRTIETH_PERCENTILE:i,FORTIETH_PERCENTILE:o,FIFTIETH_PERCENTILE:s}=X;return e===4&&(r=T.FIFTH_PERCENTILE,a=T.TENTH_PERCENTILE,n=T.TWENTIETH_PERCENTILE,i=T.THIRTIETH_PERCENTILE,o=T.FORTIETH_PERCENTILE,s=T.FIFTIETH_PERCENTILE),m(t,r)?"A+":m(t,a)?"A":m(t,n)?"B":m(t,i)?"C":m(t,o)?"D":m(t,s)?"E":"F"}var F=class{constructor(e){this.allowRatings=!0,this.options=e,this.version=3}energyPerByteByComponent(e){let a=e/N.GIGABYTE*$;return{consumerDeviceEnergy:a*V,networkEnergy:a*L,productionEnergy:a*w,dataCenterEnergy:a*v}}co2byComponent(e,r=g,a={}){let n=g,i=g,o=g,s=g;if(a?.gridIntensity){let{device:l,network:c,dataCenter:E}=a.gridIntensity;(l?.value||l?.value===0)&&(n=l.value),(c?.value||c?.value===0)&&(i=c.value),(E?.value||E?.value===0)&&(o=E.value)}r===!0&&(o=J);let u={};for(let[l,c]of Object.entries(e))l.startsWith("dataCenterEnergy")?u[l.replace("Energy","CO2")]=c*o:l.startsWith("consumerDeviceEnergy")?u[l.replace("Energy","CO2")]=c*n:l.startsWith("networkEnergy")?u[l.replace("Energy","CO2")]=c*i:u[l.replace("Energy","CO2")]=c*s;return u}perByte(e,r=!1,a=!1,n=!1,i={}){e<1&&(e=0);let o=this.energyPerByteByComponent(e,i);if(typeof r!="boolean")throw new Error(`perByte expects a boolean for the carbon intensity value. Received: ${r}`);let s=this.co2byComponent(o,r,i),l=Object.values(s).reduce((E,d)=>E+d),c=null;return n&&(c=this.ratingScale(l)),a?n?{...s,total:l,rating:c}:{...s,total:l}:n?{total:l,rating:c}:l}perVisit(e,r=!1,a=!1,n=!1,i={}){let o=this.energyPerVisitByComponent(e,i);if(typeof r!="boolean")throw new Error(`perVisit expects a boolean for the carbon intensity value. Received: ${r}`);let s=this.co2byComponent(o,r,i),l=Object.values(s).reduce((E,d)=>E+d),c=null;return n&&(c=this.ratingScale(l)),a?n?{...s,total:l,rating:c}:{...s,total:l}:n?{total:l,rating:c}:l}energyPerByte(e){let r=this.energyPerByteByComponent(e);return Object.values(r).reduce((n,i)=>n+i)}energyPerVisitByComponent(e,r={},a=_,n=C,i=A){(r.dataReloadRatio||r.dataReloadRatio===0)&&(i=r.dataReloadRatio),(r.firstVisitPercentage||r.firstVisitPercentage===0)&&(a=r.firstVisitPercentage),(r.returnVisitPercentage||r.returnVisitPercentage===0)&&(n=r.returnVisitPercentage);let o=this.energyPerByteByComponent(e),s={},u=Object.values(o);for(let[l,c]of Object.entries(o))s[`${l} - first`]=c*a,s[`${l} - subsequent`]=c*n*i;return s}energyPerVisit(e){let r=0,a=0,n=Object.entries(this.energyPerVisitByComponent(e));for(let[i,o]of n)i.indexOf("first")>0&&(r+=o);for(let[i,o]of n)i.indexOf("subsequent")>0&&(a+=o);return r+a}emissionsPerVisitInGrams(e,r=g){return O(e*r)}annualEnergyInKwh(e,r=1e3){return e*r*12}annualEmissionsInGrams(e,r=1e3){return e*r*12}annualSegmentEnergy(e){return{consumerDeviceEnergy:O(e*V),networkEnergy:O(e*L),dataCenterEnergy:O(e*v),productionEnergy:O(e*w)}}ratingScale(e){return S(e,this.version)}};var h=F;var{OPERATIONAL_KWH_PER_GB_DATACENTER:Te,OPERATIONAL_KWH_PER_GB_NETWORK:Ie,OPERATIONAL_KWH_PER_GB_DEVICE:Ne,EMBODIED_KWH_PER_GB_DATACENTER:_e,EMBODIED_KWH_PER_GB_NETWORK:Ce,EMBODIED_KWH_PER_GB_DEVICE:Ae,GLOBAL_GRID_INTENSITY:y}=G;function Q(t,e){let r=t.dataCenter+t.network+t.device,a=e.dataCenter+e.network+e.device,n=t.dataCenter+e.dataCenter,i=t.network+e.network,o=t.device+e.device;return{dataCenterOperationalCO2e:t.dataCenter,networkOperationalCO2e:t.network,consumerDeviceOperationalCO2e:t.device,dataCenterEmbodiedCO2e:e.dataCenter,networkEmbodiedCO2e:e.network,consumerDeviceEmbodiedCO2e:e.device,totalEmbodiedCO2e:a,totalOperationalCO2e:r,dataCenterCO2e:n,networkCO2e:i,consumerDeviceCO2e:o}}function q(t,e){return t?1:e?.greenHostingFactor||e?.greenHostingFactor===0?e.greenHostingFactor:0}var W=class{constructor(e){this.allowRatings=!0,this.options=e,this.version=4}operationalEnergyPerSegment(e){let r=e/N.GIGABYTE,a=r*Te,n=r*Ie,i=r*Ne;return{dataCenter:a,network:n,device:i}}operationalEmissions(e,r={}){let{dataCenter:a,network:n,device:i}=this.operationalEnergyPerSegment(e),o=y,s=y,u=y;if(r?.gridIntensity){let{device:d,network:I,dataCenter:f}=r.gridIntensity;(d?.value||d?.value===0)&&(u=d.value),(I?.value||I?.value===0)&&(s=I.value),(f?.value||f?.value===0)&&(o=f.value)}let l=a*o,c=n*s,E=i*u;return{dataCenter:l,network:c,device:E}}embodiedEnergyPerSegment(e){let r=e/N.GIGABYTE,a=r*_e,n=r*Ce,i=r*Ae;return{dataCenter:a,network:n,device:i}}embodiedEmissions(e){let{dataCenter:r,network:a,device:n}=this.embodiedEnergyPerSegment(e),i=y,o=y,s=y,u=r*i,l=a*o,c=n*s;return{dataCenter:u,network:l,device:c}}perByte(e,r=!1,a=!1,n=!1,i={}){if(e<1)return 0;let o=this.operationalEmissions(e,i),s=this.embodiedEmissions(e),u=q(r,i),l={dataCenter:o.dataCenter*(1-u)+s.dataCenter,network:o.network+s.network,device:o.device+s.device},c=l.dataCenter+l.network+l.device,E=null;if(n&&(E=this.ratingScale(c)),a){let d={...Q(o,s)};return n?{...d,total:c,rating:E}:{...d,total:c}}return n?{total:c,rating:E}:c}perVisit(e,r=!1,a=!1,n=!1,i={}){let o=1,s=0,u=0,l=q(r,i),c=this.operationalEmissions(e,i),E=this.embodiedEmissions(e);if(e<1)return 0;(i.firstVisitPercentage||i.firstVisitPercentage===0)&&(o=i.firstVisitPercentage),(i.returnVisitPercentage||i.returnVisitPercentage===0)&&(s=i.returnVisitPercentage),(i.dataReloadRatio||i.dataReloadRatio===0)&&(u=i.dataReloadRatio);let d=c.dataCenter*(1-l)+E.dataCenter+c.network+E.network+c.device+E.device,I=(c.dataCenter*(1-l)+E.dataCenter+c.network+E.network+c.device+E.device)*(1-u),f=d*o+I*s,B=null;if(n&&(B=this.ratingScale(f)),a){let x={...Q(c,E),firstVisitCO2e:d,returnVisitCO2e:I};return n?{...x,total:f,rating:B}:{...x,total:f}}return n?{total:f,rating:B}:f}ratingScale(e){return S(e,this.version)}};var U=W;var K=class{constructor(e){if(this.model=new h,e?.model==="1byte")this.model=new p;else if(e?.model==="swd")this.model=new h,e?.version===4&&(this.model=new U);else if(e?.model)throw new Error(`"${e.model}" is not a valid model. Please use "1byte" for the OneByte model, and "swd" for the Sustainable Web Design model. See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`);if(e?.rating&&typeof e.rating!="boolean")throw new Error(`The rating option must be a boolean. Please use true or false. See https://developers.thegreenwebfoundation.org/co2js/options/ to learn more about the options available in CO2.js.`);let r=!!this.model.allowRatings;if(this._segment=e?.results==="segment",this._rating=e?.rating===!0,!r&&this._rating)throw new Error(`The rating system is not supported in the model you are using. Try using the Sustainable Web Design model instead. See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`)}perByte(e,r=!1){return this.model.perByte(e,r,this._segment,this._rating)}perVisit(e,r=!1){if(this.model?.perVisit)return this.model.perVisit(e,r,this._segment,this._rating);throw new Error(`The perVisit() method is not supported in the model you are using. Try using perByte() instead. -See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`)}perByteTrace(e,r=!1,a={}){let n=v(a,this.model.version,r),{gridIntensity:i,...o}=n,{dataReloadRatio:s,firstVisitPercentage:u,returnVisitPercentage:l,...c}=o;return{co2:this.model.perByte(e,r,this._segment,this._rating,n),green:r,variables:{description:"Below are the variables used to calculate this CO2 estimate.",bytes:e,gridIntensity:{description:"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",...n.gridIntensity},...c}}}perVisitTrace(e,r=!1,a={}){if(this.model?.perVisit){let n=v(a,this.model.version,r),{gridIntensity:i,...o}=n;return{co2:this.model.perVisit(e,r,this._segment,this._rating,n),green:r,variables:{description:"Below are the variables used to calculate this CO2 estimate.",bytes:e,gridIntensity:{description:"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",...n.gridIntensity},...o}}}else throw new Error(`The perVisitTrace() method is not supported in the model you are using. Try using perByte() instead. -See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`)}};var U=W;var re=ue(te());function he(t,e){let r=typeof e=="string"?{userAgentIdentifier:e}:e;if(r?.db&&r.verbose)throw new Error("verbose mode cannot be used with a local lookup database");return typeof t=="string"?Be(t,r):De(t,r)}async function Be(t,e={}){let r=await fetch(`https://api.thegreenwebfoundation.org/greencheck/${t}`,{headers:M(e.userAgentIdentifier)});if(e?.db)return re.default.check(t,e.db);let a=await r.json();return e.verbose?a:a.green}async function De(t,e={}){try{let r="https://api.thegreenwebfoundation.org/v2/greencheckmulti",a=JSON.stringify(t),i=await(await fetch(`${r}/${a}`,{headers:M(e.userAgentIdentifier)})).json();return e.verbose?i:pe(i)}catch{return e.verbose?{}:[]}}function pe(t){return Object.entries(t).filter(([a,n])=>n.green).map(([a,n])=>n.url)}var ne={check:he};function be(t,e){return ne.check(t,e)}var K={check:be};var Le={AFG:"414",ALB:"0",DZA:"528",ASM:"753",AND:"188",AGO:"1476",AIA:"753",ATG:"753",ARG:"478",ARM:"390",ABW:"753",AUS:"808",AUT:"242",AZE:"534","AZORES (PORTUGAL)":"753",BHS:"753",BHR:"726",BGD:"528",BRB:"749",BLR:"400",BEL:"252",BLZ:"403",BEN:"745",BMU:"753",BTN:"0",BOL:"604",BES:"753",BIH:"1197",BWA:"1486",BRA:"284",VGB:"753",BRN:"681",BGR:"911",BFA:"753",BDI:"414",KHM:"1046",CMR:"659",CAN:"372",CYM:"753",CPV:"753",CAF:"188",TCD:"753","CHANNEL ISLANDS (U.K)":"753",CHL:"657",CHN:"899",COL:"410",COM:"753",COD:"0",COG:"659",COK:"753",CRI:"108",CIV:"466",HRV:"294",CUB:"559",CUW:"876",CYP:"751",CZE:"902",DNK:"362",DJI:"753",DMA:"753",DOM:"601",ECU:"560",EGY:"554",SLV:"547",GNQ:"632",ERI:"915",EST:"1057",SWZ:"0",ETH:"0",FLK:"753",FRO:"753",FJI:"640",FIN:"267",FRA:"158",GUF:"423",PYF:"753",GAB:"946",GMB:"753",GEO:"289",DEU:"650",GHA:"495",GIB:"779",GRC:"507",GRL:"264",GRD:"753",GLP:"753",GUM:"753",GTM:"798",GIN:"753",GNB:"753",GUY:"847",HTI:"1048",HND:"662",HUN:"296",ISL:"0",IND:"951",IDN:"783",IRN:"592",IRQ:"1080",IRL:"380",IMN:"436",ISR:"394",ITA:"414",JAM:"711",JPN:"471",JOR:"529",KAZ:"797",KEN:"574",KIR:"753",PRK:"754",KOR:"555",XKX:"1145",KWT:"675",KGZ:"217",LAO:"1069",LVA:"240",LBN:"794",LSO:"0",LBR:"677",LBY:"668",LIE:"151",LTU:"211",LUX:"220",MDG:"876","MADEIRA (PORTUGAL)":"663",MWI:"489",MYS:"551",MDV:"753",MLI:"1076",MLT:"520",MHL:"753",MTQ:"753",MRT:"753",MUS:"700",MYT:"753",MEX:"531",FSM:"753",MDA:"541",MCO:"158",MNG:"1366",MNE:"899",MSR:"753",MAR:"729",MOZ:"234",MMR:"719",NAM:"355",NRU:"753",NPL:"0",NLD:"326",NCL:"779",NZL:"246",NIC:"675",NER:"772",NGA:"526",NIU:"753",MKD:"851",MNP:"753",NOR:"47",OMN:"479",PAK:"592",PLW:"753",PSE:"719",PAN:"477",PNG:"597",PRY:"0",PER:"473",PHL:"672",POL:"828",PRT:"389",PRI:"596",QAT:"503",REU:"772",ROU:"489",RUS:"476",RWA:"712",SHN:"753",KNA:"753",LCA:"753",MAF:"753",SPM:"753",VCT:"753",WSM:"753",SMR:"414",STP:"753",SAU:"592",SEN:"870",SRB:"1086",SYC:"753",SLE:"489",SGP:"379",SXM:"753",SVK:"332",SVN:"620",SLB:"753",SOM:"753",ZAF:"1070",SSD:"890",ESP:"402",LKA:"731",SDN:"736",SUR:"1029",SWE:"68",CHE:"48",SYR:"713",TWN:"484",TJK:"255",TZA:"531",THA:"450",TLS:"753",TGO:"859",TON:"753",TTO:"559",TUN:"468",TUR:"376",TKM:"927",TCA:"753",TUV:"753",UGA:"279",UKR:"768",ARE:"556",GBR:"380",USA:"416",URY:"174",UZB:"612",VUT:"753",VEN:"711",VNM:"560",VIR:"650",YEM:"807",ZMB:"416",ZWE:"1575","MEMO: EU 27":"409"},Ve="marginal",ve="2021";var k={data:Le,type:Ve,year:ve};var Me={co2:U,hosting:K,averageIntensity:R,marginalIntensity:k};return de(we);})(); +See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`)}perByteTrace(e,r=!1,a={}){let n=M(a,this.model.version,r),{gridIntensity:i,...o}=n,{dataReloadRatio:s,firstVisitPercentage:u,returnVisitPercentage:l,...c}=o;return{co2:this.model.perByte(e,r,this._segment,this._rating,n),green:r,variables:{description:"Below are the variables used to calculate this CO2 estimate.",bytes:e,gridIntensity:{description:"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",...n.gridIntensity},...c}}}perVisitTrace(e,r=!1,a={}){if(this.model?.perVisit){let n=M(a,this.model.version,r),{gridIntensity:i,...o}=n;return{co2:this.model.perVisit(e,r,this._segment,this._rating,n),green:r,variables:{description:"Below are the variables used to calculate this CO2 estimate.",bytes:e,gridIntensity:{description:"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",...n.gridIntensity},...o}}}else throw new Error(`The perVisitTrace() method is not supported in the model you are using. Try using perByte() instead. +See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`)}SustainableWebDesignV3(){return new h}SustainableWebDesignV4(){return new U}OneByte(){return new p}};var k=K;var re=ue(te());function he(t,e){let r=typeof e=="string"?{userAgentIdentifier:e}:e;if(r?.db&&r.verbose)throw new Error("verbose mode cannot be used with a local lookup database");return typeof t=="string"?Be(t,r):De(t,r)}async function Be(t,e={}){let r=await fetch(`https://api.thegreenwebfoundation.org/greencheck/${t}`,{headers:H(e.userAgentIdentifier)});if(e?.db)return re.default.check(t,e.db);let a=await r.json();return e.verbose?a:a.green}async function De(t,e={}){try{let r="https://api.thegreenwebfoundation.org/v2/greencheckmulti",a=JSON.stringify(t),i=await(await fetch(`${r}/${a}`,{headers:H(e.userAgentIdentifier)})).json();return e.verbose?i:be(i)}catch{return e.verbose?{}:[]}}function be(t){return Object.entries(t).filter(([a,n])=>n.green).map(([a,n])=>n.url)}var ne={check:he};function pe(t,e){return ne.check(t,e)}var Y=pe;var Ve={AFG:"414",ALB:"0",DZA:"528",ASM:"753",AND:"188",AGO:"1476",AIA:"753",ATG:"753",ARG:"478",ARM:"390",ABW:"753",AUS:"808",AUT:"242",AZE:"534","AZORES (PORTUGAL)":"753",BHS:"753",BHR:"726",BGD:"528",BRB:"749",BLR:"400",BEL:"252",BLZ:"403",BEN:"745",BMU:"753",BTN:"0",BOL:"604",BES:"753",BIH:"1197",BWA:"1486",BRA:"284",VGB:"753",BRN:"681",BGR:"911",BFA:"753",BDI:"414",KHM:"1046",CMR:"659",CAN:"372",CYM:"753",CPV:"753",CAF:"188",TCD:"753","CHANNEL ISLANDS (U.K)":"753",CHL:"657",CHN:"899",COL:"410",COM:"753",COD:"0",COG:"659",COK:"753",CRI:"108",CIV:"466",HRV:"294",CUB:"559",CUW:"876",CYP:"751",CZE:"902",DNK:"362",DJI:"753",DMA:"753",DOM:"601",ECU:"560",EGY:"554",SLV:"547",GNQ:"632",ERI:"915",EST:"1057",SWZ:"0",ETH:"0",FLK:"753",FRO:"753",FJI:"640",FIN:"267",FRA:"158",GUF:"423",PYF:"753",GAB:"946",GMB:"753",GEO:"289",DEU:"650",GHA:"495",GIB:"779",GRC:"507",GRL:"264",GRD:"753",GLP:"753",GUM:"753",GTM:"798",GIN:"753",GNB:"753",GUY:"847",HTI:"1048",HND:"662",HUN:"296",ISL:"0",IND:"951",IDN:"783",IRN:"592",IRQ:"1080",IRL:"380",IMN:"436",ISR:"394",ITA:"414",JAM:"711",JPN:"471",JOR:"529",KAZ:"797",KEN:"574",KIR:"753",PRK:"754",KOR:"555",XKX:"1145",KWT:"675",KGZ:"217",LAO:"1069",LVA:"240",LBN:"794",LSO:"0",LBR:"677",LBY:"668",LIE:"151",LTU:"211",LUX:"220",MDG:"876","MADEIRA (PORTUGAL)":"663",MWI:"489",MYS:"551",MDV:"753",MLI:"1076",MLT:"520",MHL:"753",MTQ:"753",MRT:"753",MUS:"700",MYT:"753",MEX:"531",FSM:"753",MDA:"541",MCO:"158",MNG:"1366",MNE:"899",MSR:"753",MAR:"729",MOZ:"234",MMR:"719",NAM:"355",NRU:"753",NPL:"0",NLD:"326",NCL:"779",NZL:"246",NIC:"675",NER:"772",NGA:"526",NIU:"753",MKD:"851",MNP:"753",NOR:"47",OMN:"479",PAK:"592",PLW:"753",PSE:"719",PAN:"477",PNG:"597",PRY:"0",PER:"473",PHL:"672",POL:"828",PRT:"389",PRI:"596",QAT:"503",REU:"772",ROU:"489",RUS:"476",RWA:"712",SHN:"753",KNA:"753",LCA:"753",MAF:"753",SPM:"753",VCT:"753",WSM:"753",SMR:"414",STP:"753",SAU:"592",SEN:"870",SRB:"1086",SYC:"753",SLE:"489",SGP:"379",SXM:"753",SVK:"332",SVN:"620",SLB:"753",SOM:"753",ZAF:"1070",SSD:"890",ESP:"402",LKA:"731",SDN:"736",SUR:"1029",SWE:"68",CHE:"48",SYR:"713",TWN:"484",TJK:"255",TZA:"531",THA:"450",TLS:"753",TGO:"859",TON:"753",TTO:"559",TUN:"468",TUR:"376",TKM:"927",TCA:"753",TUV:"753",UGA:"279",UKR:"768",ARE:"556",GBR:"380",USA:"416",URY:"174",UZB:"612",VUT:"753",VEN:"711",VNM:"560",VIR:"650",YEM:"807",ZMB:"416",ZWE:"1575","MEMO: EU 27":"409"},Le="marginal",ve="2021";var j={data:Ve,type:Le,year:ve};var we={co2:k,hosting:Y,averageIntensity:R,marginalIntensity:j};return de(Me);})(); //# sourceMappingURL=index.js.map diff --git a/jsr.json b/jsr.json index abfd5949..5d01b578 100644 --- a/jsr.json +++ b/jsr.json @@ -1,7 +1,7 @@ { "name": "@greenweb/co2js", - "version": "0.16.0", - "exports": "./dist/esm/index.js", + "version": "0.16.2", + "exports": "./mod.ts", "exclude": [ "!dist/**" ] diff --git a/mod.ts b/mod.ts new file mode 100644 index 00000000..e258df15 --- /dev/null +++ b/mod.ts @@ -0,0 +1 @@ +export * from "./src/index.js"; diff --git a/package-lock.json b/package-lock.json index b299802f..9df7f9eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tgwf/co2", - "version": "0.16.0", + "version": "0.16.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@tgwf/co2", - "version": "0.16.0", + "version": "0.16.1", "license": "Apache-2.0", "devDependencies": { "@tgwf/url2green": "^0.4.0", diff --git a/package.json b/package.json index 73c8346a..9ecab6f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tgwf/co2", - "version": "0.16.0", + "version": "0.16.1", "description": "Work out the co2 of your digital services", "main": "dist/cjs/index-node.js", "module": "dist/esm/index.js", diff --git a/src/co2.js b/src/co2.js index e3daa5ab..db8f598b 100644 --- a/src/co2.js +++ b/src/co2.js @@ -227,6 +227,18 @@ class CO2 { ); } } + + SustainableWebDesignV3() { + return new SustainableWebDesignV3(); + } + + SustainableWebDesignV4() { + return new SustainableWebDesignV4(); + } + + OneByte() { + return new OneByte(); + } } export { CO2 }; diff --git a/src/constants/test-constants.js b/src/constants/test-constants.js index 918de299..5e564565 100644 --- a/src/constants/test-constants.js +++ b/src/constants/test-constants.js @@ -12,7 +12,7 @@ export const ONEBYTE = { export const SWDV3 = { MILLION_GREY: 0.38998, MILLION_GREEN: 0.33756, - MILLION_PERVISIT_GREY: 0.29444, + MILLION_PERVISIT_GREY: 0.29455, MILLION_PERVISIT_GREEN: 0.25451, TGWF_GREY_VALUE: 0.27980, @@ -25,13 +25,13 @@ export const SWDV3 = { MILLION_GREEN_DATACENTERS: 0.00607, MILLION_GREY_PRODUCTION: 0.07399, - MILLION_PERVISIT_GREY_DEVICE_GRID_INTENSITY_CHANGE: 0.34827, + MILLION_PERVISIT_GREY_DEVICE_GRID_INTENSITY_CHANGE: 0.3463, MILLION_PERVISIT_GREY_DATACENTER_GRID_INTENSITY_CHANGE: 0.30996, MILLION_PERVISIT_GREY_NETWORK_GRID_INTENSITY_CHANGE: 0.30893, - MILLION_PERBYTE_GREY_DEVICE_GRID_INTENSITY_CHANGE: 0.46130, - MILLION_PERBYTE_GREY_DATACENTER_GRID_INTENSITY_CHANGE: 0.41055, - MILLION_PERBYTE_GREY_NETWORK_GRID_INTENSITY_CHANGE: 0.40918, + MILLION_PERBYTE_GREY_DEVICE_GRID_INTENSITY_CHANGE: 0.4587, + MILLION_PERBYTE_GREY_DATACENTER_GRID_INTENSITY_CHANGE: 0.40991, + MILLION_PERBYTE_GREY_NETWORK_GRID_INTENSITY_CHANGE: 0.40859, MILLION_PERVISIT_GREY_DEVICES_FIRST: 0.15188, MILLION_PERVISIT_GREY_DEVICES_SECOND: 0.00092, diff --git a/src/data/average-intensities.min.js b/src/data/average-intensities.min.js index fd7c96c3..601c7746 100644 --- a/src/data/average-intensities.min.js +++ b/src/data/average-intensities.min.js @@ -1 +1,11 @@ -const data = {"AFG":132.53,"AFRICA":544.76,"ALB":24.29,"DZA":634.61,"ASM":611.11,"AGO":174.73,"ATG":611.11,"ARG":354.1,"ARM":264.54,"ABW":561.22,"ASEAN":571.29,"ASIA":590.05,"AUS":548.65,"AUT":110.81,"AZE":671.39,"BHS":660.1,"BHR":904.62,"BGD":691.41,"BRB":605.51,"BLR":441.74,"BEL":138.11,"BLZ":225.81,"BEN":584.07,"BTN":23.33,"BOL":531.69,"BIH":601.29,"BWA":847.91,"BRA":98.31,"BRN":893.91,"BGR":335.33,"BFA":467.53,"BDI":250,"CPV":558.14,"KHM":417.71,"CMR":305.42,"CAN":171.12,"CYM":642.86,"CAF":0,"TCD":628.57,"CHL":291.11,"CHN":582.26,"COL":259.51,"COM":642.86,"COG":700,"COD":24.46,"COK":250,"CRI":53.38,"CIV":393.89,"HRV":204.96,"CUB":637.61,"CYP":534.32,"CZE":449.72,"DNK":151.65,"DJI":692.31,"DMA":529.41,"DOM":580.78,"ECU":166.91,"EGY":569.59,"SLV":224.76,"GNQ":591.84,"ERI":631.58,"EST":416.67,"SWZ":172.41,"ETH":24.64,"EU":243.94,"EUROPE":301.99,"FLK":500,"FRO":404.76,"FJI":288.46,"FIN":79.16,"FRA":56.02,"GUF":217.82,"PYF":442.86,"G20":477.15,"G7":341.04,"GAB":491.6,"GMB":666.67,"GEO":167.59,"DEU":381.16,"GHA":484,"GRC":336.57,"GRL":178.57,"GRD":640,"GLP":500,"GUM":622.86,"GTM":328.27,"GIN":236.84,"GNB":625,"GUY":640.35,"HTI":567.31,"HND":282.27,"HKG":699.5,"HUN":204.19,"ISL":27.68,"IND":713.44,"IDN":682.43,"IRN":655.13,"IRQ":688.81,"IRL":282.98,"ISR":582.93,"ITA":330.72,"JAM":555.56,"JPN":485.39,"JOR":540.92,"KAZ":821.39,"KEN":71.2,"KIR":666.67,"XKX":894.65,"KWT":649.16,"KGZ":147.29,"LAO":265.51,"LATIN AMERICA AND CARIBBEAN":260.09,"LVA":123.99,"LBN":599.01,"LSO":20,"LBR":227.85,"LBY":818.69,"LTU":160.07,"LUX":105.26,"MAC":448.98,"MDG":436.44,"MWI":66.67,"MYS":605.32,"MDV":611.77,"MLI":408,"MLT":459.14,"MTQ":523.18,"MRT":464.71,"MUS":632.48,"MEX":507.25,"MIDDLE EAST":657.52,"MDA":643.46,"MNG":775.31,"MNE":418.09,"MSR":1000,"MAR":624.45,"MOZ":135.65,"MMR":436.92,"NAM":59.26,"NRU":750,"NPL":24.44,"NLD":268.48,"NCL":660.58,"NZL":112.76,"NIC":265.12,"NER":670.89,"NGA":523.25,"NORTH AMERICA":343.66,"PRK":389.59,"MKD":556.19,"NOR":30.05,"OCEANIA":489.59,"OECD":341.19,"OMN":564.55,"PAK":440.61,"PSE":516.13,"PAN":161.68,"PNG":507.25,"PRY":24.31,"PER":266.48,"POL":661.93,"PRT":165.55,"PRI":677.96,"QAT":602.5,"REU":572.82,"ROU":240.58,"RUS":441.04,"RWA":316.33,"KNA":636.36,"LCA":666.67,"SPM":600,"VCT":529.41,"WSM":473.68,"STP":642.86,"SAU":706.79,"SEN":511.6,"SRB":647.52,"SYC":564.52,"SLE":50,"SGP":470.78,"SVK":116.77,"SVN":231.28,"SLB":700,"SOM":578.95,"ZAF":707.69,"KOR":430.57,"SSD":629.03,"ESP":174.05,"LKA":509.78,"SDN":263.16,"SUR":349.28,"SWE":40.7,"CHE":34.68,"SYR":701.66,"TWN":650.73,"TJK":116.86,"TZA":339.25,"THA":549.61,"PHL":610.74,"TGO":443.18,"TON":625,"TTO":681.53,"TUN":563.96,"TUR":464.59,"TKM":1306.03,"TCA":653.85,"UGA":44.53,"UKR":259.69,"ARE":561.14,"GBR":237.59,"USA":369.47,"URY":128.79,"UZB":1167.6,"VUT":571.43,"VEN":185.8,"VNM":475.45,"VGB":647.06,"VIR":632.35,"WORLD":481.46,"YEM":566.1,"ZMB":111.97,"ZWE":297.87}; const type = "average"; export { data, type }; export default { data, type }; \ No newline at end of file +/** + * @fileoverview Minified average CO2 emissions intensity data for countries. + * @generated Generated by generate_average_co2.js + * @version 1.0.0 +*/ + +/** + * @constant {Object.} data - Average CO2 emissions intensity data for various countries. + * @constant {string} type - Type of data being represented. + */ +const data = {"AFG":132.53,"AFRICA":547.83,"ALB":24.29,"DZA":634.61,"ASM":611.11,"AGO":174.73,"ATG":611.11,"ARG":353.96,"ARM":264.54,"ABW":561.22,"ASEAN":570.41,"ASIA":591.13,"AUS":556.3,"AUT":110.81,"AZE":671.39,"BHS":660.1,"BHR":904.62,"BGD":691.41,"BRB":605.51,"BLR":441.74,"BEL":138.11,"BLZ":225.81,"BEN":584.07,"BTN":23.33,"BOL":531.69,"BIH":601.29,"BWA":847.91,"BRA":96.4,"BRN":893.91,"BGR":335.33,"BFA":467.53,"BDI":250,"CPV":558.14,"KHM":417.71,"CMR":305.42,"CAN":165.15,"CYM":642.86,"CAF":0,"TCD":628.57,"CHL":291.11,"CHN":583.61,"COL":259.51,"COM":642.86,"COG":700,"COD":24.46,"COK":250,"CRI":53.38,"CIV":393.89,"HRV":204.96,"CUB":637.61,"CYP":534.32,"CZE":449.72,"DNK":151.65,"DJI":692.31,"DMA":529.41,"DOM":580.78,"ECU":166.91,"EGY":574.04,"SLV":224.76,"GNQ":591.84,"ERI":631.58,"EST":416.67,"SWZ":172.41,"ETH":24.64,"EU":243.93,"EUROPE":302.29,"FLK":500,"FRO":404.76,"FJI":288.46,"FIN":79.16,"FRA":56.02,"GUF":217.82,"PYF":442.86,"G20":477.89,"G7":341.44,"GAB":491.6,"GMB":666.67,"GEO":167.59,"DEU":381.41,"GHA":484,"GRC":336.57,"GRL":178.57,"GRD":640,"GLP":500,"GUM":622.86,"GTM":328.27,"GIN":236.84,"GNB":625,"GUY":640.35,"HTI":567.31,"HND":282.27,"HKG":699.5,"HUN":204.19,"ISL":27.68,"IND":713.01,"IDN":682.43,"IRN":641.73,"IRQ":688.81,"IRL":282.98,"ISR":582.93,"ITA":330.72,"JAM":555.56,"JPN":493.59,"JOR":540.92,"KAZ":821.9,"KEN":71.2,"KIR":666.67,"XKX":894.65,"KWT":649.16,"KGZ":147.29,"LAO":265.51,"LATIN AMERICA AND CARIBBEAN":256.03,"LVA":123.99,"LBN":599.01,"LSO":20,"LBR":227.85,"LBY":818.69,"LTU":160.07,"LUX":105.26,"MAC":448.98,"MDG":436.44,"MWI":66.67,"MYS":607.88,"MDV":611.77,"MLI":408,"MLT":459.14,"MTQ":523.18,"MRT":464.71,"MUS":632.48,"MEX":492.34,"MIDDLE EAST":643.04,"MDA":643.46,"MNG":775.31,"MNE":418.09,"MSR":1000,"MAR":624.45,"MOZ":135.65,"MMR":436.92,"NAM":59.26,"NRU":750,"NPL":24.44,"NLD":268.48,"NCL":660.58,"NZL":112.76,"NIC":265.12,"NER":670.89,"NGA":523.25,"NORTH AMERICA":342.95,"PRK":389.59,"MKD":556.19,"NOR":30.05,"OCEANIA":495.74,"OECD":341.27,"OMN":564.55,"PAK":440.61,"PSE":516.13,"PAN":161.68,"PNG":507.25,"PRY":24.31,"PER":266.48,"POL":661.93,"PRT":165.55,"PRI":677.96,"QAT":602.5,"REU":572.82,"ROU":240.58,"RUS":445.02,"RWA":316.33,"KNA":636.36,"LCA":666.67,"SPM":600,"VCT":529.41,"WSM":473.68,"STP":642.86,"SAU":696.31,"SEN":511.6,"SRB":647.52,"SYC":564.52,"SLE":50,"SGP":470.78,"SVK":116.77,"SVN":231.28,"SLB":700,"SOM":578.95,"ZAF":709.69,"KOR":432.11,"SSD":629.03,"ESP":174.05,"LKA":509.78,"SDN":263.16,"SUR":349.28,"SWE":40.7,"CHE":34.68,"SYR":701.66,"TWN":644.36,"TJK":116.86,"TZA":339.25,"THA":549.85,"PHL":610.74,"TGO":443.18,"TON":625,"TTO":681.53,"TUN":563.96,"TUR":464.59,"TKM":1306.03,"TCA":653.85,"UGA":44.53,"UKR":256.21,"ARE":492.7,"GBR":228.25,"USA":369.47,"URY":128.79,"UZB":1167.6,"VUT":571.43,"VEN":185.8,"VNM":472.47,"VGB":647.06,"VIR":632.35,"WORLD":481.65,"YEM":566.1,"ZMB":111.97,"ZWE":297.87}; const type = "average"; export { data, type }; export default { data, type }; \ No newline at end of file diff --git a/src/data/marginal-intensities-2021.min.js b/src/data/marginal-intensities-2021.min.js index 3db75c07..6e0097f4 100644 --- a/src/data/marginal-intensities-2021.min.js +++ b/src/data/marginal-intensities-2021.min.js @@ -1 +1,12 @@ +/** + * @fileoverview Minified marginal CO2 emissions intensity data for countries (2021). + * @generated Generated by generate_marginal_co2.js + * @version 1.0.0 + */ + +/** + * @constant {Object.} data - Average CO2 emissions intensity data for various countries. + * @constant {string} type - Type of data being represented. + * @constant {string} year - Year for which the data was collected. + */ const data = {"AFG":"414","ALB":"0","DZA":"528","ASM":"753","AND":"188","AGO":"1476","AIA":"753","ATG":"753","ARG":"478","ARM":"390","ABW":"753","AUS":"808","AUT":"242","AZE":"534","AZORES (PORTUGAL)":"753","BHS":"753","BHR":"726","BGD":"528","BRB":"749","BLR":"400","BEL":"252","BLZ":"403","BEN":"745","BMU":"753","BTN":"0","BOL":"604","BES":"753","BIH":"1197","BWA":"1486","BRA":"284","VGB":"753","BRN":"681","BGR":"911","BFA":"753","BDI":"414","KHM":"1046","CMR":"659","CAN":"372","CYM":"753","CPV":"753","CAF":"188","TCD":"753","CHANNEL ISLANDS (U.K)":"753","CHL":"657","CHN":"899","COL":"410","COM":"753","COD":"0","COG":"659","COK":"753","CRI":"108","CIV":"466","HRV":"294","CUB":"559","CUW":"876","CYP":"751","CZE":"902","DNK":"362","DJI":"753","DMA":"753","DOM":"601","ECU":"560","EGY":"554","SLV":"547","GNQ":"632","ERI":"915","EST":"1057","SWZ":"0","ETH":"0","FLK":"753","FRO":"753","FJI":"640","FIN":"267","FRA":"158","GUF":"423","PYF":"753","GAB":"946","GMB":"753","GEO":"289","DEU":"650","GHA":"495","GIB":"779","GRC":"507","GRL":"264","GRD":"753","GLP":"753","GUM":"753","GTM":"798","GIN":"753","GNB":"753","GUY":"847","HTI":"1048","HND":"662","HUN":"296","ISL":"0","IND":"951","IDN":"783","IRN":"592","IRQ":"1080","IRL":"380","IMN":"436","ISR":"394","ITA":"414","JAM":"711","JPN":"471","JOR":"529","KAZ":"797","KEN":"574","KIR":"753","PRK":"754","KOR":"555","XKX":"1145","KWT":"675","KGZ":"217","LAO":"1069","LVA":"240","LBN":"794","LSO":"0","LBR":"677","LBY":"668","LIE":"151","LTU":"211","LUX":"220","MDG":"876","MADEIRA (PORTUGAL)":"663","MWI":"489","MYS":"551","MDV":"753","MLI":"1076","MLT":"520","MHL":"753","MTQ":"753","MRT":"753","MUS":"700","MYT":"753","MEX":"531","FSM":"753","MDA":"541","MCO":"158","MNG":"1366","MNE":"899","MSR":"753","MAR":"729","MOZ":"234","MMR":"719","NAM":"355","NRU":"753","NPL":"0","NLD":"326","NCL":"779","NZL":"246","NIC":"675","NER":"772","NGA":"526","NIU":"753","MKD":"851","MNP":"753","NOR":"47","OMN":"479","PAK":"592","PLW":"753","PSE":"719","PAN":"477","PNG":"597","PRY":"0","PER":"473","PHL":"672","POL":"828","PRT":"389","PRI":"596","QAT":"503","REU":"772","ROU":"489","RUS":"476","RWA":"712","SHN":"753","KNA":"753","LCA":"753","MAF":"753","SPM":"753","VCT":"753","WSM":"753","SMR":"414","STP":"753","SAU":"592","SEN":"870","SRB":"1086","SYC":"753","SLE":"489","SGP":"379","SXM":"753","SVK":"332","SVN":"620","SLB":"753","SOM":"753","ZAF":"1070","SSD":"890","ESP":"402","LKA":"731","SDN":"736","SUR":"1029","SWE":"68","CHE":"48","SYR":"713","TWN":"484","TJK":"255","TZA":"531","THA":"450","TLS":"753","TGO":"859","TON":"753","TTO":"559","TUN":"468","TUR":"376","TKM":"927","TCA":"753","TUV":"753","UGA":"279","UKR":"768","ARE":"556","GBR":"380","USA":"416","URY":"174","UZB":"612","VUT":"753","VEN":"711","VNM":"560","VIR":"650","YEM":"807","ZMB":"416","ZWE":"1575","MEMO: EU 27":"409"}; const type = "marginal"; const year = "2021"; export { data, type, year }; export default { data, type, year }; \ No newline at end of file diff --git a/src/hosting.js b/src/hosting.js index b8e3da5d..4d783f20 100644 --- a/src/hosting.js +++ b/src/hosting.js @@ -2,6 +2,10 @@ import hostingAPI from "./hosting-api.js"; +/** + * @module hosting + */ + /** * Check if a domain is hosted by a green web host. * @param {string|array} domain - The domain to check, or an array of domains to be checked. @@ -17,6 +21,4 @@ function check(domain, optionsOrAgentId) { return hostingAPI.check(domain, optionsOrAgentId); } -export default { - check, -}; +export default check; diff --git a/src/sustainable-web-design-v3.test.js b/src/sustainable-web-design-v3.test.js index 14c716f0..2cd189a6 100644 --- a/src/sustainable-web-design-v3.test.js +++ b/src/sustainable-web-design-v3.test.js @@ -99,7 +99,7 @@ describe("sustainable web design model version 3", () => { describe("emissionsPerVisitInGrams", () => { it("should calculate the correct co2 per visit", () => { const energy = swd.energyPerVisit(averageWebsiteInBytes); - expect(swd.emissionsPerVisitInGrams(energy)).toEqual(0.66); + expect(swd.emissionsPerVisitInGrams(energy)).toEqual(0.67); }); it("should accept a dynamic KwH value", () => {