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