Created
September 23, 2017 20:35
-
-
Save lukiffer/2d7d6859a4ae4da796404f8d6bbb1fa9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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