ingredientcalc 0.1.0
A small, dependency-free D library for recipe ingredient math: cup/tablespoon/teaspoon/ml to grams by ingredient density, ounce-to-gram, and recipe scaling by servings.
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
ingredientcalc
A small, dependency-free D library for recipe ingredient conversion — turning the cups, tablespoons, and teaspoons in a recipe into grams (and back), and scaling a whole recipe to a new serving count.
A cup of flour and a cup of honey weigh very different amounts, so the volume conversions are driven by a real per-ingredient density table (grams per US cup). The worked examples mirror the model used at ingredientcalculator.com.
Why
Home and professional bakers constantly move between two measurement systems: volume (US cups/tbsp/tsp) and weight (grams/ounces). A gram scale is far more repeatable than a measuring cup, but most recipes are written in volume. The correct conversion depends on the ingredient:
grams = cups × density(ingredient) // 1 cup flour = 120 g, 1 cup sugar = 200 g
This library ships the density table, the volume↔weight conversions, the
ounce↔gram axis, and recipe scaling by servings — everything @safe pure and
dependency-free.
Install
Add it to your dub.json:
"dependencies": {
"ingredientcalc": "~>0.1.0"
}
Usage
import ingredientcalc;
import std.stdio;
void main()
{
// "2 cups flour, 1 cup sugar, 1/2 cup butter" in grams.
writeln("flour: ", cupsToGrams(2.0, Ingredient.allPurposeFlour)); // 240 g
writeln("sugar: ", cupsToGrams(1.0, Ingredient.granulatedSugar)); // 200 g
writeln("butter: ", cupsToGrams(0.5, Ingredient.butter)); // 113.5 g
// Tablespoons / teaspoons of a named ingredient:
writeln("1 tbsp honey: ", tbspToGrams(1.0, Ingredient.honey), " g");
// Ounce ↔ gram weight axis:
writeln("8 oz: ", ouncesToGrams(8.0), " g");
// Scale a 12-cookie batch up to 18:
writeln("flour for 18: ", scaleQuantity(240.0, 12.0, 18.0), " g"); // 360 g
}
API
| Symbol | Description |
|---|---|
densityGramsPerCup(ing) | Grams per US cup for an Ingredient (the density table). |
cupsToGrams(cups, ing) / gramsToCups(grams, ing) | Cup ↔ gram for a named ingredient. |
tbspToGrams(tbsp, ing) / tspToGrams(tsp, ing) | Tablespoon / teaspoon → grams (16 tbsp, 48 tsp per cup). |
mlToGrams(ml, gramsPerCup) / gramsToMl(grams, gramsPerCup) | Millilitre ↔ gram for any density. |
ouncesToGrams(oz) / gramsToOunces(g) | Avoirdupois ounce ↔ gram (1 oz = 28.3495 g). |
scaleQuantity(amount, fromServings, toServings) | Rescale one amount by a servings ratio. |
Ingredient density table
Grams per US customary cup (236.588 mL), following common culinary references:
| Ingredient | g/cup | Ingredient | g/cup | |
|---|---|---|---|---|
water | 236.59 | butter | 227 | |
milk | 244 | vegetableOil | 218 | |
allPurposeFlour | 120 | honey | 340 | |
breadFlour | 127 | mapleSyrup | 322 | |
cakeFlour | 114 | cocoaPowder | 85 | |
wholeWheatFlour | 120 | rolledOats | 90 | |
granulatedSugar | 200 | whiteRice | 185 | |
brownSugar | 220 | tableSalt | 273 | |
powderedSugar | 120 | bakingPowder | 221 |
Build & test
dub build
dub test
Requires a D compiler (DMD or LDC) and DUB. Every public
function is @safe pure and the package has no dependencies.
License
MIT — see LICENSE.
- 0.1.0 released 8 hours ago
- theluckystrike/ingredientcalc
- ingredientcalculator.com
- MIT
- Copyright © 2026, ingredientcalculator
- Authors:
- Dependencies:
- none
- Versions:
-
Show all 2 versions0.1.0 2026-Jul-16 ~main 2026-Jul-16 - Download Stats:
-
-
1 downloads today
-
1 downloads this week
-
1 downloads this month
-
1 downloads total
-
- Score:
- 0.0
- Short URL:
- ingredientcalc.dub.pm