Recipe.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. class RecipeIngredient{
  2. constructor(ingredient, quantity){
  3. this._ingredient = ingredient;
  4. this._quantity = quantity;
  5. }
  6. get ingredient(){
  7. return this._ingredient;
  8. }
  9. get quantity(){
  10. switch(this._ingredient.unit){
  11. case "g":return this._quantity;
  12. case "kg": return this._quantity / 1000;
  13. case "oz": return this._quantity / 28.3495;
  14. case "lb": return this._quantity / 453.5924;
  15. case "ml": return this._quantity * 1000;
  16. case "l": return this._quantity;
  17. case "tsp": return this._quantity * 202.8842;
  18. case "tbsp": return this._quantity * 67.6278;
  19. case "ozfl": return this._quantity * 33.8141;
  20. case "cup": return this._quantity * 4.1667;
  21. case "pt": return this._quantity * 2.1134;
  22. case "qt": return this._quantity * 1.0567;
  23. case "gal": return this._quantity / 3.7854;
  24. case "mm": return this._quantity * 1000;
  25. case "cm": return this._quantity * 100;
  26. case "m": return this._quantity;
  27. case "in": return this._quantity * 39.3701;
  28. case "ft": return this._quantity * 3.2808;
  29. default: return this._quantity;
  30. }
  31. }
  32. set quantity(quantity){
  33. this_quantity = this.convertToBase(quantity);
  34. }
  35. getQuantityDisplay(){
  36. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  37. }
  38. convertToBase(quantity){
  39. switch(this._ingredient.unit){
  40. case "g": return quantity;
  41. case "kg": return quantity * 1000;
  42. case "oz": return quantity * 28.3495;
  43. case "lb": return quantity * 453.5924;
  44. case "ml": return quantity / 1000;
  45. case "l": return quantity;
  46. case "tsp": return quantity / 202.8842;
  47. case "tbsp": return quantity / 67.6278;
  48. case "ozfl": return quantity / 33.8141;
  49. case "cup": return quantity / 4.1667;
  50. case "pt": return quantity / 2.1134;
  51. case "qt": return quantity / 1.0567;
  52. case "gal": return quantity * 3.7854;
  53. case "mm": return quantity / 1000;
  54. case "cm": return quantity / 100;
  55. case "m": return quantity;
  56. case "in": return quantity / 39.3701;
  57. case "ft": return quantity / 3.2808;
  58. default: return quantity;
  59. }
  60. }
  61. }
  62. /*
  63. Recipe Object
  64. id = database id of recipe
  65. name = name of recipe
  66. price = price of recipe in cents
  67. ingredients = [{
  68. ingredient: Ingredient Object,
  69. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  70. }]
  71. parent = merchant that it belongs to
  72. */
  73. class Recipe{
  74. constructor(id, name, price, ingredients, parent){
  75. this._id = id;
  76. this._name = name;
  77. this._price = price;
  78. this._parent = parent;
  79. this._ingredients = [];
  80. for(let i = 0; i < ingredients.length; i++){
  81. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  82. const recipeIngredient = new RecipeIngredient(
  83. ingredient.ingredient,
  84. ingredients[i].quantity
  85. );
  86. this._ingredients.push(recipeIngredient);
  87. }
  88. }
  89. get id(){
  90. return this._id;
  91. }
  92. get name(){
  93. return this._name;
  94. }
  95. set name(name){
  96. this._name = name;
  97. }
  98. get price(){
  99. return this._price / 100;
  100. }
  101. set price(price){
  102. this._price = price;
  103. }
  104. get parent(){
  105. return this._parent;
  106. }
  107. get ingredients(){
  108. return this._ingredients;
  109. }
  110. addIngredient(ingredient, quantity){
  111. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  112. this._ingredients.push(recipeIngredient);
  113. this._parent.modules.recipeBook.isPopulated = false;
  114. this._parent.modules.analytics.isPopulated = false;
  115. }
  116. removeIngredients(){
  117. this._ingredients = [];
  118. }
  119. }
  120. module.exports = Recipe;