1.0.2 • Published 4 years ago

@csejtei/nutrition v1.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

Nutrition calculator

Create Foods with base nutrition values, change one of the base values and recalculate other values automatically.

Installation

npm i @csejtei/nutrition

Example

import { Food, Nutritions, Units } from '@csejtei/nutrition';

const baseValues = {
    amount: 100,
    fat: 30,
    carbohydrate: 40,
    protein: 65,
    calories: 124
};
const food = new Food('rice', Units.GRAM, baseValues);

// returns 'rice'
food.getName();

// returns 'g'
food.getUnit();

// changes food amount value and store the result in the currentValues.amount
// there are also changeCalories, changeFat, changeCarbohydrate, changeCalories functions
food.changeAmount(23);

// get current nutrition values of the rice
const { calories, amount, 
fat, carbohydrate, 
protein } = food.getCurrentValues();

// get base nutrition values of the rice
const { calories, amount, 
fat, carbohydrate, 
protein } = food.getBaseValues();

Error handling

import { Food, Nutritions, 
Units, InvalidFoodAmountError } from '@csejtei/nutrition';

const baseValues = {
    amount: 0,
    fat: 30,
    carbohydrate: 40,
    protein: 65,
    calories: 124
};

try {
    const rice = new Food('rice', Units.GRAM, baseValues);
} catch(error) {
    //error: InvalidFoodAmountError
}

All errors types:

  • InvalidFoodAmountError -> if the amount of the baseValues is less or equal zero or the changeAmount parameter is less or equal zero
  • EmptyFoodNameError -> if the Food first parameter is empty
1.0.0

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago