UNPKG

@tgwf/co2

Version:
152 lines (151 loc) 6.95 kB
import { averageIntensity } from "../index.js"; import { GLOBAL_GRID_INTENSITY, PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD, FIRST_TIME_VIEWING_PERCENTAGE, RETURNING_VISITOR_PERCENTAGE } from "../constants/index.js"; const formatNumber = (num) => parseFloat(num.toFixed(2)); function parseOptions(options) { var _a, _b, _c, _d, _e, _f; if (typeof options !== "object") { throw new Error("Options must be an object"); } const adjustments = {}; if (options == null ? void 0 : options.gridIntensity) { adjustments.gridIntensity = {}; const { device, dataCenter, network } = options.gridIntensity; if (device || device === 0) { if (typeof device === "object") { if (!averageIntensity.data[(_a = device.country) == null ? void 0 : _a.toUpperCase()]) { console.warn(`"${device.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.`); adjustments.gridIntensity["device"] = { value: GLOBAL_GRID_INTENSITY }; } adjustments.gridIntensity["device"] = { country: device.country, value: parseFloat(averageIntensity.data[(_b = device.country) == null ? void 0 : _b.toUpperCase()]) }; } else if (typeof device === "number") { adjustments.gridIntensity["device"] = { value: device }; } else { adjustments.gridIntensity["device"] = { value: GLOBAL_GRID_INTENSITY }; console.warn(`The device grid intensity must be a number or an object. You passed in a ${typeof device}. Falling back to global average grid intensity.`); } } if (dataCenter || dataCenter === 0) { if (typeof dataCenter === "object") { if (!averageIntensity.data[(_c = dataCenter.country) == null ? void 0 : _c.toUpperCase()]) { console.warn(`"${dataCenter.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.`); adjustments.gridIntensity["dataCenter"] = { value: GLOBAL_GRID_INTENSITY }; } adjustments.gridIntensity["dataCenter"] = { country: dataCenter.country, value: parseFloat(averageIntensity.data[(_d = dataCenter.country) == null ? void 0 : _d.toUpperCase()]) }; } else if (typeof dataCenter === "number") { adjustments.gridIntensity["dataCenter"] = { value: dataCenter }; } else { adjustments.gridIntensity["dataCenter"] = { value: GLOBAL_GRID_INTENSITY }; console.warn(`The data center grid intensity must be a number or an object. You passed in a ${typeof dataCenter}. Falling back to global average grid intensity.`); } } if (network || network === 0) { if (typeof network === "object") { if (!averageIntensity.data[(_e = network.country) == null ? void 0 : _e.toUpperCase()]) { console.warn(`"${network.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. Falling back to global average grid intensity.`); adjustments.gridIntensity["network"] = { value: GLOBAL_GRID_INTENSITY }; } adjustments.gridIntensity["network"] = { country: network.country, value: parseFloat(averageIntensity.data[(_f = network.country) == null ? void 0 : _f.toUpperCase()]) }; } else if (typeof network === "number") { adjustments.gridIntensity["network"] = { value: network }; } else { adjustments.gridIntensity["network"] = { value: GLOBAL_GRID_INTENSITY }; console.warn(`The network grid intensity must be a number or an object. You passed in a ${typeof network}. Falling back to global average grid intensity.`); } } } if ((options == null ? void 0 : options.dataReloadRatio) || options.dataReloadRatio === 0) { if (typeof options.dataReloadRatio === "number") { if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) { adjustments.dataReloadRatio = options.dataReloadRatio; } else { adjustments.dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD; console.warn(`The dataReloadRatio option must be a number between 0 and 1. You passed in ${options.dataReloadRatio}. Falling back to default value.`); } } else { adjustments.dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD; console.warn(`The dataReloadRatio option must be a number. You passed in a ${typeof options.dataReloadRatio}. Falling back to default value.`); } } if ((options == null ? void 0 : options.firstVisitPercentage) || options.firstVisitPercentage === 0) { if (typeof options.firstVisitPercentage === "number") { if (options.firstVisitPercentage >= 0 && options.firstVisitPercentage <= 1) { adjustments.firstVisitPercentage = options.firstVisitPercentage; } else { adjustments.firstVisitPercentage = FIRST_TIME_VIEWING_PERCENTAGE; console.warn(`The firstVisitPercentage option must be a number between 0 and 1. You passed in ${options.firstVisitPercentage}. Falling back to default value.`); } } else { adjustments.firstVisitPercentage = FIRST_TIME_VIEWING_PERCENTAGE; console.warn(`The firstVisitPercentage option must be a number. You passed in a ${typeof options.firstVisitPercentage}. Falling back to default value.`); } } if ((options == null ? void 0 : options.returnVisitPercentage) || options.returnVisitPercentage === 0) { if (typeof options.returnVisitPercentage === "number") { if (options.returnVisitPercentage >= 0 && options.returnVisitPercentage <= 1) { adjustments.returnVisitPercentage = options.returnVisitPercentage; } else { adjustments.returnVisitPercentage = RETURNING_VISITOR_PERCENTAGE; console.warn(`The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. Falling back to default value.`); } } else { adjustments.returnVisitPercentage = RETURNING_VISITOR_PERCENTAGE; console.warn(`The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. Falling back to default value.`); } } return adjustments; } function getApiRequestHeaders(comment = "") { return { "User-Agent": `co2js/${"0.15.0"} ${comment}` }; } export { formatNumber, getApiRequestHeaders, parseOptions };