Recipe.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 = quantity;
  9. }
  10. get ingredient(){
  11. return this._ingredient;
  12. }
  13. get quantity(){
  14. if(this._ingredient.specialUnit === "bottle"){
  15. return this._quantity / this._ingredient.unitSize;
  16. }
  17. switch(this._ingredient.unit){
  18. case "g":return this._quantity;
  19. case "kg": return this._quantity / 1000;
  20. case "oz": return this._quantity / 28.3495;
  21. case "lb": return this._quantity / 453.5924;
  22. case "ml": return this._quantity * 1000;
  23. case "l": return this._quantity;
  24. case "tsp": return this._quantity * 202.8842;
  25. case "tbsp": return this._quantity * 67.6278;
  26. case "ozfl": return this._quantity * 33.8141;
  27. case "cup": return this._quantity * 4.1667;
  28. case "pt": return this._quantity * 2.1134;
  29. case "qt": return this._quantity * 1.0567;
  30. case "gal": return this._quantity / 3.7854;
  31. case "mm": return this._quantity * 1000;
  32. case "cm": return this._quantity * 100;
  33. case "m": return this._quantity;
  34. case "in": return this._quantity * 39.3701;
  35. case "ft": return this._quantity * 3.2808;
  36. default: return this._quantity;
  37. }
  38. }
  39. set quantity(quantity){
  40. if(quantity < 0){
  41. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  42. return false;
  43. }
  44. this_quantity = this.convertToBase(quantity);
  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. class Recipe{
  71. constructor(id, name, price, ingredients, parent){
  72. if(price < 0){
  73. banner.createError("PRICE CANNOT BE A NEGATIVE NUMBER");
  74. return false;
  75. }
  76. if(!this.isSanitaryString(name)){
  77. banner.createError("NAME CONTAINS ILLEGAL CHARACTERS");
  78. return false;
  79. }
  80. this._id = id;
  81. this._name = name;
  82. this._price = price;
  83. this._parent = parent;
  84. this._ingredients = [];
  85. for(let i = 0; i < ingredients.length; i++){
  86. const recipeIngredient = new RecipeIngredient(
  87. ingredients[i].ingredient,
  88. ingredients[i].quantity
  89. );
  90. this._ingredients.push(recipeIngredient);
  91. }
  92. this._parent.modules.recipeBook.isPopulated = false;
  93. this._parent.modules.analytics.isPopulated = false;
  94. }
  95. get id(){
  96. return this._id;
  97. }
  98. get name(){
  99. return this._name;
  100. }
  101. set name(name){
  102. if(!this.isSanitaryString(name)){
  103. return false;
  104. }
  105. this._name = name;
  106. }
  107. get price(){
  108. return this._price;
  109. }
  110. set price(price){
  111. if(price < 0){
  112. return false;
  113. }
  114. this._price = price;
  115. }
  116. get parent(){
  117. return this._parent;
  118. }
  119. get ingredients(){
  120. return this._ingredients;
  121. }
  122. addIngredient(ingredient, quantity){
  123. if(quantity < 0){
  124. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  125. return false;
  126. }
  127. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  128. this._ingredients.push(recipeIngredient);
  129. this._parent.modules.recipeBook.isPopulated = false;
  130. this._parent.modules.analytics.isPopulated = false;
  131. }
  132. removeIngredient(ingredient){
  133. const index = this._ingredients.indexOf(ingredient);
  134. this._ingredients.splice(index, 1);
  135. }
  136. updateIngredient(ingredient, quantity){
  137. if(quantity < 0){
  138. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  139. return false;
  140. }
  141. const index = this._ingredients.indoxOf(ingredient);
  142. this._ingredients[index].quantity = quantity;
  143. }
  144. isSanitaryString(str){
  145. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  146. for(let i = 0; i < disallowed.length; i++){
  147. if(str.includes(disallowed[i])){
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. }
  154. module.exports = Recipe;