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