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

SymbolDescription
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:

Ingredientg/cupIngredientg/cup
water236.59butter227
milk244vegetableOil218
allPurposeFlour120honey340
breadFlour127mapleSyrup322
cakeFlour114cocoaPowder85
wholeWheatFlour120rolledOats90
granulatedSugar200whiteRice185
brownSugar220tableSalt273
powderedSugar120bakingPowder221

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.

Authors:
  • ingredientcalculator
Dependencies:
none
Versions:
0.1.0 2026-Jul-16
~main 2026-Jul-16
Show all 2 versions
Download Stats:
  • 1 downloads today

  • 1 downloads this week

  • 1 downloads this month

  • 1 downloads total

Score:
0.0
Short URL:
ingredientcalc.dub.pm