1
1

Measure.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. interface Measurement {
  2. getText : () => string;
  3. }
  4. /**
  5. * A measure is ALWAYS created in Centimeters.
  6. * How a measure gets displayed can get changed later, so always use this class for measures!
  7. * If you're american, use the helper static functions fromInches, fromFeet to get centimeters.
  8. * For instance, 5'10" would get created as:
  9. * new Measure(Measure.fromFeet(5) + Measure.fromInches(10)
  10. *
  11. * If multiple measurements are added, it's treated as area of something simple like rectangles or cubes or whatever, they're just multiplied.
  12. */
  13. enum MeasureUnitType {
  14. CORRECT,
  15. MURICAN
  16. }
  17. class Measure implements Measurement, Printable {
  18. private units : number;
  19. private sides : number;
  20. public static unitType = new StoredMemory("UnitType", MeasureUnitType.MURICAN);
  21. public constructor (...sides : Array<number>) {
  22. this.units = 1;
  23. sides.forEach((side) => {
  24. this.units *= side;
  25. });
  26. this.sides = sides.length;
  27. }
  28. // ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  29. private superscript = ["" , "" , "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"];
  30. public getText () {
  31. let meters = Math.pow(this.sides, 100);
  32. if (this.units > meters) {
  33. return (+(this.units / meters).toFixed(2)).toString() + "m" + this.superscript[this.sides];
  34. } else {
  35. return this.units.toString() + "cm" + this.superscript[this.sides];
  36. }
  37. }
  38. public getPrintedName () {
  39. let unit = Measure.unitType.getValue();
  40. if (unit == MeasureUnitType.CORRECT) {
  41. let meters = Math.pow(this.sides, 100);
  42. if (this.units > meters) {
  43. return (+(this.units / meters).toFixed(2)).toString() + "m" + this.superscript[this.sides];
  44. } else {
  45. return this.units.toString() + "cm" + this.superscript[this.sides];
  46. }
  47. } else if (unit == MeasureUnitType.MURICAN) {
  48. // Gotta say, although this looks awful, at least I don't have to worry about "when does inch becomes feet".
  49. // In fact I liked it so much I'll actually make it the default.
  50. let inches = Measure.toInches(this.units);
  51. let feet = Math.floor(inches / 12);
  52. inches = inches - (feet * 12);
  53. inches = Math.floor(inches);
  54. let text = [];
  55. if (feet > 0) {
  56. text.push(feet.toString() + "′");
  57. }
  58. if (inches > 0) {
  59. text.push(inches.toString() + "″");
  60. }
  61. return text.join(" ");
  62. } else {
  63. return "INVALID"
  64. }
  65. }
  66. public getNumber () {
  67. return this.units;
  68. }
  69. public getSides () {
  70. return this.sides;
  71. }
  72. public static fromInches (inches : number) {
  73. return inches * 2.54;
  74. }
  75. public static toInches (cm : number) {
  76. return cm / 2.54;
  77. }
  78. public static fromFeet (feet : number) {
  79. return feet * 30.48;
  80. }
  81. }
  82. class MeasureLiquid implements Measurement {
  83. private units : number;
  84. public constructor (milliliters : number) {
  85. this.units = milliliters;
  86. }
  87. public getText () {
  88. if (this.units > 1000) {
  89. return (+(this.units / 1000).toFixed(2)).toString() + "L";
  90. } else {
  91. return this.units.toString() + "mL";
  92. }
  93. }
  94. public static fromLiters (liters : number) {
  95. return liters * 1000;
  96. }
  97. }