Ingredient.js 3.1 KB

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