@vim-crouwel/cfpc
Version:
TypeScript library for calculating CO2 emissions from food, energy, and transportation
59 lines (58 loc) • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.co2PerKg = co2PerKg;
exports.dataSource = dataSource;
exports.getFoodCategories = getFoodCategories;
exports.getFoodItems = getFoodItems;
const utils_1 = require("../utils");
const foodList = (0, utils_1.getFood)();
/**
* Calculate CO2 emissions for food consumption
* @param category - Food category
* @param label - Specific food item
* @param kg - Weight in kilograms
* @returns CO2 emissions in kg
*/
function co2PerKg(category, label, kg) {
checkCategoryAndLabel(category, label);
if (kg < 0)
throw new Error('Weight cannot be negative');
return foodList[category][label].co2_per_kg * kg;
}
/**
* Get data source for a specific food item
* @param category - Food category
* @param label - Specific food item
* @returns Data source reference
*/
function dataSource(category, label) {
checkCategoryAndLabel(category, label);
return foodList[category][label].source;
}
/**
* Get all available food categories
* @returns Array of food category names
*/
function getFoodCategories() {
return Object.keys(foodList);
}
/**
* Get all food items in a specific category
* @param category - Food category
* @returns Array of food item names
*/
function getFoodItems(category) {
const cat = foodList[category];
if (!cat)
throw new Error(`${category} does not exist.`);
return Object.keys(cat);
}
//UTILS
function checkCategoryAndLabel(category, label) {
const cat = foodList[category];
if (!cat)
throw new Error(`${category} does not exist.`);
const food = cat[label];
if (!food)
throw new Error(`${label} does not exist.`);
}