npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

brewing-water-calculator

v2.0.0

Published

Water chemistry calculator for brewing - calculates salt additions using quadratic programming

Readme

Brewing Water Calculator

A TypeScript library for calculating precise salt additions to achieve target water profiles in brewing. Uses quadratic programming to find optimal solutions that minimize deviations across all ions.

Features

  • Optimal Salt Calculations: Uses quadratic programming to find the best combination of brewing salts
  • Balanced Error Distribution: Minimizes squared deviations for even error distribution across all ions
  • Comprehensive Brewing Analysis: Calculates alkalinity, residual alkalinity, hardness, and sulfate/chloride balance
  • TypeScript Support: Fully typed for excellent IDE support
  • Production Ready: Battle-tested in production brewing applications

Installation

npm install brewing-water-calculator

Usage

import { calculateWaterAdditions } from 'brewing-water-calculator';

// Define your base water profile (in ppm)
const baseProfile = {
  Ca: 22,
  Mg: 2.7,
  Na: 12,
  SO4: 6,
  Cl: 14,
  HCO3: 50
};

// Define your target water profile (in ppm)
const targetProfile = {
  Ca: 48,
  Mg: 2.7,
  Na: 12,
  SO4: 60,
  Cl: 40,
  HCO3: 50
};

// Calculate salt additions for 20 liters
const result = await calculateWaterAdditions({
  volumeLiters: 20,
  baseProfile,
  targetProfile
});

console.log(result);
// {
//   success: true,
//   additions: {
//     "Gypsum (CaSO4)": 1.5,
//     "Calcium Chloride (CaCl2)": 0.8,
//     "Epsom Salt (MgSO4)": 0,
//     "Table Salt (NaCl)": 0.3,
//     "Baking Soda (NaHCO3)": 0
//   },
//   calculatedProfile: {
//     Ca: 48.2,
//     Mg: 2.7,
//     Na: 12.5,
//     SO4: 59.8,
//     Cl: 39.9,
//     HCO3: 50
//   },
//   deviations: {
//     Ca: 0.2,
//     Mg: 0,
//     Na: 0.5,
//     SO4: -0.2,
//     Cl: -0.1,
//     HCO3: 0
//   },
//   totalSaltWeight: 2.6,
//   brewingAnalysis: {
//     alkalinity: 41,
//     residualAlkalinity: 5.8,
//     sulfateChlorideRatio: 1.5,
//     sulfateChlorideBalance: "Slightly Bitter",
//     totalHardness: 50.9,
//     effectiveHardness: 35.9,
//     colorRange: "8-20 EBC (Pale)"
//   }
// }

API

calculateWaterAdditions(input)

Calculates the optimal salt additions to achieve a target water profile.

Parameters:

  • input (CalculationInput): Object containing:
    • volumeLiters (number): Volume of water in liters
    • baseProfile (WaterProfile): Starting water profile in ppm
    • targetProfile (WaterProfile): Desired water profile in ppm

Returns: Promise<CalculationResult>

  • success (boolean): Whether calculation succeeded
  • additions (SaltAdditions): Grams of each salt to add
  • calculatedProfile (WaterProfile): Achieved water profile
  • deviations (WaterProfile): Difference from target
  • totalSaltWeight (number): Total grams of salts
  • brewingAnalysis (BrewingAnalysis): Brewing-specific metrics

Type Interfaces

interface CalculationInput {
  volumeLiters: number;
  baseProfile: WaterProfile;
  targetProfile: WaterProfile;
}

interface WaterProfile {
  Ca: number;   // Calcium (ppm)
  Mg: number;   // Magnesium (ppm)
  Na: number;   // Sodium (ppm)
  SO4: number;  // Sulfate (ppm)
  Cl: number;   // Chloride (ppm)
  HCO3: number; // Bicarbonate (ppm)
}

Brewing Salts

The calculator uses these common brewing salts:

| Salt | Formula | Primary Contributions | |------|---------|----------------------| | Gypsum | CaSO₄·2H₂O | Calcium, Sulfate | | Calcium Chloride | CaCl₂·2H₂O | Calcium, Chloride | | Epsom Salt | MgSO₄·7H₂O | Magnesium, Sulfate | | Table Salt | NaCl | Sodium, Chloride | | Baking Soda | NaHCO₃ | Sodium, Bicarbonate |

Brewing Analysis Metrics

The calculator provides these brewing-specific metrics:

  • Alkalinity: Total alkalinity as CaCO₃ (ppm)
  • Residual Alkalinity: Effective alkalinity after calcium/magnesium compensation
  • Sulfate/Chloride Ratio: Balance indicator for hop vs malt character
  • Total Hardness: Ca + Mg (ppm)
  • Effective Hardness: Palmer's formula (Ca/1.4 + Mg/1.7)
  • Color Range: Suggested beer color based on residual alkalinity

Algorithm

The calculator uses quadratic programming to minimize the sum of squared deviations:

minimize: Σ(achieved[ion] - target[ion])²
subject to: salt_additions ≥ 0

This approach ensures:

  • Optimal solution that minimizes total error
  • Balanced error distribution across all ions
  • No negative salt additions
  • Mathematically proven optimal solution

Requirements

  • Node.js >= 16.0.0
  • TypeScript >= 5.0.0 (for development)

Development

# Install dependencies
npm install

# Build the package
npm run build

# Clean build directory
npm run clean

License

MIT © Scott Ryburn

Contributing

Issues and pull requests are welcome! Please feel free to submit bug reports or feature requests.

Links