// Nutrients beyond the seven tracked metrics. Keys follow Open Food Facts // naming, so both product databases and label scans share one vocabulary. // Amounts are stored in the listed unit; keys not listed here are stored in // grams. export const nutrientCatalog = [ { key: "saturated-fat", label: "Saturated fat", unit: "g" }, { key: "trans-fat", label: "Trans fat", unit: "g" }, { key: "monounsaturated-fat", label: "Monounsaturated fat", unit: "g" }, { key: "polyunsaturated-fat", label: "Polyunsaturated fat", unit: "g" }, { key: "omega-3-fat", label: "Omega-3 fat", unit: "g" }, { key: "omega-6-fat", label: "Omega-6 fat", unit: "g" }, { key: "omega-9-fat", label: "Omega-9 fat", unit: "g" }, { key: "cholesterol", label: "Cholesterol", unit: "mg" }, { key: "added-sugars", label: "Added sugars", unit: "g" }, { key: "polyols", label: "Sugar alcohols", unit: "g" }, { key: "soluble-fiber", label: "Soluble fiber", unit: "g" }, { key: "insoluble-fiber", label: "Insoluble fiber", unit: "g" }, { key: "starch", label: "Starch", unit: "g" }, { key: "vitamin-a", label: "Vitamin A", unit: "µg" }, { key: "vitamin-a-iu", label: "Vitamin A (IU)", unit: "IU" }, { key: "vitamin-c", label: "Vitamin C", unit: "mg" }, { key: "vitamin-d", label: "Vitamin D", unit: "µg" }, { key: "vitamin-e", label: "Vitamin E", unit: "mg" }, { key: "vitamin-k", label: "Vitamin K", unit: "µg" }, { key: "vitamin-b1", label: "Thiamin", unit: "mg" }, { key: "vitamin-b2", label: "Riboflavin", unit: "mg" }, { key: "vitamin-pp", label: "Niacin", unit: "mg" }, { key: "vitamin-b6", label: "Vitamin B6", unit: "mg" }, { key: "vitamin-b9", label: "Folate", unit: "µg" }, { key: "folates", label: "Folate, total", unit: "µg" }, { key: "folic-acid", label: "Folic acid", unit: "µg" }, { key: "vitamin-b12", label: "Vitamin B12", unit: "µg" }, { key: "biotin", label: "Biotin", unit: "µg" }, { key: "pantothenic-acid", label: "Pantothenic acid", unit: "mg" }, { key: "choline", label: "Choline", unit: "mg" }, { key: "calcium", label: "Calcium", unit: "mg" }, { key: "iron", label: "Iron", unit: "mg" }, { key: "potassium", label: "Potassium", unit: "mg" }, { key: "magnesium", label: "Magnesium", unit: "mg" }, { key: "phosphorus", label: "Phosphorus", unit: "mg" }, { key: "zinc", label: "Zinc", unit: "mg" }, { key: "copper", label: "Copper", unit: "mg" }, { key: "manganese", label: "Manganese", unit: "mg" }, { key: "selenium", label: "Selenium", unit: "µg" }, { key: "iodine", label: "Iodine", unit: "µg" }, { key: "chromium", label: "Chromium", unit: "µg" }, { key: "molybdenum", label: "Molybdenum", unit: "µg" }, { key: "chloride", label: "Chloride", unit: "mg" }, { key: "fluoride", label: "Fluoride", unit: "mg" }, { key: "caffeine", label: "Caffeine", unit: "mg" }, { key: "taurine", label: "Taurine", unit: "mg" }, { key: "alcohol", label: "Alcohol", unit: "g" }, ]; // Where each tracked metric lives in a product's nutrient map. export const coreNutrientKeys = { calories: "energy-kcal", protein: "proteins", carbs: "carbohydrates", fat: "fat", fiber: "fiber", sugar: "sugars", sodium: "sodium", }; // Stored units for the tracked metrics inside a product's nutrient map. export const coreUnits = { "energy-kcal": "kcal", proteins: "g", carbohydrates: "g", fat: "g", fiber: "g", sugars: "g", sodium: "mg", }; const byKey = new Map(nutrientCatalog.map((item) => [item.key, item])); const gramsPer = { g: 1, mg: 1e-3, µg: 1e-6 }; export const nutrientKeyPattern = /^[a-z0-9][a-z0-9-]{0,47}$/; export function nutrientUnit(key) { return coreUnits[key] || byKey.get(key)?.unit || "g"; } /** Converts grams to the stored unit for `key`; null when it isn't a mass. */ export function fromGrams(key, grams) { const factor = gramsPer[nutrientUnit(key)]; return factor ? grams / factor : null; } export function convertMass(amount, from, to) { const a = gramsPer[from]; const b = gramsPer[to]; return a && b ? (amount * a) / b : null; } export function nutrientLabel(key) { if (byKey.has(key)) return byKey.get(key).label; const words = key.replace(/-/g, " "); return words.charAt(0).toUpperCase() + words.slice(1); } /** Catalog order first, then any other keys alphabetically. */ export function sortNutrientKeys(keys) { const order = new Map(nutrientCatalog.map((item, i) => [item.key, i])); return [...keys].sort( (a, b) => (order.get(a) ?? 1e3) - (order.get(b) ?? 1e3) || a.localeCompare(b), ); } /** A readable amount: grams of uncatalogued nutrients scale to mg or µg. */ export function displayAmount(key, amount) { let unit = nutrientUnit(key); let value = amount; if (!byKey.has(key) && !coreUnits[key] && value > 0 && value < 1) { unit = value < 0.001 ? "µg" : "mg"; value = convertMass(amount, "g", unit); } return { value, unit }; }