@gram-lang/i18n
Shared unit/time normalization and UI-string dictionaries, used internally across the pipeline (the parser doesn’t normalize units — that happens in @gram-lang/kitchen’s scaling engine and @gram-lang/analyzer’s mass standardization) so "càs", "tbsp", and "cuillère à soupe" are all recognized as the same unit regardless of which locale a recipe or a database entry was written in.
Canonical units & aliases
Section titled “Canonical units & aliases”normalizeUnit maps any recognized alias (English or French, case-insensitive) to its canonical short form:
| Canonical | Aliases |
|---|---|
tsp | teaspoon, teaspoons |
tbsp | tablespoon, tablespoons |
tad | tads |
dash | dashes |
pinch | pinches |
smidgen | smidgen, smidge, smidgens |
drop | drops |
cup | cups |
pt | pint, pints |
qt | |
gal | gallon, gallons |
fl oz | floz, fl. oz., fluid ounce, fluid ounces |
ml | milliliter, milliliters |
cl | centiliter, centiliters |
dl | deciliter, deciliters |
l | liter, liters |
mg | milligram, milligrams |
g | gram, grams |
kg | kilo, kilos, kilogram, kilograms |
oz | ounce, ounces |
lb | lbs, pound, pounds |
Unrecognized input is returned lowercased and trimmed, unchanged.
Time units
Section titled “Time units”Same alias-normalization pattern, scoped to durations:
| Canonical | Aliases |
|---|---|
d | day, days |
h | hr, hrs, hour, hours |
m | min, mins, minute, minutes |
s | sec, secs, second, seconds |
Parsing a duration
Section titled “Parsing a duration”parseDuration parses timer durations (1h30, 1 hr 30 mins, 1:30 h, 5'30", 45 min - 1 h) into minutes across every language in TIME_DICTIONARIES. Unrecognized inputs return a failure reason—which @gram-lang/kitchen maps to MISSING_UNIT, INVALID_UNIT, or INVALID_DURATION—without guessing. Each side preserves its parsed parts and form (units, clock, or prime), with sign and upper-bound checks left to the caller.
The reasons (DurationFailure) are listed in the source, each with an example: empty, missingUnit, unknownUnit, words, multiplication, iso8601, unexpected, ambiguousClock, clockFormat, unitOrder, duplicateUnit, overflow60, decimalInCompound, decimalPrime, bareNumber, reversedRange, emptyRange, ambiguousRange. See Writing a duration for the forms themselves.
Default ingredient categories
Section titled “Default ingredient categories”Used by gram shop and other consumers to group a shopping list by culinary family when an ingredient’s database entry has no explicit category:
| Default categories |
|---|
| Vegetables |
| Fruits |
| Meat |
| Seafood |
| Dairy |
| Grains |
| Legumes |
| Nuts |
| Oils |
| Herbs |
| Spices |
| Condiments |
| Sugars |
| Beverages |
| Other |
CATEGORY_KEYS exports stable, locale-invariant category keys (produce, dairy, meat, pantry, …), and getCategoryLabels(lang) resolves those keys to human-readable localized labels:
Unit conversion tables
Section titled “Unit conversion tables”@gram-lang/i18n acts as the single source of truth for same-family unit conversion factors (UNIT_CONVERSIONS) and time-to-minute duration multipliers (TIME_TO_MINUTES):
Numbers in the reader’s language
Section titled “Numbers in the reader’s language”formatNumber writes a number as a reader of the language does (1.5 in English, 1,5 in French), rounded to two decimals by default and with no thousands separator, as a recipe writes 1500 g. localizeDecimals does the same for a quantity already written as text (a range, a timer), and decimalSeparator gives the separator itself. Every view and the CLI write the numbers they show through these; a number read back by a script or a file (a data-* attribute, JSON, .gram source) keeps its point.
A quantity is shown with formatAmount, which rounds it to its size rather than to a decimal of its unit: three significant digits, never fewer than its whole part, and two below 1, so a small amount is never shown as 0 (0.15 kg stays 0.15 kg, not 0.2 kg). roundAmount gives the rounded number, amountDecimals the number of decimals kept.
Spoons, cups and imperial measures (tsp, tbsp, cup, tasse, oz, lb, pt…) are read in fractions: toKitchenFraction writes such a quantity as a whole number and a common fraction, as a cook writes it, so 1.75 cup and 1 3/4 cup both show 1 3/4 cup. It returns undefined when no common fraction is near (1.6) and for any other unit: metric units and counts keep decimals. isFractionUnit tells whether a unit is one of them, commonFraction gives the fraction of a value below 1.
Large amounts and metric weights
Section titled “Large amounts and metric weights”toLargerMetricUnit converts a metric mass (mg, g) or volume (ml, cl, dl) to kg or l once it reaches 1000 g or 1000 ml (rounded to two decimal places), and returns any other amount unchanged. Gram uses it for what is bought or read as a total, never for what is weighed (see Kilograms to buy, grams to weigh). isMetricMassUnit checks whether a unit (including localized aliases) is a metric mass (mg, g, kg).
Sums of quantities
Section titled “Sums of quantities”Every quantity of the pipeline (a sum like {1 cup + 1 tbsp}, a plain {200 g}, the entries of a shopping list) goes through one model of terms: a value, a unit, and minus for a term taken away. Kitchen, the analyzer and the renderer all add up, scale and write quantities with these functions, so they always agree. The module is pure: it knows nothing about the AST or the compiled recipe.
UNIT_LADDERS: the units that convert by a whole ratio, each counted in its ladder’s smallest unit: US volume (tsp1,tbsp3,fl oz6,cup48,pt96,qt192,gal768), imperial mass (oz1,lb16), metric mass (mg,g,livre,kg) and metric volume (ml,cl,dl,tasse,l). They agree withUNIT_CONVERSIONSto 0.01%.pinch,dashand unknown units are on no ladder.termsTotal: one unit keeps its unit (1 tasse + 1 tassestays intasse, never converted); units of one ladder add up in the smallest unit written, with exact whole-number arithmetic; one family across systems gives grams or milliliters; anything else (a volume and a weight, unknown units) givesnull. Ranges add up bound by bound, and a total with a range stays a range.mergeTerms:"unit"adds up the terms of each unit (a run sheet’s kit),"ladder"the terms of each ladder (the shopping list). An amount never starts with a term taken away:formatTermskeeps the written order (1 cup − 2 tbsp + 30 g, each kind taking away on its own) and only moves the terms added first when it would (1 cup − 2 tbsp, never−2 tbsp + 1 cup).formatTerms:mode: "written"(default) writes the terms as the author did;mode: "computed"writes an amount Gram worked out the way a cook measures it (see scaling sums of quantities). Options:lang,buying(a shopping list: move up to the unit you buy),rounding(round to what a cook can measure, after scaling),plus/minusseparators, andnumber/textto write numbers your own way.