nutri-calc
OSSThe Health Star Rating algorithm as a published TypeScript package.
library
install
npm install nutri-calcThe 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 nullWhat 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.