All files meal.js

100% Statements 20/20
100% Branches 9/9
100% Functions 2/2
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                                9x 9x 9x 9x 9x 9x 9x 9x 9x                     9x 9x 459x 180x 180x 98x 98x 3x   95x         9x     1x
/**
 * Meal class. Wraps meal data from TheMealDB API
 *
 * @module meal
 */
 
'use strict';
 
/** Meal class. Wraps data more cleanly for meals obtained from TheMealDB API. */
class Meal {
  /**
   * Wraps data more cleanly for meals obtained from TheMealDB API
   *
   * @param mealData {Object} Raw meal data from TheMealDB
   */
  constructor(mealData) {
    this.name = mealData.strMeal;
    this.id = mealData.idMeal;
    this.region = mealData.strArea;
    this.category = mealData.strCategory;
    this.ingredients = getIngredients(mealData);
    this.imageUrl = mealData.strMealThumb;
    this.tags = mealData.strTags;
    this.instructions = mealData.strInstructions;
    this.video = mealData.strYoutube;
  }
}
 
/**
 * Parse individual ingredients from a meal with the amount needed
 *
 * @param mealData {Object} Raw meal data from TheMealDB
 * @returns {Object} Object where Key: ingredient, Value: amount
 */
function getIngredients(mealData) {
  let ingredients = {};
  for (const property in mealData) {
    if (property.includes("strIngredient")) {
      const ingredient = mealData[property];
      if (ingredient !== null && typeof ingredient !== "undefined" && ingredient !== "") {
        const amountKey = property.replace("strIngredient", "strMeasure");
        if (ingredient in ingredients) {
          ingredients[ingredient] += ", " + mealData[amountKey];
        } else {
          ingredients[ingredient] = mealData[amountKey];
        }
      }
    }
  }
  return ingredients;
}
 
module.exports = Meal;