ConvertSpeed.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. /**
  8. * Convert speed operation
  9. */
  10. class ConvertSpeed extends Operation {
  11. /**
  12. * ConvertSpeed constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "Convert speed";
  17. this.module = "Default";
  18. this.description = "Converts a unit of speed to another format.";
  19. this.inputType = "BigNumber";
  20. this.outputType = "BigNumber";
  21. this.args = [
  22. {
  23. "name": "Input units",
  24. "type": "option",
  25. "value": SPEED_UNITS
  26. },
  27. {
  28. "name": "Output units",
  29. "type": "option",
  30. "value": SPEED_UNITS
  31. }
  32. ];
  33. }
  34. /**
  35. * @param {BigNumber} input
  36. * @param {Object[]} args
  37. * @returns {BigNumber}
  38. */
  39. run(input, args) {
  40. const [inputUnits, outputUnits] = args;
  41. input = input.times(SPEED_FACTOR[inputUnits]);
  42. return input.div(SPEED_FACTOR[outputUnits]);
  43. }
  44. }
  45. const SPEED_UNITS = [
  46. "[Metric]", "Metres per second (m/s)", "Kilometres per hour (km/h)", "[/Metric]",
  47. "[Imperial]", "Miles per hour (mph)", "Knots (kn)", "[/Imperial]",
  48. "[Comparisons]", "Human hair growth rate", "Bamboo growth rate", "World's fastest snail", "Usain Bolt's top speed", "Jet airliner cruising speed", "Concorde", "SR-71 Blackbird", "Space Shuttle", "International Space Station", "[/Comparisons]",
  49. "[Scientific]", "Sound in standard atmosphere", "Sound in water", "Lunar escape velocity", "Earth escape velocity", "Earth's solar orbit", "Solar system's Milky Way orbit", "Milky Way relative to the cosmic microwave background", "Solar escape velocity", "Neutron star escape velocity (0.3c)", "Light in a diamond (0.4136c)", "Signal in an optical fibre (0.667c)", "Light (c)", "[/Scientific]",
  50. ];
  51. const SPEED_FACTOR = { // Multiples of m/s
  52. // Metric
  53. "Metres per second (m/s)": 1,
  54. "Kilometres per hour (km/h)": 0.2778,
  55. // Imperial
  56. "Miles per hour (mph)": 0.44704,
  57. "Knots (kn)": 0.5144,
  58. // Comparisons
  59. "Human hair growth rate": 4.8e-9,
  60. "Bamboo growth rate": 1.4e-5,
  61. "World's fastest snail": 0.00275,
  62. "Usain Bolt's top speed": 12.42,
  63. "Jet airliner cruising speed": 250,
  64. "Concorde": 603,
  65. "SR-71 Blackbird": 981,
  66. "Space Shuttle": 1400,
  67. "International Space Station": 7700,
  68. // Scientific
  69. "Sound in standard atmosphere": 340.3,
  70. "Sound in water": 1500,
  71. "Lunar escape velocity": 2375,
  72. "Earth escape velocity": 11200,
  73. "Earth's solar orbit": 29800,
  74. "Solar system's Milky Way orbit": 200000,
  75. "Milky Way relative to the cosmic microwave background": 552000,
  76. "Solar escape velocity": 617700,
  77. "Neutron star escape velocity (0.3c)": 100000000,
  78. "Light in a diamond (0.4136c)": 124000000,
  79. "Signal in an optical fibre (0.667c)": 200000000,
  80. "Light (c)": 299792458,
  81. };
  82. export default ConvertSpeed;