Recipe.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. 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. controller.createBanner("QUANTITY CANNOT BE A NEGATIVE NUMBER", "error");
  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. /*
  77. Recipe Object
  78. id = database id of recipe
  79. name = name of recipe
  80. price = price of recipe in cents
  81. ingredients = [{
  82. ingredient: Ingredient Object,
  83. quantity: quantity of the ingredient within the recipe (stored as base unit, i.e grams)
  84. }]
  85. parent = merchant that it belongs to
  86. */
  87. class Recipe{
  88. constructor(id, name, price, ingredients, parent){
  89. if(price < 0){
  90. controller.createBanner("PRICE CANNOT BE A NEGATIVE NUMBER", "error");
  91. return false;
  92. }
  93. if(!this.isSanitaryString(name)){
  94. controller.createBanner("NAME CONTAINS ILLEGAL CHARACTERS", "error");
  95. return false;
  96. }
  97. this._id = id;
  98. this._name = name;
  99. this._price = price;
  100. this._parent = parent;
  101. this._ingredients = [];
  102. for(let i = 0; i < ingredients.length; i++){
  103. const ingredient = parent.getIngredient(ingredients[i].ingredient);
  104. const recipeIngredient = new RecipeIngredient(
  105. ingredient.ingredient,
  106. ingredients[i].quantity
  107. );
  108. this._ingredients.push(recipeIngredient);
  109. }
  110. }
  111. get id(){
  112. return this._id;
  113. }
  114. get name(){
  115. return this._name;
  116. }
  117. set name(name){
  118. if(!this.isSanitaryString(name)){
  119. return false;
  120. }
  121. this._name = name;
  122. }
  123. get price(){
  124. return this._price / 100;
  125. }
  126. set price(price){
  127. if(price < 0){
  128. return false;
  129. }
  130. this._price = price;
  131. }
  132. get parent(){
  133. return this._parent;
  134. }
  135. get ingredients(){
  136. return this._ingredients;
  137. }
  138. addIngredient(ingredient, quantity){
  139. if(quantity < 0){
  140. controller.createBanner("QUANTITY CANNOT BE A NEGATIVE NUMBER", "error");
  141. return false;
  142. }
  143. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  144. this._ingredients.push(recipeIngredient);
  145. this._parent.modules.recipeBook.isPopulated = false;
  146. this._parent.modules.analytics.isPopulated = false;
  147. }
  148. removeIngredients(){
  149. this._ingredients = [];
  150. }
  151. isSanitaryString(str){
  152. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  153. for(let i = 0; i < disallowed.length; i++){
  154. if(str.includes(disallowed[i])){
  155. return false;
  156. }
  157. }
  158. return true;
  159. }
  160. }
  161. module.exports = Recipe;