nutri-calc

OSS

The Health Star Rating algorithm as a published TypeScript package.

library

Get it
install
npm install nutri-calc

The real Health Star Rating used in Australia and New Zealand, implemented from the standard and published to npm. The whole thing is one function:

calculateHealthStarRating(category, {
  energykJ,
  saturatedFatGrams,
  totalSugarsGrams,
  sodiumMilligrams,
  percentageFruitVegetableNutLegume,
  fibreGrams,
  proteinGrams,
});
// → a rating from 0.5 to 5, or null

What the calculation actually does

It scores baseline points from the risk nutrients (energy, saturated fat, sugars, sodium), then offsets that with the good stuff: protein, fibre, and the fruit-vegetable-nut-legume percentage. The category matters, because the standard special-cases plain water, cheese, and fats and oils with their own rules. Get a rule wrong in a regulated product and it is not a styling bug, it is a compliance problem, so the math lives in one tested place and every project gets the same answer.

now runningwhisper_scheduleopen