UNPKG

@tgwf/co2

Version:
8 lines (7 loc) 14.7 kB
{ "version": 3, "sources": ["../../../src/helpers/index.js"], "sourcesContent": ["import { averageIntensity } from \"../index.js\";\nimport {\n GLOBAL_GRID_INTENSITY as SWDM3_GLOBAL_GRID_INTENSITY,\n SWDV4,\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD,\n FIRST_TIME_VIEWING_PERCENTAGE,\n RETURNING_VISITOR_PERCENTAGE,\n SWDMV3_RATINGS,\n SWDMV4_RATINGS,\n} from \"../constants/index.js\";\n\nconst SWDM4_GLOBAL_GRID_INTENSITY = SWDV4.GLOBAL_GRID_INTENSITY;\n// Shared type definitions to be used across different files\n\n/**\n * @typedef {Object} DomainCheckOptions options to control the behavior when checking a domain\n * @property {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @property {boolean} verbose - Optional. Whether to return a verbose response.\n * @property {string[]} db - Optional. A database list to use for lookups.\n */\n\nconst formatNumber = (num) => parseFloat(num.toFixed(2));\n\nconst lessThanEqualTo = (num, limit) => num <= limit;\n\nfunction parseByteTraceOptions(options = {}, version = 3, green = false) {\n const globalGridIntensity =\n version === 4 ? SWDM4_GLOBAL_GRID_INTENSITY : SWDM3_GLOBAL_GRID_INTENSITY;\n // CHeck that it is an object\n if (typeof options !== \"object\") {\n throw new Error(\"Options must be an object\");\n }\n\n const adjustments = {};\n\n /**\n *\n * @param {string} segment The name of the segment (\"device\"|\"dataCenter\"|\"network\")\n * @param {number|object} segmentIntensity The segment intensity\n */\n function setIntensity(segment, segmentIntensity) {\n if (segmentIntensity || segmentIntensity === 0) {\n if (typeof segmentIntensity === \"object\") {\n if (!averageIntensity.data[segmentIntensity.country?.toUpperCase()]) {\n console.warn(\n `\"${segmentIntensity.country}\" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \\nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \\nFalling back to global average grid intensity.`\n );\n adjustments.gridIntensity[segment] = {\n value: globalGridIntensity,\n };\n }\n adjustments.gridIntensity[segment] = {\n country: segmentIntensity.country,\n value: parseFloat(\n averageIntensity.data[segmentIntensity.country?.toUpperCase()]\n ),\n };\n } else if (typeof segmentIntensity === \"number\") {\n adjustments.gridIntensity[segment] = {\n value: segmentIntensity,\n };\n } else {\n adjustments.gridIntensity[segment] = {\n value: globalGridIntensity,\n };\n console.warn(\n `The ${segment} grid intensity must be a number or an object. You passed in a ${typeof segmentIntensity}. \\nFalling back to global average grid intensity.`\n );\n }\n } else {\n adjustments.gridIntensity[segment] = {\n value: globalGridIntensity,\n };\n }\n }\n\n if (options?.gridIntensity) {\n adjustments.gridIntensity = {};\n const { device, dataCenter, network } = options.gridIntensity;\n setIntensity(\"device\", device);\n setIntensity(\"dataCenter\", dataCenter);\n setIntensity(\"network\", network);\n } else {\n adjustments.gridIntensity = {\n device: { value: globalGridIntensity },\n dataCenter: { value: globalGridIntensity },\n network: { value: globalGridIntensity },\n };\n }\n\n if (\n options?.greenHostingFactor ||\n (options.greenHostingFactor === 0 && version === 4)\n ) {\n if (typeof options.greenHostingFactor === \"number\") {\n if (options.greenHostingFactor >= 0 && options.greenHostingFactor <= 1) {\n adjustments.greenHostingFactor = options.greenHostingFactor;\n } else {\n adjustments.greenHostingFactor = 0;\n console.warn(\n `The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.greenHostingFactor = 0;\n console.warn(\n `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else if (version === 4) {\n adjustments.greenHostingFactor = 0;\n }\n\n if (green) {\n adjustments.greenHostingFactor = 1;\n }\n\n return adjustments;\n}\n\nfunction parseVisitTraceOptions(options = {}, version = 3, green = false) {\n // CHeck that it is an object\n if (typeof options !== \"object\") {\n throw new Error(\"Options must be an object\");\n }\n\n const adjustments = parseByteTraceOptions(options, version, green);\n\n if (options?.dataReloadRatio || options.dataReloadRatio === 0) {\n if (typeof options.dataReloadRatio === \"number\") {\n if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) {\n adjustments.dataReloadRatio = options.dataReloadRatio;\n } else {\n adjustments.dataReloadRatio =\n version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\n console.warn(\n `The dataReloadRatio option must be a number between 0 and 1. You passed in ${options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.dataReloadRatio =\n version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\n console.warn(\n `The dataReloadRatio option must be a number. You passed in a ${typeof options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.dataReloadRatio =\n version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\n console.warn(\n `The dataReloadRatio option must be a number. You passed in a ${typeof options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n\n if (options?.firstVisitPercentage || options.firstVisitPercentage === 0) {\n if (typeof options.firstVisitPercentage === \"number\") {\n if (\n options.firstVisitPercentage >= 0 &&\n options.firstVisitPercentage <= 1\n ) {\n adjustments.firstVisitPercentage = options.firstVisitPercentage;\n } else {\n adjustments.firstVisitPercentage =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\n console.warn(\n `The firstVisitPercentage option must be a number between 0 and 1. You passed in ${options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.firstVisitPercentage =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\n console.warn(\n `The firstVisitPercentage option must be a number. You passed in a ${typeof options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.firstVisitPercentage =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\n console.warn(\n `The firstVisitPercentage option must be a number. You passed in a ${typeof options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n\n if (options?.returnVisitPercentage || options.returnVisitPercentage === 0) {\n if (typeof options.returnVisitPercentage === \"number\") {\n if (\n options.returnVisitPercentage >= 0 &&\n options.returnVisitPercentage <= 1\n ) {\n adjustments.returnVisitPercentage = options.returnVisitPercentage;\n } else {\n adjustments.returnVisitPercentage =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\n console.warn(\n `The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.returnVisitPercentage =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\n console.warn(\n `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.returnVisitPercentage =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\n console.warn(\n `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n\n return adjustments;\n}\n\n/**\n * Returns an object containing all the HTTP headers to use when making a request to the Green Web Foundation API.\n * @param {string} comment - Optional. The app, site, or organisation that is making the request.\n *\n * @returns {import('http').OutgoingHttpHeaders}\n */\nfunction getApiRequestHeaders(comment = \"\") {\n return { \"User-Agent\": `co2js/${process.env.CO2JS_VERSION} ${comment}` };\n}\n\n/**\n * Returns the SWDM rating for a given CO2e value and version of the SWDM.\n * @param {number} co2e - The CO2e value to rate.\n * @param {number} swdmVersion - The version of the SWDM to use. Defaults to version 3.\n * @returns {string} The SWDM rating.\n */\nfunction outputRating(co2e, swdmVersion) {\n let {\n FIFTH_PERCENTILE,\n TENTH_PERCENTILE,\n TWENTIETH_PERCENTILE,\n THIRTIETH_PERCENTILE,\n FORTIETH_PERCENTILE,\n FIFTIETH_PERCENTILE,\n } = SWDMV3_RATINGS;\n\n if (swdmVersion === 4) {\n FIFTH_PERCENTILE = SWDMV4_RATINGS.FIFTH_PERCENTILE;\n TENTH_PERCENTILE = SWDMV4_RATINGS.TENTH_PERCENTILE;\n TWENTIETH_PERCENTILE = SWDMV4_RATINGS.TWENTIETH_PERCENTILE;\n THIRTIETH_PERCENTILE = SWDMV4_RATINGS.THIRTIETH_PERCENTILE;\n FORTIETH_PERCENTILE = SWDMV4_RATINGS.FORTIETH_PERCENTILE;\n FIFTIETH_PERCENTILE = SWDMV4_RATINGS.FIFTIETH_PERCENTILE;\n }\n\n if (lessThanEqualTo(co2e, FIFTH_PERCENTILE)) {\n return \"A+\";\n } else if (lessThanEqualTo(co2e, TENTH_PERCENTILE)) {\n return \"A\";\n } else if (lessThanEqualTo(co2e, TWENTIETH_PERCENTILE)) {\n return \"B\";\n } else if (lessThanEqualTo(co2e, THIRTIETH_PERCENTILE)) {\n return \"C\";\n } else if (lessThanEqualTo(co2e, FORTIETH_PERCENTILE)) {\n return \"D\";\n } else if (lessThanEqualTo(co2e, FIFTIETH_PERCENTILE)) {\n return \"E\";\n } else {\n return \"F\";\n }\n}\n\nexport {\n formatNumber,\n parseByteTraceOptions,\n parseVisitTraceOptions,\n getApiRequestHeaders,\n lessThanEqualTo,\n outputRating,\n};\n"], "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,eAAiC;AACjC,uBAQO;AAEP,MAAM,8BAA8B,uBAAM;AAU1C,MAAM,eAAe,CAAC,QAAQ,WAAW,IAAI,QAAQ,CAAC,CAAC;AAEvD,MAAM,kBAAkB,CAAC,KAAK,UAAU,OAAO;AAE/C,SAAS,sBAAsB,UAAU,CAAC,GAAG,UAAU,GAAG,QAAQ,OAAO;AACvE,QAAM,sBACJ,YAAY,IAAI,8BAA8B,iBAAAA;AAEhD,MAAI,OAAO,YAAY,UAAU;AAC/B,UAAM,IAAI,MAAM,2BAA2B;AAAA,EAC7C;AAEA,QAAM,cAAc,CAAC;AAOrB,WAAS,aAAa,SAAS,kBAAkB;AAxCnD;AAyCI,QAAI,oBAAoB,qBAA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