Recipe.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. class RecipeIngredient{
  2. constructor(ingredient, quantity){
  3. if(quantity < 0){
  4. controller.createBanner("QUANTITY CANNOT BE A NEGATIVE NUMBER", "error");
  5. return false;
  6. }
  7. this._ingredient = ingredient;
  8. this._quantity = quantity;
  9. }
  10. get ingredient(){
  11. return this._ingredient;
  12. }
  13. get quantity(){
  14. switch(this._ingredient.unit){
  15. case "g":return this._quantity;
  16. case "kg": return this._quantity / 1000;
  17. case "oz": return this._quantity / 28.3495;
  18. case "lb": return this._quantity / 453.5924;
  19. case "ml": return this._quantity * 1000;
  20. case "l": return this._quantity;
  21. case "tsp": return this._quantity * 202.8842;
  22. case "tbsp": return this._quantity * 67.6278;
  23. case "ozfl": return this._quantity * 33.8141;
  24. case "cup": return this._quantity * 4.1667;
  25. case "pt": return this._quantity * 2.1134;
  26. case "qt": return this._quantity * 1.0567;
  27. case "gal": return this._quantity / 3.7854;
  28. case "mm": return this._quantity * 1000;
  29. case "cm": return this._quantity * 100;
  30. case "m": return this._quantity;
  31. case "in": return this._quantity * 39.3701;
  32. case "ft": return this._quantity * 3.2808;
  33. default: return this._quantity;
  34. }
  35. }
  36. set quantity(quantity){
  37. if(quantity < 0){
  38. controller.createBanner("QUANTITY CANNOT BE A NEGATIVE NUMBER", "error");
  39. return false;
  40. }
  41. this_quantity = this.convertToBase(quantity);
  42. }
  43. getQuantityDisplay(){
  44. return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
  45. }
  46. convertToBase(quantity){
  47. switch(this._ingredient.unit){
  48. case "g": return quantity;
  49. case "kg": return quantity * 1000;
  50. case "oz": return quantity * 28.3495;
  51. case "lb": return quantity * 453.5924;
  52. case "ml": return quantity / 1000;
  53. case "l": return quantity;
  54. case "tsp": return quantity / 202.8842;
  55. case "tbsp": return quantity / 67.6278;
  56. case "ozfl": return quantity / 33.8141;
  57. case "cup": return quantity / 4.1667;
  58. case "pt": return quantity / 2.1134;
  59. case "qt": return quantity / 1.0567;
  60. case "gal": return quantity * 3.7854;
  61. case "mm": return quantity / 1000;
  62. case "cm": return quantity / 100;
  63. case "m": return quantity;
  64. case "in": return quantity / 39.3701;
  65. case "ft": return quantity / 3.2808;
  66. default: return quantity;
  67. }
  68. }
  69. }
  70. /*
  71. Recipe Object
  72. id = database id of recipe
  73. name = name of recipe
  74. price = price of recipe in cents
  75. ingredients = [{
  76. ingredient: Ingredient Object,
  77. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  78. }]
  79. parent = merchant that it belongs to
  80. */
  81. class Recipe{
  82. constructor(id, name, price, ingredients, parent){
  83. if(price < 0){
  84. controller.createBanner("PRICE CANNOT BE A NEGATIVE NUMBER", "error");
  85. return false;
  86. }
  87. if(!this.isSanitaryString(name)){
  88. controller.createBanner("NAME CONTAINS ILLEGAL CHARACTERS", "error");
  89. return false;
  90. }
  91. this._id = id;
  92. this._name = name;
  93. this._price = price;
  94. this._parent = parent;
  95. this._ingredients = [];
  96. for(let i = 0; i < ingredients.length; i++){
  97. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  98. const recipeIngredient = new RecipeIngredient(
  99. ingredient.ingredient,
  100. ingredients[i].quantity
  101. );
  102. this._ingredients.push(recipeIngredient);
  103. }
  104. }
  105. get id(){
  106. return this._id;
  107. }
  108. get name(){
  109. return this._name;
  110. }
  111. set name(name){
  112. if(!this.isSanitaryString(name)){
  113. return false;
  114. }
  115. this._name = name;
  116. }
  117. get price(){
  118. return this._price / 100;
  119. }
  120. set price(price){
  121. if(price < 0){
  122. return false;
  123. }
  124. this._price = price;
  125. }
  126. get parent(){
  127. return this._parent;
  128. }
  129. get ingredients(){
  130. return this._ingredients;
  131. }
  132. addIngredient(ingredient, quantity){
  133. if(quantity < 0){
  134. controller.createBanner("QUANTITY CANNOT BE A NEGATIVE NUMBER", "error");
  135. return false;
  136. }
  137. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  138. this._ingredients.push(recipeIngredient);
  139. this._parent.modules.recipeBook.isPopulated = false;
  140. this._parent.modules.analytics.isPopulated = false;
  141. }
  142. removeIngredients(){
  143. this._ingredients = [];
  144. }
  145. isSanitaryString(str){
  146. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  147. for(let i = 0; i < disallowed.length; i++){
  148. if(str.includes(disallowed[i])){
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. }
  155. module.exports = Recipe;