1 /* 2 * VelocityUnit.js - Unit conversions for velocity/speed measurements 3 * 4 * Copyright © 2014-2015, 2018 JEDLSoft 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 /* 21 !depends 22 Measurement.js 23 */ 24 25 var Measurement = require("./Measurement.js"); 26 27 /** 28 * @class 29 * Create a new speed measurement instance. 30 * 31 * @constructor 32 * @extends Measurement 33 * @param options {{unit:string,amount:number|string|undefined}} Options controlling 34 * the construction of this instance 35 */ 36 var VelocityUnit = function (options) { 37 this.unit = "meter-per-second"; 38 this.amount = 0; 39 40 this.ratios = VelocityUnit.ratios; 41 this.aliases = VelocityUnit.aliases; 42 this.aliasesLower = VelocityUnit.aliasesLower; 43 this.systems = VelocityUnit.systems; 44 45 this.parent.call(this, options); 46 }; 47 48 VelocityUnit.prototype = new Measurement(); 49 VelocityUnit.prototype.parent = Measurement; 50 VelocityUnit.prototype.constructor = VelocityUnit; 51 52 VelocityUnit.ratios = { 53 /* index, k/h f/s miles/h knot m/s km/s miles/s */ 54 "kilometer-per-hour": [ 1, 1, 0.911344, 0.621371, 0.539957, 0.277778, 2.77778e-4, 1.72603109e-4 ], 55 "foot-per-second": [ 2, 1.09728, 1, 0.681818, 0.592484, 0.3048, 3.048e-4, 1.89393939e-4 ], 56 "mile-per-hour": [ 3, 1.60934, 1.46667, 1, 0.868976, 0.44704, 4.4704e-4, 2.77777778e-4 ], 57 "knot": [ 4, 1.852, 1.68781, 1.15078, 1, 0.514444, 5.14444e-4, 3.19660958e-4 ], 58 "meter-per-second": [ 5, 3.6, 3.28084, 2.236936, 1.94384, 1, 0.001, 6.21371192e-4 ], 59 "kilometer-per-second": [ 6, 3600, 3280.8399, 2236.93629, 1943.84449, 1000, 1, 0.621371192 ], 60 "mile-per-second": [ 7, 5793.6384, 5280, 3600, 3128.31447, 1609.344, 1.609344, 1 ] 61 }; 62 63 /** 64 * Return a new instance of this type of measurement. 65 * 66 * @param {Object} params parameters to the constructor 67 * @return {Measurement} a measurement subclass instance 68 */ 69 VelocityUnit.prototype.newUnit = function(params) { 70 return new VelocityUnit(params); 71 }; 72 73 VelocityUnit.systems = { 74 "metric": [ 75 "kilometer-per-hour", 76 "meter-per-second", 77 "kilometer-per-second" 78 ], 79 "imperial": [ 80 "foot-per-second", 81 "mile-per-hour", 82 "knot", 83 "mile-per-second" 84 ], 85 "uscustomary": [ 86 "foot-per-second", 87 "mile-per-hour", 88 "knot", 89 "mile-per-second" 90 ], 91 "conversions": { 92 "imperial": { 93 "metric": { 94 "mile-per-hour": "kilometer-per-hour", 95 "foot-per-second": "meter-per-second", 96 "mile-per-second": "kilometer-per-second", 97 "knot": "kilometer-per-hour" 98 } 99 }, 100 "uscustomary": { 101 "metric": { 102 "mile-per-hour": "kilometer-per-hour", 103 "foot-per-second": "meter-per-second", 104 "mile-per-second": "kilometer-per-second", 105 "knot": "kilometer-per-hour" 106 } 107 }, 108 "metric": { 109 "uscustomary": { 110 "kilometer-per-hour": "mile-per-hour", 111 "meter-per-second": "foot-per-second", 112 "kilometer-per-second": "mile-per-second" 113 }, 114 "imperial": { 115 "kilometer-per-hour": "mile-per-hour", 116 "meter-per-second": "foot-per-second", 117 "kilometer-per-second": "mile-per-second" 118 } 119 } 120 } 121 }; 122 123 /** 124 * Return the type of this measurement. Examples are "mass", 125 * "length", "speed", etc. Measurements can only be converted 126 * to measurements of the same type.<p> 127 * 128 * The type of the units is determined automatically from the 129 * units. For example, the unit "grams" is type "mass". Use the 130 * static call {@link Measurement.getAvailableUnits} 131 * to find out what units this version of ilib supports. 132 * 133 * @return {string} the name of the type of this measurement 134 */ 135 VelocityUnit.prototype.getMeasure = function() { 136 return "velocity"; 137 }; 138 139 VelocityUnit.aliases = { 140 "foot/sec": "foot-per-second", 141 "foot/s": "foot-per-second", 142 "feet/s": "foot-per-second", 143 "f/s": "foot-per-second", 144 "feet/second": "foot-per-second", 145 "feet/sec": "foot-per-second", 146 "meter/sec": "meter-per-second", 147 "meter/s": "meter-per-second", 148 "meters/s": "meter-per-second", 149 "metre/sec": "meter-per-second", 150 "metre/s": "meter-per-second", 151 "metres/s": "meter-per-second", 152 "mt/sec": "meter-per-second", 153 "m/sec": "meter-per-second", 154 "mt/s": "meter-per-second", 155 "m/s": "meter-per-second", 156 "mps": "meter-per-second", 157 "meters/second": "meter-per-second", 158 "meters/sec": "meter-per-second", 159 "kilometer/hour": "kilometer-per-hour", 160 "km/hour": "kilometer-per-hour", 161 "kilometers/hour": "kilometer-per-hour", 162 "kilometer per hour": "kilometer-per-hour", 163 "kilometers per hour": "kilometer-per-hour", 164 "kph": "kilometer-per-hour", 165 "kmh": "kilometer-per-hour", 166 "km/h": "kilometer-per-hour", 167 "kilometer/h": "kilometer-per-hour", 168 "kilometers/h": "kilometer-per-hour", 169 "km/hr": "kilometer-per-hour", 170 "kilometer/hr": "kilometer-per-hour", 171 "kilometers/hr": "kilometer-per-hour", 172 "kilometre/hour": "kilometer-per-hour", 173 "mph": "mile-per-hour", 174 "mile/hour": "mile-per-hour", 175 "mile per hour": "mile-per-hour", 176 "miles per hour": "miles-per-hour", 177 "mile/hr": "mile-per-hour", 178 "mile/h": "mile-per-hour", 179 "miles/h": "mile-per-hour", 180 "miles/hr": "mile-per-hour", 181 "miles/hour": "mile-per-hour", 182 "kn": "knot", 183 "kt": "knot", 184 "kts": "knot", 185 "knots": "knot", 186 "nm/h": "knot", 187 "nm/hr": "knot", 188 "nauticalmile/h": "knot", 189 "nauticalmile/hr": "knot", 190 "nauticalmile/hour": "knot", 191 "nauticalmiles/hr": "knot", 192 "nauticalmiles/hour": "knot", 193 "nautical mile per hour": "knot", 194 "nautical miles per hour": "knot", 195 "nautical-mile/h": "knot", 196 "nautical-mile/hr": "knot", 197 "nautical-mile/hour": "knot", 198 "nautical-miles/hr": "knot", 199 "nautical-miles/hour": "knot", 200 "knot": "knot", 201 "kilometer/second": "kilometer-per-second", 202 "kilometer/sec": "kilometer-per-second", 203 "kilometre/sec": "kilometer-per-second", 204 "Kilometre/sec": "kilometer-per-second", 205 "kilometers/second": "kilometer-per-second", 206 "kilometers/sec": "kilometer-per-second", 207 "kilometres/sec": "kilometer-per-second", 208 "Kilometres/sec": "kilometer-per-second", 209 "km/sec": "kilometer-per-second", 210 "Km/s": "kilometer-per-second", 211 "km/s": "kilometer-per-second", 212 "miles/second": "mile-per-second", 213 "miles/sec": "mile-per-second", 214 "miles/s": "mile-per-second", 215 "mile/s": "mile-per-second", 216 "mile/sec": "mile-per-second", 217 "Mile/s": "mile-per-second" 218 }; 219 220 (function() { 221 VelocityUnit.aliasesLower = {}; 222 for (var a in VelocityUnit.aliases) { 223 VelocityUnit.aliasesLower[a.toLowerCase()] = VelocityUnit.aliases[a]; 224 } 225 })(); 226 227 /** 228 * Convert a speed to another measure. 229 * @static 230 * @param to {string} unit to convert to 231 * @param from {string} unit to convert from 232 * @param speed {number} amount to be convert 233 * @returns {number|undefined} the converted amount 234 */ 235 VelocityUnit.convert = function(to, from, speed) { 236 from = Measurement.getUnitIdCaseInsensitive(VelocityUnit, from) || from; 237 to = Measurement.getUnitIdCaseInsensitive(VelocityUnit, to) || to; 238 var fromRow = VelocityUnit.ratios[from]; 239 var toRow = VelocityUnit.ratios[to]; 240 if (typeof(from) === 'undefined' || typeof(to) === 'undefined') { 241 return undefined; 242 } 243 var result = speed * fromRow[toRow[0]]; 244 return result; 245 }; 246 247 /** 248 * @private 249 * @static 250 */ 251 VelocityUnit.getMeasures = function () { 252 return Object.keys(VelocityUnit.ratios); 253 }; 254 255 //register with the factory method 256 Measurement._constructors["speed"] = VelocityUnit; 257 Measurement._constructors["velocity"] = VelocityUnit; 258 259 module.exports = VelocityUnit; 260