ConvertMass.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. /**
  8. * Convert mass operation
  9. */
  10. class ConvertMass extends Operation {
  11. /**
  12. * ConvertMass constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "Convert mass";
  17. this.module = "Default";
  18. this.description = "Converts a unit of mass to another format.";
  19. this.infoURL = "https://wikipedia.org/wiki/Orders_of_magnitude_(mass)";
  20. this.inputType = "BigNumber";
  21. this.outputType = "BigNumber";
  22. this.args = [
  23. {
  24. "name": "Input units",
  25. "type": "option",
  26. "value": MASS_UNITS
  27. },
  28. {
  29. "name": "Output units",
  30. "type": "option",
  31. "value": MASS_UNITS
  32. }
  33. ];
  34. }
  35. /**
  36. * @param {BigNumber} input
  37. * @param {Object[]} args
  38. * @returns {BigNumber}
  39. */
  40. run(input, args) {
  41. const [inputUnits, outputUnits] = args;
  42. input = input.times(MASS_FACTOR[inputUnits]);
  43. return input.div(MASS_FACTOR[outputUnits]);
  44. }
  45. }
  46. const MASS_UNITS = [
  47. "[Metric]", "Yoctogram (yg)", "Zeptogram (zg)", "Attogram (ag)", "Femtogram (fg)", "Picogram (pg)", "Nanogram (ng)", "Microgram (μg)", "Milligram (mg)", "Centigram (cg)", "Decigram (dg)", "Gram (g)", "Decagram (dag)", "Hectogram (hg)", "Kilogram (kg)", "Megagram (Mg)", "Tonne (t)", "Gigagram (Gg)", "Teragram (Tg)", "Petagram (Pg)", "Exagram (Eg)", "Zettagram (Zg)", "Yottagram (Yg)", "[/Metric]",
  48. "[Imperial Avoirdupois]", "Grain (gr)", "Dram (dr)", "Ounce (oz)", "Pound (lb)", "Nail", "Stone (st)", "Quarter (gr)", "Tod", "US hundredweight (cwt)", "Imperial hundredweight (cwt)", "US ton (t)", "Imperial ton (t)", "[/Imperial Avoirdupois]",
  49. "[Imperial Troy]", "Grain (gr)", "Pennyweight (dwt)", "Troy dram (dr t)", "Troy ounce (oz t)", "Troy pound (lb t)", "Mark", "[/Imperial Troy]",
  50. "[Archaic]", "Wey", "Wool wey", "Suffolk wey", "Wool sack", "Coal sack", "Load", "Last", "Flax or feather last", "Gunpowder last", "Picul", "Rice last", "[/Archaic]",
  51. "[Comparisons]", "Big Ben (14 tonnes)", "Blue whale (180 tonnes)", "International Space Station (417 tonnes)", "Space Shuttle (2,041 tonnes)", "RMS Titanic (52,000 tonnes)", "Great Pyramid of Giza (6,000,000 tonnes)", "Earth's oceans (1.4 yottagrams)", "[/Comparisons]",
  52. "[Astronomical]", "A teaspoon of neutron star (5,500 million tonnes)", "Lunar mass (ML)", "Earth mass (M⊕)", "Jupiter mass (MJ)", "Solar mass (M☉)", "Sagittarius A* (7.5 x 10^36 kgs-ish)", "Milky Way galaxy (1.2 x 10^42 kgs)", "The observable universe (1.45 x 10^53 kgs)", "[/Astronomical]",
  53. ];
  54. const MASS_FACTOR = { // Multiples of a gram
  55. // Metric
  56. "Yoctogram (yg)": 1e-24,
  57. "Zeptogram (zg)": 1e-21,
  58. "Attogram (ag)": 1e-18,
  59. "Femtogram (fg)": 1e-15,
  60. "Picogram (pg)": 1e-12,
  61. "Nanogram (ng)": 1e-9,
  62. "Microgram (μg)": 1e-6,
  63. "Milligram (mg)": 1e-3,
  64. "Centigram (cg)": 1e-2,
  65. "Decigram (dg)": 1e-1,
  66. "Gram (g)": 1,
  67. "Decagram (dag)": 10,
  68. "Hectogram (hg)": 100,
  69. "Kilogram (kg)": 1000,
  70. "Megagram (Mg)": 1e6,
  71. "Tonne (t)": 1e6,
  72. "Gigagram (Gg)": 1e9,
  73. "Teragram (Tg)": 1e12,
  74. "Petagram (Pg)": 1e15,
  75. "Exagram (Eg)": 1e18,
  76. "Zettagram (Zg)": 1e21,
  77. "Yottagram (Yg)": 1e24,
  78. // Imperial Avoirdupois
  79. "Grain (gr)": 64.79891e-3,
  80. "Dram (dr)": 1.7718451953125,
  81. "Ounce (oz)": 28.349523125,
  82. "Pound (lb)": 453.59237,
  83. "Nail": 3175.14659,
  84. "Stone (st)": 6.35029318e3,
  85. "Quarter (gr)": 12700.58636,
  86. "Tod": 12700.58636,
  87. "US hundredweight (cwt)": 45.359237e3,
  88. "Imperial hundredweight (cwt)": 50.80234544e3,
  89. "US ton (t)": 907.18474e3,
  90. "Imperial ton (t)": 1016.0469088e3,
  91. // Imperial Troy
  92. "Pennyweight (dwt)": 1.55517384,
  93. "Troy dram (dr t)": 3.8879346,
  94. "Troy ounce (oz t)": 31.1034768,
  95. "Troy pound (lb t)": 373.2417216,
  96. "Mark": 248.8278144,
  97. // Archaic
  98. "Wey": 76.5e3,
  99. "Wool wey": 101.7e3,
  100. "Suffolk wey": 161.5e3,
  101. "Wool sack": 153000,
  102. "Coal sack": 50.80234544e3,
  103. "Load": 918000,
  104. "Last": 1836000,
  105. "Flax or feather last": 770e3,
  106. "Gunpowder last": 1090e3,
  107. "Picul": 60.478982e3,
  108. "Rice last": 1200e3,
  109. // Comparisons
  110. "Big Ben (14 tonnes)": 14e6,
  111. "Blue whale (180 tonnes)": 180e6,
  112. "International Space Station (417 tonnes)": 417e6,
  113. "Space Shuttle (2,041 tonnes)": 2041e6,
  114. "RMS Titanic (52,000 tonnes)": 52000e6,
  115. "Great Pyramid of Giza (6,000,000 tonnes)": 6e12,
  116. "Earth's oceans (1.4 yottagrams)": 1.4e24,
  117. // Astronomical
  118. "A teaspoon of neutron star (5,500 million tonnes)": 5.5e15,
  119. "Lunar mass (ML)": 7.342e25,
  120. "Earth mass (M⊕)": 5.97219e27,
  121. "Jupiter mass (MJ)": 1.8981411476999997e30,
  122. "Solar mass (M☉)": 1.98855e33,
  123. "Sagittarius A* (7.5 x 10^36 kgs-ish)": 7.5e39,
  124. "Milky Way galaxy (1.2 x 10^42 kgs)": 1.2e45,
  125. "The observable universe (1.45 x 10^53 kgs)": 1.45e56,
  126. };
  127. export default ConvertMass;