Convert.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * Unit conversion operations.
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. * @copyright Crown Copyright 2016
  6. * @license Apache-2.0
  7. *
  8. * @namespace
  9. */
  10. const Convert = {
  11. /**
  12. * @constant
  13. * @default
  14. */
  15. DISTANCE_UNITS: [
  16. "[Metric]", "Nanometres (nm)", "Micrometres (µm)", "Millimetres (mm)", "Centimetres (cm)", "Metres (m)", "Kilometers (km)", "[/Metric]",
  17. "[Imperial]", "Thou (th)", "Inches (in)", "Feet (ft)", "Yards (yd)", "Chains (ch)", "Furlongs (fur)", "Miles (mi)", "Leagues (lea)", "[/Imperial]",
  18. "[Maritime]", "Fathoms (ftm)", "Cables", "Nautical miles", "[/Maritime]",
  19. "[Comparisons]", "Cars (4m)", "Buses (8.4m)", "American football fields (91m)", "Football pitches (105m)", "[/Comparisons]",
  20. "[Astronomical]", "Earth-to-Moons", "Earth's equators", "Astronomical units (au)", "Light-years (ly)", "Parsecs (pc)", "[/Astronomical]",
  21. ],
  22. /**
  23. * @constant
  24. * @default
  25. */
  26. DISTANCE_FACTOR: { // Multiples of a metre
  27. "Nanometres (nm)" : 1e-9,
  28. "Micrometres (µm)" : 1e-6,
  29. "Millimetres (mm)" : 1e-3,
  30. "Centimetres (cm)" : 1e-2,
  31. "Metres (m)" : 1,
  32. "Kilometers (km)" : 1e3,
  33. "Thou (th)" : 0.0000254,
  34. "Inches (in)" : 0.0254,
  35. "Feet (ft)" : 0.3048,
  36. "Yards (yd)" : 0.9144,
  37. "Chains (ch)" : 20.1168,
  38. "Furlongs (fur)" : 201.168,
  39. "Miles (mi)" : 1609.344,
  40. "Leagues (lea)" : 4828.032,
  41. "Fathoms (ftm)" : 1.853184,
  42. "Cables" : 185.3184,
  43. "Nautical miles" : 1853.184,
  44. "Cars (4m)" : 4,
  45. "Buses (8.4m)" : 8.4,
  46. "American football fields (91m)": 91,
  47. "Football pitches (105m)": 105,
  48. "Earth-to-Moons" : 380000000,
  49. "Earth's equators" : 40075016.686,
  50. "Astronomical units (au)": 149597870700,
  51. "Light-years (ly)" : 9460730472580800,
  52. "Parsecs (pc)" : 3.0856776e16
  53. },
  54. /**
  55. * Convert distance operation.
  56. *
  57. * @param {number} input
  58. * @param {Object[]} args
  59. * @returns {number}
  60. */
  61. runDistance: function (input, args) {
  62. var inputUnits = args[0],
  63. outputUnits = args[1];
  64. input = input * Convert.DISTANCE_FACTOR[inputUnits];
  65. return input / Convert.DISTANCE_FACTOR[outputUnits];
  66. // TODO Remove rounding errors (e.g. 1.000000000001)
  67. },
  68. /**
  69. * @constant
  70. * @default
  71. */
  72. DATA_UNITS: [
  73. "Bits (b)", "Nibbles", "Octets", "Bytes (B)",
  74. "[Binary bits (2^n)]", "Kibibits (Kib)", "Mebibits (Mib)", "Gibibits (Gib)", "Tebibits (Tib)", "Pebibits (Pib)", "Exbibits (Eib)", "Zebibits (Zib)", "Yobibits (Yib)", "[/Binary bits (2^n)]",
  75. "[Decimal bits (10^n)]", "Decabits", "Hectobits", "Kilobits (kb)", "Megabits (Mb)", "Gigabits (Gb)", "Terabits (Tb)", "Petabits (Pb)", "Exabits (Eb)", "Zettabits (Zb)", "Yottabits (Yb)", "[/Decimal bits (10^n)]",
  76. "[Binary bytes (8 x 2^n)]", "Kibibytes (KiB)", "Mebibytes (MiB)", "Gibibytes (GiB)", "Tebibytes (TiB)", "Pebibytes (PiB)", "Exbibytes (EiB)", "Zebibytes (ZiB)", "Yobibytes (YiB)", "[/Binary bytes (8 x 2^n)]",
  77. "[Decimal bytes (8 x 10^n)]", "Kilobytes (KB)", "Megabytes (MB)", "Gigabytes (GB)", "Terabytes (TB)", "Petabytes (PB)", "Exabytes (EB)", "Zettabytes (ZB)", "Yottabytes (YB)", "[/Decimal bytes (8 x 10^n)]"
  78. ],
  79. /**
  80. * @constant
  81. * @default
  82. */
  83. DATA_FACTOR: { // Multiples of a bit
  84. "Bits (b)" : 1,
  85. "Nibbles" : 4,
  86. "Octets" : 8,
  87. "Bytes (B)" : 8,
  88. // Binary bits (2^n)
  89. "Kibibits (Kib)" : 1024,
  90. "Mebibits (Mib)" : 1048576,
  91. "Gibibits (Gib)" : 1073741824,
  92. "Tebibits (Tib)" : 1099511627776,
  93. "Pebibits (Pib)" : 1125899906842624,
  94. "Exbibits (Eib)" : 1152921504606846976,
  95. "Zebibits (Zib)" : 1180591620717411303424,
  96. "Yobibits (Yib)" : 1208925819614629174706176,
  97. // Decimal bits (10^n)
  98. "Decabits" : 10,
  99. "Hectobits" : 100,
  100. "Kilobits (Kb)" : 1e3,
  101. "Megabits (Mb)" : 1e6,
  102. "Gigabits (Gb)" : 1e9,
  103. "Terabits (Tb)" : 1e12,
  104. "Petabits (Pb)" : 1e15,
  105. "Exabits (Eb)" : 1e18,
  106. "Zettabits (Zb)" : 1e21,
  107. "Yottabits (Yb)" : 1e24,
  108. // Binary bytes (8 x 2^n)
  109. "Kibibytes (KiB)" : 8192,
  110. "Mebibytes (MiB)" : 8388608,
  111. "Gibibytes (GiB)" : 8589934592,
  112. "Tebibytes (TiB)" : 8796093022208,
  113. "Pebibytes (PiB)" : 9007199254740992,
  114. "Exbibytes (EiB)" : 9223372036854775808,
  115. "Zebibytes (ZiB)" : 9444732965739290427392,
  116. "Yobibytes (YiB)" : 9671406556917033397649408,
  117. // Decimal bytes (8 x 10^n)
  118. "Kilobytes (KB)" : 8e3,
  119. "Megabytes (MB)" : 8e6,
  120. "Gigabytes (GB)" : 8e9,
  121. "Terabytes (TB)" : 8e12,
  122. "Petabytes (PB)" : 8e15,
  123. "Exabytes (EB)" : 8e18,
  124. "Zettabytes (ZB)" : 8e21,
  125. "Yottabytes (YB)" : 8e24,
  126. },
  127. /**
  128. * Convert data units operation.
  129. *
  130. * @param {number} input
  131. * @param {Object[]} args
  132. * @returns {number}
  133. */
  134. runDataSize: function (input, args) {
  135. var inputUnits = args[0],
  136. outputUnits = args[1];
  137. input = input * Convert.DATA_FACTOR[inputUnits];
  138. return input / Convert.DATA_FACTOR[outputUnits];
  139. },
  140. /**
  141. * @constant
  142. * @default
  143. */
  144. AREA_UNITS: [
  145. "[Metric]", "Square metre (sq m)", "Square kilometre (sq km)", "Centiare (ca)", "Deciare (da)", "Are (a)", "Decare (daa)", "Hectare (ha)", "[/Metric]",
  146. "[Imperial]", "Square inch (sq in)", "Square foot (sq ft)", "Square yard (sq yd)", "Square mile (sq mi)", "Perch (sq per)", "Rood (ro)", "International acre (ac)", "[/Imperial]",
  147. "[US customary units]", "US survey acre (ac)", "US survey square mile (sq mi)", "US survey township", "[/US customary units]",
  148. "[Nuclear physics]", "Yoctobarn (yb)", "Zeptobarn (zb)", "Attobarn (ab)", "Femtobarn (fb)", "Picobarn (pb)", "Nanobarn (nb)", "Microbarn (μb)", "Millibarn (mb)", "Barn (b)", "Kilobarn (kb)", "Megabarn (Mb)", "Outhouse", "Shed", "Planck area", "[/Nuclear physics]",
  149. "[Comparisons]", "Washington D.C.", "Isle of Wight", "Wales", "Texas", "[/Comparisons]",
  150. ],
  151. /**
  152. * @constant
  153. * @default
  154. */
  155. AREA_FACTOR: { // Multiples of a square metre
  156. // Metric
  157. "Square metre (sq m)" : 1,
  158. "Square kilometre (sq km)" : 1e6,
  159. "Centiare (ca)" : 1,
  160. "Deciare (da)" : 10,
  161. "Are (a)" : 100,
  162. "Decare (daa)" : 1e3,
  163. "Hectare (ha)" : 1e4,
  164. // Imperial
  165. "Square inch (sq in)" : 0.00064516,
  166. "Square foot (sq ft)" : 0.09290304,
  167. "Square yard (sq yd)" : 0.83612736,
  168. "Square mile (sq mi)" : 2589988.110336,
  169. "Perch (sq per)" : 42.21,
  170. "Rood (ro)" : 1011,
  171. "International acre (ac)" : 4046.8564224,
  172. // US customary units
  173. "US survey acre (ac)" : 4046.87261,
  174. "US survey square mile (sq mi)" : 2589998.470305239,
  175. "US survey township" : 93239944.9309886,
  176. // Nuclear physics
  177. "Yoctobarn (yb)" : 1e-52,
  178. "Zeptobarn (zb)" : 1e-49,
  179. "Attobarn (ab)" : 1e-46,
  180. "Femtobarn (fb)" : 1e-43,
  181. "Picobarn (pb)" : 1e-40,
  182. "Nanobarn (nb)" : 1e-37,
  183. "Microbarn (μb)" : 1e-34,
  184. "Millibarn (mb)" : 1e-31,
  185. "Barn (b)" : 1e-28,
  186. "Kilobarn (kb)" : 1e-25,
  187. "Megabarn (Mb)" : 1e-22,
  188. "Planck area" : 2.6e-70,
  189. "Shed" : 1e-52,
  190. "Outhouse" : 1e-34,
  191. // Comparisons
  192. "Washington D.C." : 176119191.502848,
  193. "Isle of Wight" : 380000000,
  194. "Wales" : 20779000000,
  195. "Texas" : 696241000000,
  196. },
  197. /**
  198. * Convert area operation.
  199. *
  200. * @param {number} input
  201. * @param {Object[]} args
  202. * @returns {number}
  203. */
  204. runArea: function (input, args) {
  205. var inputUnits = args[0],
  206. outputUnits = args[1];
  207. input = input * Convert.AREA_FACTOR[inputUnits];
  208. return input / Convert.AREA_FACTOR[outputUnits];
  209. },
  210. /**
  211. * @constant
  212. * @default
  213. */
  214. MASS_UNITS: [
  215. "[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]",
  216. "[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]",
  217. "[Imperial Troy]", "Grain (gr)", "Pennyweight (dwt)", "Troy dram (dr t)", "Troy ounce (oz t)", "Troy pound (lb t)", "Mark", "[/Imperial Troy]",
  218. "[Archaic]", "Wey", "Wool wey", "Suffolk wey", "Wool sack", "Coal sack", "Load", "Last", "Flax or feather last", "Gunpowder last", "Picul", "Rice last", "[/Archaic]",
  219. "[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]",
  220. "[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]",
  221. ],
  222. /**
  223. * @constant
  224. * @default
  225. */
  226. MASS_FACTOR: { // Multiples of a gram
  227. // Metric
  228. "Yoctogram (yg)" : 1e-24,
  229. "Zeptogram (zg)" : 1e-21,
  230. "Attogram (ag)" : 1e-18,
  231. "Femtogram (fg)" : 1e-15,
  232. "Picogram (pg)" : 1e-12,
  233. "Nanogram (ng)" : 1e-9,
  234. "Microgram (μg)" : 1e-6,
  235. "Milligram (mg)" : 1e-3,
  236. "Centigram (cg)" : 1e-2,
  237. "Decigram (dg)" : 1e-1,
  238. "Gram (g)" : 1,
  239. "Decagram (dag)" : 10,
  240. "Hectogram (hg)" : 100,
  241. "Kilogram (kg)" : 1000,
  242. "Megagram (Mg)" : 1e6,
  243. "Tonne (t)" : 1e6,
  244. "Gigagram (Gg)" : 1e9,
  245. "Teragram (Tg)" : 1e12,
  246. "Petagram (Pg)" : 1e15,
  247. "Exagram (Eg)" : 1e18,
  248. "Zettagram (Zg)" : 1e21,
  249. "Yottagram (Yg)" : 1e24,
  250. // Imperial Avoirdupois
  251. "Grain (gr)" : 64.79891e-3,
  252. "Dram (dr)" : 1.7718451953125,
  253. "Ounce (oz)" : 28.349523125,
  254. "Pound (lb)" : 453.59237,
  255. "Nail" : 3175.14659,
  256. "Stone (st)" : 6.35029318e3,
  257. "Quarter (gr)" : 12700.58636,
  258. "Tod" : 12700.58636,
  259. "US hundredweight (cwt)" : 45.359237e3,
  260. "Imperial hundredweight (cwt)" : 50.80234544e3,
  261. "US ton (t)" : 907.18474e3,
  262. "Imperial ton (t)" : 1016.0469088e3,
  263. // Imperial Troy
  264. "Pennyweight (dwt)" : 1.55517384,
  265. "Troy dram (dr t)" : 3.8879346,
  266. "Troy ounce (oz t)" : 31.1034768,
  267. "Troy pound (lb t)" : 373.2417216,
  268. "Mark" : 248.8278144,
  269. // Archaic
  270. "Wey" : 76.5e3,
  271. "Wool wey" : 101.7e3,
  272. "Suffolk wey" : 161.5e3,
  273. "Wool sack" : 153000,
  274. "Coal sack" : 50.80234544e3,
  275. "Load" : 918000,
  276. "Last" : 1836000,
  277. "Flax or feather last" : 770e3,
  278. "Gunpowder last" : 1090e3,
  279. "Picul" : 60.478982e3,
  280. "Rice last" : 1200e3,
  281. // Comparisons
  282. "Big Ben (14 tonnes)" : 14e6,
  283. "Blue whale (180 tonnes)" : 180e6,
  284. "International Space Station (417 tonnes)" : 417e6,
  285. "Space Shuttle (2,041 tonnes)" : 2041e6,
  286. "RMS Titanic (52,000 tonnes)" : 52000e6,
  287. "Great Pyramid of Giza (6,000,000 tonnes)" : 6e12,
  288. "Earth's oceans (1.4 yottagrams)" : 1.4e24,
  289. // Astronomical
  290. "A teaspoon of neutron star (5,500 million tonnes)" : 5.5e15,
  291. "Lunar mass (ML)" : 7.342e25,
  292. "Earth mass (M⊕)" : 5.97219e27,
  293. "Jupiter mass (MJ)" : 1.8981411476999997e30,
  294. "Solar mass (M☉)" : 1.98855e33,
  295. "Sagittarius A* (7.5 x 10^36 kgs-ish)" : 7.5e39,
  296. "Milky Way galaxy (1.2 x 10^42 kgs)" : 1.2e45,
  297. "The observable universe (1.45 x 10^53 kgs)" : 1.45e56,
  298. },
  299. /**
  300. * Convert mass operation.
  301. *
  302. * @param {number} input
  303. * @param {Object[]} args
  304. * @returns {number}
  305. */
  306. runMass: function (input, args) {
  307. var inputUnits = args[0],
  308. outputUnits = args[1];
  309. input = input * Convert.MASS_FACTOR[inputUnits];
  310. return input / Convert.MASS_FACTOR[outputUnits];
  311. },
  312. /**
  313. * @constant
  314. * @default
  315. */
  316. SPEED_UNITS: [
  317. "[Metric]", "Metres per second (m/s)", "Kilometres per hour (km/h)", "[/Metric]",
  318. "[Imperial]", "Miles per hour (mph)", "Knots (kn)", "[/Imperial]",
  319. "[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]",
  320. "[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]",
  321. ],
  322. /**
  323. * @constant
  324. * @default
  325. */
  326. SPEED_FACTOR: { // Multiples of m/s
  327. // Metric
  328. "Metres per second (m/s)" : 1,
  329. "Kilometres per hour (km/h)" : 0.2778,
  330. // Imperial
  331. "Miles per hour (mph)" : 0.44704,
  332. "Knots (kn)" : 0.5144,
  333. // Comparisons
  334. "Human hair growth rate" : 4.8e-9,
  335. "Bamboo growth rate" : 1.4e-5,
  336. "World's fastest snail" : 0.00275,
  337. "Usain Bolt's top speed" : 12.42,
  338. "Jet airliner cruising speed" : 250,
  339. "Concorde" : 603,
  340. "SR-71 Blackbird" : 981,
  341. "Space Shuttle" : 1400,
  342. "International Space Station" : 7700,
  343. // Scientific
  344. "Sound in standard atmosphere" : 340.3,
  345. "Sound in water" : 1500,
  346. "Lunar escape velocity" : 2375,
  347. "Earth escape velocity" : 11200,
  348. "Earth's solar orbit" : 29800,
  349. "Solar system's Milky Way orbit" : 200000,
  350. "Milky Way relative to the cosmic microwave background" : 552000,
  351. "Solar escape velocity" : 617700,
  352. "Neutron star escape velocity (0.3c)" : 100000000,
  353. "Light in a diamond (0.4136c)" : 124000000,
  354. "Signal in an optical fibre (0.667c)" : 200000000,
  355. "Light (c)" : 299792458,
  356. },
  357. /**
  358. * Convert speed operation.
  359. *
  360. * @param {number} input
  361. * @param {Object[]} args
  362. * @returns {number}
  363. */
  364. runSpeed: function (input, args) {
  365. var inputUnits = args[0],
  366. outputUnits = args[1];
  367. input = input * Convert.SPEED_FACTOR[inputUnits];
  368. return input / Convert.SPEED_FACTOR[outputUnits];
  369. },
  370. };
  371. export default Convert;