thegreenwebfoundation / co2.js

An npm module for accessing the green web API, and estimating the carbon emissions from using digital services
Other
375 stars 47 forks source link

Add Sustainable Web Design rating scale #207

Closed fershad closed 1 month ago

fershad commented 2 months ago

Triage

Type of change

Please select any of the below items that are appropriate.

This pull request:

Related issue/s

Please list below any issues this pull request is related to.

Docs changes required

Do any changes made in this pull request require parts of the CO2.js documentation to be updated?

If you answered "Yes", please create an corresponding issue in our Developer Documentation repository.

Related issue: https://github.com/thegreenwebfoundation/developer-docs/issues/50

Describe the changes made in this pull request

As clearly as possible, describe the changes made in the pull request. You should at least detail "what changes have been made" and "what the results of these changes will be".

This change adds the Sustainable Web Design rating scale into CO2.js for developers using the Sustainable Web Design Model to estimate emissions.

The rating scale is "off" by default, but developers can opt-in to returning results with a carbon emissions total & a corresponding rating by setting rating: true when instantiating an instance of the Sustainable Web Design Model in CO2.js.

import { co2 } from '@tgwf/co2';

const model = new co2({ rating: true });

The rating system is based on the number of bytes passed into carbon emissions results calculated by the perByte, perVisit, perByteTrace, or perVisitTrace functions.

Currently the perByte and perVisit functions return a number representing the estimated CO2e calculated by those functions. When rating: true is set, these functions will now return an object with two keys - rating and total. For example:

import { co2 } from '@tgwf/co2';

const hasRating = new co2({ rating: true });
const noRating = new co2();

hasRating.perByte(1000);

/* Returns:
{ total: 0.0003936519, rating: 'A+' }
*/

noRating.perByte(1000)
/* Returns: 
0.0003936519
*/
fershad commented 2 months ago

@mrchrisadams are you comfortable with this getting merged?