Skip to content

Instantly share code, notes, and snippets.

@lukiffer
Created September 23, 2017 20:35
Show Gist options
  • Save lukiffer/2d7d6859a4ae4da796404f8d6bbb1fa9 to your computer and use it in GitHub Desktop.
Save lukiffer/2d7d6859a4ae4da796404f8d6bbb1fa9 to your computer and use it in GitHub Desktop.
import { INutritionData } from '../models/nutrition-data.interface';
import { Ingredient } from '../models/ingredient';
import { RecipeIngredient } from '../models/recipe-ingredient';
import * as _ from 'lodash';
export class NutritionCalculatorService {
public calculate(ingredients: RecipeIngredient[]): INutritionData {
const result = new Ingredient(null, null);
const properties = ['calories', 'sodium'];
_.each(ingredients, (i): void => {
_.each(properties, (p): void => {
this.add(result, i, p);
});
});
return result;
}
private add(runningTotal: INutritionData, addition: RecipeIngredient, property: string): void {
runningTotal[property] += addition.quantity * addition[property];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment