Ingredient.js 2.9 KB

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