@gram-lang/analyzer
Enriches a CompilationResult (from @gram-lang/kitchen) with physical properties — standardized mass, purchasing yield, nutrition estimates, and baker’s percentages — by cross-referencing an ingredient database. This is the only stage in the pipeline that requires a database; parsing and compilation operate purely on any .gram source text without any external dependencies.
analyze
Section titled “analyze”The ingredient database is the second positional argument, not a field on
options—analyze(compiled, database, options?).
analyze() is a pure function that never mutates its result argument. AnalyzedCompilationResult extends CompilationResult — same shape, plus per-usage normalizedMass, conversionMethod, isEstimate, purchasingMass, and bakersPercentage fields, along with a metrics.nutrition block. See Data formats for a fully annotated example.
AnalyzerOptions
Section titled “AnalyzerOptions”All enrichment flags default to enabled (via internal !== false checks) — pass explicit false to opt out of a given enrichment pass.
| Option | Type | Description |
|---|---|---|
enableMassStandardization | boolean | Convert ingredient quantities into standardized grams. |
enableYieldCalculation | boolean | Apply the ingredient database’s physical.yield (waste/prep factor) when standardizing mass. |
enableNutritionalEstimation | boolean | Compute metrics.nutrition (calories and macronutrients, globally and per-portion). |
enableBakersMath | boolean | Compute bakersPercentage relative to the *-marked (or bakersReference) ingredient. |
bakersReference | string | Explicit ingredient id to use as the 100% baker’s-percentage base, instead of the * modifier. |
portions | number | Overrides the recipe’s own portions: frontmatter as the divisor for metrics.nutrition.perPortion. |
lang | string | Optional locale code (e.g. 'en', 'fr') for locale-scoped unit normalization and category sorting. |
validateIngredientDatabase
Section titled “validateIngredientDatabase”Validates entry-by-entry rather than all-or-nothing: one malformed ingredient does not prevent valid entries from loading. Pass data to the pipeline and surface rejected issues to the user (e.g. via gram db validate). See Data formats for the IngredientData YAML schema.
Mass utilities
Section titled “Mass utilities”standardizeMass resolves a quantity to grams via the following priority cascade: (1) direct mass units (g, kg, oz…), (2) volume units converted through the ingredient’s resolved density, (3) count units (clove, slice…) converted via the ingredient’s unit_weight. It returns null — never guessing blindly — when a volume unit has no resolvable density.
convertUnit converts between two arbitrary unit strings, bridging mass↔volume families when a density (expressed in g/mL) is provided. It returns null when a cross-family conversion is impossible without density data.
Mass & volume conversion table
Section titled “Mass & volume conversion table”The default base conversion factors used by standardizeMass and convertUnit for same-family conversions (mass↔mass, volume↔volume), prior to any ingredient density application:
| Family | Unit | Factor (relative to base) |
|---|---|---|
mass (base: g) | mg | 0.001 |
mass (base: g) | g | 1 |
mass (base: g) | kg | 1000 |
mass (base: g) | oz | 28.3495 |
mass (base: g) | lb | 453.592 |
mass (base: g) | livre | 500 |
volume (base: ml) | ml | 1 |
volume (base: ml) | cl | 10 |
volume (base: ml) | dl | 100 |
volume (base: ml) | l | 1000 |
volume (base: ml) | drop | 0.078 |
volume (base: ml) | smidgen | 0.156 |
volume (base: ml) | pinch | 0.3125 |
volume (base: ml) | dash | 0.625 |
volume (base: ml) | tad | 1.25 |
volume (base: ml) | tsp | 4.9289 |
volume (base: ml) | tbsp | 14.7868 |
volume (base: ml) | cup | 236.588 |
volume (base: ml) | tasse | 250 |
volume (base: ml) | pt | 473.176 |
volume (base: ml) | qt | 946.353 |
volume (base: ml) | gal | 3785.41 |
volume (base: ml) | fl oz | 29.5735 |
calculateNutrition
Section titled “calculateNutrition”Flattens composite ingredients and alternatives (taking the first option), skips optional-modifier and zero-quantity items, and sums nutrition values (declared per 100g in the database — see Data formats) scaled by each ingredient’s standardized mass. Returns isEstimate: true if any contributing mass was itself an estimate, and a coverage ratio (0–1: fraction of ingredients with known nutrition profiles).
NutritionMetrics provides the nutritional profile across three complementary bases:
Every basis is derived directly from unrounded raw sums and rounded independently, preventing compounding rounding discrepancies between the total and individual portions. The basis property represents the mass denominator used for the per100g calculation — see Nutritional estimation for details on why this distinction matters when no cooking loss is modelled.
The list of tracked nutrients, their units, and their precision rules are defined in a single exported NUTRIENTS table, ensuring strict parity between gram db enrich, gram db lint, gram db search, editor hover hints, and all visual renderers.
diffRecipes
Section titled “diffRecipes”Computes the structural diff between two compiled recipes (e.g. two revisions of the same recipe file, or an unscaled baseline compared to its scaled counterpart). It tracks changes in ingredient quantities and units (with percentChange), preparation updates, schedule deltas (totalTime, idleTime, activeTime, preparationTime), added, removed, or modified sections, timer and temperature adjustments, and frontmatter (meta) changes. The hasChanges property evaluates to true whenever any diff category is non-empty.
The timing deltas compare durations from the default schedule (schedule): totalTime, idleTime, activeTime, and preparationTime all originate from schedule. An active timer defined as a range (~{20-25min}) counts on its longest bound for safety. These baseline metrics are identical on both sides of the comparison and match the default layout shown in gram view.
A section spliced in from a modular @use import (ProcessedSection.module populated) is isolated from step-level section comparisons — preventing a single imported module from offsetting subsequent section indices and reporting false-positive diffs. Its import statement is compared independently by (uri, binding, scale factor) inside the modules: ModuleDelta[] array — any re-bound or rescaled module is flagged with "changed".