Recipe.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /*
  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. banner.createError("PRICE CANNOT BE A NEGATIVE NUMBER");
  91. return false;
  92. }
  93. if(!this.isSanitaryString(name)){
  94. banner.createError("NAME CONTAINS ILLEGAL CHARACTERS");
  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 recipeIngredient = new RecipeIngredient(
  104. ingredients[i].ingredient,
  105. ingredients[i].quantity
  106. );
  107. this._ingredients.push(recipeIngredient);
  108. }
  109. }
  110. get id(){
  111. return this._id;
  112. }
  113. get name(){
  114. return this._name;
  115. }
  116. set name(name){
  117. if(!this.isSanitaryString(name)){
  118. return false;
  119. }
  120. this._name = name;
  121. }
  122. get price(){
  123. return this._price / 100;
  124. }
  125. set price(price){
  126. if(price < 0){
  127. return false;
  128. }
  129. this._price = price;
  130. }
  131. get parent(){
  132. return this._parent;
  133. }
  134. get ingredients(){
  135. return this._ingredients;
  136. }
  137. addIngredient(ingredient, quantity){
  138. if(quantity < 0){
  139. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  140. return false;
  141. }
  142. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  143. this._ingredients.push(recipeIngredient);
  144. this._parent.modules.recipeBook.isPopulated = false;
  145. this._parent.modules.analytics.isPopulated = false;
  146. }
  147. removeIngredients(){
  148. this._ingredients = [];
  149. }
  150. isSanitaryString(str){
  151. let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
  152. for(let i = 0; i < disallowed.length; i++){
  153. if(str.includes(disallowed[i])){
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. }
  160. module.exports = Recipe;