Ingredient.js 2.7 KB

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