UNPKG

@tgwf/co2

Version:
167 lines (166 loc) 8.06 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var helpers_exports = {}; __export(helpers_exports, { formatNumber: () => formatNumber, getApiRequestHeaders: () => getApiRequestHeaders, parseOptions: () => parseOptions }); module.exports = __toCommonJS(helpers_exports); var import__ = require("../index.js"); var import_constants = require("../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 (!import__.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: import_constants.GLOBAL_GRID_INTENSITY }; } adjustments.gridIntensity["device"] = { country: device.country, value: parseFloat(import__.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: import_constants.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 (!import__.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: import_constants.GLOBAL_GRID_INTENSITY }; } adjustments.gridIntensity["dataCenter"] = { country: dataCenter.country, value: parseFloat(import__.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: import_constants.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 (!import__.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: import_constants.GLOBAL_GRID_INTENSITY }; } adjustments.gridIntensity["network"] = { country: network.country, value: parseFloat(import__.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: import_constants.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 = import_constants.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 = import_constants.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 = import_constants.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 = import_constants.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 = import_constants.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 = import_constants.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}` }; } //# sourceMappingURL=index.js.map