Recipe.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. class RecipeIngredient{
  2. constructor(ingredient, quantity){
  3. if(quantity < 0){
  4. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  5. return false;
  6. }
  7. this._ingredient = ingredient;
  8. this._quantity = this.convertToBase(quantity);
  9. }
  10. get ingredient(){
  11. return this._ingredient;
  12. }
  13. get quantity(){
  14. switch(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. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  39. return false;
  40. }
  41. this_quantity = this.convertToBase(quantity);
  42. }
  43. convertToBase(quantity){
  44. switch(this._ingredient.unit){
  45. case "g": return quantity;
  46. case "kg": return quantity / 1000;
  47. case "oz": return quantity / 28.3495;
  48. case "lb": return quantity / 453.5924;
  49. case "ml": return quantity *= 1000;
  50. case "l": return quantity;
  51. case "tsp": return quantity * 202.8842;
  52. case "tbsp": return quantity * 67.6278;
  53. case "ozfl": return quantity * 33.8141;
  54. case "cup": return quantity * 4.1667;
  55. case "pt": return quantity * 2.1134;
  56. case "qt": return quantity * 1.0567;
  57. case "gal": return quantity / 3.7854;
  58. case "mm": return quantity * 1000;
  59. case "cm": return quantity * 100;
  60. case "m": return quantity;
  61. case "in": return quantity * 39.3701;
  62. case "ft": return quantity * 3.2808;
  63. default: return quantity;
  64. }
  65. }
  66. }
  67. class Recipe{
  68. constructor(id, name, price, ingredients, parent){
  69. if(price < 0){
  70. banner.createError("PRICE CANNOT BE A NEGATIVE NUMBER");
  71. return false;
  72. }
  73. if(!this.isSanitaryString(name)){
  74. banner.createError("NAME CONTAINS ILLEGAL CHARACTERS");
  75. return false;
  76. }
  77. this._id = id;
  78. this._name = name;
  79. this._price = price;
  80. this._parent = parent;
  81. this._ingredients = [];
  82. for(let i = 0; i < ingredients.length; i++){
  83. const recipeIngredient = new RecipeIngredient(
  84. ingredients[i].ingredient,
  85. ingredients[i].quantity
  86. );
  87. this._ingredients.push(recipeIngredient);
  88. }
  89. this._parent.modules.recipeBook.isPopulated = false;
  90. this._parent.modules.analytics.isPopulated = false;
  91. }
  92. get id(){
  93. return this._id;
  94. }
  95. get name(){
  96. return this._name;
  97. }
  98. set name(name){
  99. if(!this.isSanitaryString(name)){
  100. return false;
  101. }
  102. this._name = name;
  103. }
  104. get price(){
  105. return this._price;
  106. }
  107. set price(price){
  108. if(price < 0){
  109. return false;
  110. }
  111. this._price = price;
  112. }
  113. get parent(){
  114. return this._parent;
  115. }
  116. get ingredients(){
  117. return this._ingredients;
  118. }
  119. addIngredient(ingredient, quantity){
  120. if(quantity < 0){
  121. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  122. return false;
  123. }
  124. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  125. this._ingredients.push(recipeIngredient);
  126. this._parent.modules.recipeBook.isPopulated = false;
  127. this._parent.modules.analytics.isPopulated = false;
  128. this._parent.modules.recipeBook.display();
  129. }
  130. removeIngredient(ingredient){
  131. const index = this._ingredients.indexOf(ingredient);
  132. this._ingredients.splice(index, 1);
  133. }
  134. updateIngredient(ingredient, quantity){
  135. if(quantity < 0){
  136. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  137. return false;
  138. }
  139. const index = this._ingredients.indoxOf(ingredient);
  140. this._ingredients[index].quantity = quantity;
  141. }
  142. isSanitaryString(str){
  143. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  144. for(let i = 0; i < disallowed.length; i++){
  145. if(str.includes(disallowed[i])){
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. }
  152. module.exports = Recipe;