<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SB Calc</title>
    <description>The latest articles on DEV Community by SB Calc (@sbcalc).</description>
    <link>https://dev.to/sbcalc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4206965%2F3f39fedb-8d30-4dfc-a94a-2fad610ede83.png</url>
      <title>DEV Community: SB Calc</title>
      <link>https://dev.to/sbcalc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sbcalc"/>
    <language>en</language>
    <item>
      <title>Building a Customizable Drink Nutrition Calculator: Modeling Size, Milk, Syrups and Toppings</title>
      <dc:creator>SB Calc</dc:creator>
      <pubDate>Sun, 25 Oct 2026 16:43:12 +0000</pubDate>
      <link>https://dev.to/sbcalc/building-a-customizable-drink-nutrition-calculator-modeling-size-milk-syrups-and-toppings-1fam</link>
      <guid>https://dev.to/sbcalc/building-a-customizable-drink-nutrition-calculator-modeling-size-milk-syrups-and-toppings-1fam</guid>
      <description>&lt;p&gt;Most nutrition tools treat food as a fixed item: one product, one label. Coffee drinks break that model immediately. A single latte can be ordered in multiple sizes, with a handful of milk options, any number of syrup pumps, and optional toppings. That's a combinatorial explosion if you try to store every variant.&lt;/p&gt;

&lt;p&gt;This post walks through the approach we used for a coffee-drink nutrition calculator and the trade-offs behind it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't store variants, store a base plus deltas
&lt;/h2&gt;

&lt;p&gt;Instead of a row per combination, each drink gets a &lt;strong&gt;base recipe&lt;/strong&gt; for a reference size and default milk. Every customization is then a &lt;strong&gt;delta&lt;/strong&gt; applied on top:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Nutrients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;calories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;fat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;carbs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;sugar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;protein&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;caffeine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;DrinkBase&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SizeId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Nutrients&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// published values per size&lt;/span&gt;
  &lt;span class="nl"&gt;defaultMilk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MilkId&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;defaultPumps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SizeId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Modifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;perUnit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Nutrients&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// e.g. one syrup pump, one topping serving&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using published per-size values for the base (rather than scaling one size mathematically) matters. Cup sizes don't scale linearly in practice: ice displaces liquid in cold drinks, espresso shots step up in jumps, and some sizes get extra pumps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Milk is a substitution, not an addition
&lt;/h2&gt;

&lt;p&gt;Syrups and toppings are additive: add &lt;code&gt;n * perUnit&lt;/code&gt;. Milk is different. Swapping 2% for almond means &lt;em&gt;removing&lt;/em&gt; the default milk's contribution and adding the new one. So each drink also stores an approximate milk volume per size, and the swap becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;milkNutrients&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultMilk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;vol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;milkNutrients&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;selectedMilk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;vol&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This keeps the math honest for drinks where milk dominates (lattes, cappuccinos) and nearly irrelevant for drinks where it doesn't (Americanos, brewed coffee).&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep the UI reactive and the data boring
&lt;/h2&gt;

&lt;p&gt;The calculator UI is just four controls: drink, size, milk, and a list of syrups and toppings with quantity steppers. Each change recomputes the total in a pure function, so there's no server round trip, and the result can be cached and shared via URL params.&lt;/p&gt;

&lt;p&gt;The data layer is deliberately boring: static JSON generated from published nutrition information, versioned in the repo, with a changelog entry whenever values are updated. Static pages per drink also mean every drink has a crawlable, fast detail page, which helps both users and search engines.&lt;/p&gt;

&lt;h2&gt;
  
  
  Edge cases worth handling
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sugar-free syrups&lt;/strong&gt; need their own modifier entries rather than a "sugar = 0" flag, since they still carry small amounts of other nutrients.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cold foam and sweet cream&lt;/strong&gt; are toppings with real volume; treat them as modifiers with a serving size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clamp inputs.&lt;/strong&gt; Negative pumps or 40 pumps of caramel shouldn't silently produce nonsense.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  See it running
&lt;/h2&gt;

&lt;p&gt;If you want to poke at a live implementation of this model, &lt;a href="https://starbucks-calorie-calculators.com" rel="noopener noreferrer"&gt;Starbucks Calorie Calculators&lt;/a&gt; is a working example of this kind of configurable model: pick a drink, change the size or milk, add pumps, and watch the totals update instantly.&lt;/p&gt;

&lt;p&gt;The broader lesson applies to any configurable product, from pizza builders to meal kits: model the base, model the deltas, and let a pure function do the rest.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
