Ingredient.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. class Ingredient{
  2. constructor(id, name, category, unitType, unit, parent, unitSize = undefined){
  3. this._id = id;
  4. this._name = name;
  5. this._category = category;
  6. this._unitType = unitType;
  7. this._unit = unit;
  8. this._parent = parent;
  9. this._unitSize = unitSize;
  10. this._subIngredients = [];
  11. }
  12. get id(){
  13. return this._id;
  14. }
  15. get name(){
  16. return this._name;
  17. }
  18. set name(name){
  19. this._name = name;
  20. }
  21. get category(){
  22. return this._category;
  23. }
  24. set category(category){
  25. this._category = category;
  26. }
  27. get unitType(){
  28. return this._unitType;
  29. }
  30. get unit(){
  31. return this._unit;
  32. }
  33. set unit(unit){
  34. this._unit = unit;
  35. }
  36. get parent(){
  37. return this._parent;
  38. }
  39. get specialUnit(){
  40. return this._specialUnit;
  41. }
  42. get unitSize(){
  43. switch(this._unit){
  44. case "g":return this._unitSize;
  45. case "kg": return this._unitSize / 1000;
  46. case "oz": return this._unitSize / 28.3495;
  47. case "lb": return this._unitSize / 453.5924;
  48. case "ml": return this._unitSize * 1000;
  49. case "l": return this._unitSize;
  50. case "tsp": return this._unitSize * 202.8842;
  51. case "tbsp": return this._unitSize * 67.6278;
  52. case "ozfl": return this._unitSize * 33.8141;
  53. case "cup": return this._unitSize * 4.1667;
  54. case "pt": return this._unitSize * 2.1134;
  55. case "qt": return this._unitSize * 1.0567;
  56. case "gal": return this._unitSize / 3.7854;
  57. case "mm": return this._unitSize * 1000;
  58. case "cm": return this._unitSize * 100;
  59. case "m": return this._unitSize;
  60. case "in": return this._unitSize * 39.3701;
  61. case "ft": return this._unitSize * 3.2808;
  62. default: return this._unitSize;
  63. }
  64. }
  65. set unitSize(unitSize){
  66. if(unitSize < 0){
  67. return false;
  68. }
  69. this._unitSize = unitSize;
  70. }
  71. get subIngredients(){
  72. return this._subIngredients;
  73. }
  74. addIngredients(ingredients){
  75. for(let i = 0; i < ingredients.length; i++){
  76. this.subIngredients.push({
  77. ingredient: this._parent.getIngredient(ingredients[i].ingredient).ingredient,
  78. quantity: ingredients[i].quantity
  79. });
  80. }
  81. }
  82. getBaseUnitSize(){
  83. return this._unitSize;
  84. }
  85. getNameAndUnit(){
  86. return `${this._name} (${this._unit.toUpperCase()})`;
  87. }
  88. getPotentialUnits(){
  89. let mass = ["g", "kg", "oz", "lb"];
  90. let volume = ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"];
  91. let length = ["mm", "cm", "m", "in", "ft"];
  92. if(mass.includes(this._unit)) return mass;
  93. if(volume.includes(this._unit)) return volume;
  94. if(length.includes(this._unit)) return length;
  95. if(this._unit === "bottle") return volume;
  96. return [];
  97. }
  98. }
  99. module.exports = Ingredient;