1 /* 2 * VolumeUnit.js - Unit conversions for volume 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 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 /* 22 !depends 23 Measurement.js 24 */ 25 26 var Measurement = require("./Measurement.js"); 27 28 /** 29 * @class 30 * Create a new Volume measurement instance. 31 * 32 * @constructor 33 * @extends Measurement 34 * @param options {{unit:string,amount:number|string|undefined}} Options controlling 35 * the construction of this instance 36 */ 37 var VolumeUnit = function (options) { 38 this.unit = "cubic-meter"; 39 this.amount = 0; 40 41 this.ratios = VolumeUnit.ratios; 42 this.aliases = VolumeUnit.aliases; 43 this.aliasesLower = VolumeUnit.aliasesLower; 44 this.systems = VolumeUnit.systems; 45 46 this.parent.call(this, options); 47 }; 48 49 VolumeUnit.prototype = new Measurement(); 50 VolumeUnit.prototype.parent = Measurement; 51 VolumeUnit.prototype.constructor = VolumeUnit; 52 53 VolumeUnit.ratios = { 54 /* index, tsp, tbsp, cubic inch us ounce, cup, pint, quart, gallon, cubic foot, milliliter liter, cubic meter, imperial tsp, imperial tbsp, imperial ounce, imperial cup, imperial pint, imperial quart, imperial gal, cubic cm */ 55 "teaspoon" : [1, 1, 0.3333333333333333, 0.300781, 0.166667, 0.0208333, 0.0104167, 0.00520833, 0.00130208, 0.000174063, 4.92892, 0.00492892, 4.9289e-6, 0.832674, 0.277558, 0.173474, 0.0173474, 0.00867369, 0.00433684, 0.00108421, 4.92892 ], 56 "tablespoon": [2, 3, 1, 0.902344, 0.5, 0.0625, 0.0312, 0.015625, 0.00390625, 0.00052219, 14.7868, 0.0147868, 1.4787e-5, 2.49802, 0.832674, 0.520421, 0.0520421, 0.0260211, 0.0130105, 0.00325263, 14.7868 ], 57 "cubic-inch": [3, 3.32468, 1.10823, 1, 0.554113, 0.0692641, 0.034632, 0.017316, 0.004329, 0.000578704, 16.3871, 0.0163871, 1.6387e-5, 2.76837, 0.92279, 0.576744, 0.057674402, 0.0288372, 0.0144186, 0.00360465, 16.3871 ], 58 "fluid-ounce": [4, 6, 2, 1.80469, 1, 0.125, 0.0625, 0.03125, 0.0078125, 0.00104438, 29.5735, 0.0295735, 2.9574e-5, 4.99604, 1.04084, 1.04084, 0.10408427, 0.0520421, 0.0260211, 0.00650526, 29.5735 ], 59 "cup": [5, 48, 16, 14.4375, 8, 1, 0.5, 0.25, 0.0625, 0.00835503, 236.588, 0.236588, 0.000236588, 39.9683, 13.3228, 8.32674, 0.83267418, 0.416337, 0.208168, 0.0520421, 236.588 ], 60 "pint": [6, 96, 32, 28.875, 16, 2, 1, 0.5, 0.125, 0.0167101, 473.176, 0.473176, 0.000473176, 79.9367, 26.6456, 16.6535, 1.66534836, 0.83267418, 0.416337, 0.104084, 473.176 ], 61 "quart": [7, 192, 64, 57.75, 32, 4, 2, 1, 0.25, 0.0334201, 946.353, 0.946353, 0.000946353, 159.873, 53.2911, 33.307, 3.33069674, 1.66534836, 0.832674, 0.208168, 946.353 ], 62 "gallon": [8, 768, 256, 231, 128, 16, 8, 4, 1, 0.133681, 3785.41, 3.78541, 0.00378541, 639.494, 213.165, 133.228, 13.322787, 6.66139, 3.33069674, 0.832674, 3785.41 ], 63 "cubic-foot": [9, 5745.04, 1915.01, 1728, 957.506, 119.688, 59.8442, 29.9221, 7.48052, 1, 28316.8, 28.3168, 0.0283168, 4783.74, 1594.58, 996.613, 99.661367, 49.8307, 24.9153, 6.22883, 28316.8 ], 64 "milliliter": [10, 0.202884, 0.067628, 0.0610237, 0.033814, 0.00422675, 0.00211338, 0.00105669, 0.000264172, 3.5315e-5, 1, 0.001, 1e-6, 0.168936, 0.0563121, 0.0351951, 0.00351950797, 0.00175975, 0.000879877, 0.000219969, 1 ], 65 "liter": [11, 202.884, 67.628, 61.0237, 33.814, 4.22675, 2.11338, 1.05669, 0.264172, 0.0353147, 1000, 1, 0.001, 56.3121, 56.3121, 35.191, 3.51950797, 1.75975, 0.879877, 0.219969, 1000 ], 66 "cubic-meter": [12, 202884, 67628, 61023.7, 33814, 4226.75, 2113.38, 1056.69, 264.172, 35.3147, 1e+6, 1000, 1, 168936, 56312.1, 35195.1, 3519.50797, 1759.75, 879.877, 219.969, 1e+6 ], 67 "teaspoon-imperial": [13, 1.20095, 0.200158, 0.361223, 0.600475, 0.0250198, 0.0125099, 0.00625495, 0.00156374, 0.000209041, 5.91939, 0.00591939, 5.9194e-6, 1, 0.3333333333333333, 0.208333333333333, 0.0208333333333333, 0.0104167, 0.00520833, 0.00130208, 5.91939 ], 68 "tablespoon-imperial": [14, 3.60285, 1.20095, 1.08367, 0.600475, 0.0750594, 0.0375297, 0.0187649, 0.00469121, 0.000627124, 17.7582, 0.0177582, 1.7758e-5, 3, 1, 0.625, 0.0625, 0.03125, 0.015625, 0.00390625, 17.7582 ], 69 "ounce-imperial": [15, 5.76456, 1.92152, 1.73387, 0.96076, 0.120095, 0.0600475, 0.0300238, 0.00750594, 0.0010034, 28.4131, 0.0284131, 2.8413e-5, 4.8, 1.6, 1, 0.1, 0.05, 0.025, 0.00625, 28.4131 ], 70 "pint-imperial": [17, 115.291, 38.4304, 34.6774, 19.2152, 2.4019, 1.20095, 0.600475, 0.150119, 0.020068, 568.261, 0.568261, 0.000568261, 96, 32, 20, 2, 1, 0.5, 0.125, 568.261 ], 71 "cup-imperial": [16, 57.64557, 19.2151988, 17.3387145, 9.6075994,1.20095, 0.60047496, 0.30023748, 0.07505937, 0.010033978, 284.130625, 0.28413063, 2.841306e-4, 48, 16, 10, 1, 0.5, 0.25, 0.0625, 284.130625 ], 72 "quart-imperial": [18, 230.582, 76.8608, 69.3549, 38.4304, 4.8038, 2.4019, 1.20095, 0.300238, 0.0401359, 1136.52, 1.13652, 0.00113652, 192, 64, 40, 4, 2, 1, 0.25, 1136.52 ], 73 "gallon-imperial": [19, 922.33, 307.443, 277.42, 153.722, 19.2152, 9.6076, 4.8038, 1.20095, 0.160544, 4546.09, 4.54609, 0.00454609, 768, 256, 160, 16, 8, 4, 1, 4546.09 ], 74 "cubic-centimeter": [20, 0.202884, 0.067628, 0.0610237, 0.033814, 0.00422675, 0.00211338, 0.00105669, 0.000264172, 3.5315e-5, 1, 0.001, 1e-6, 0.168936, 0.0563121, 0.0351951, 0.00351950797, 0.00175975, 0.000879877, 0.000219969, 1 ] 75 }; 76 77 /** 78 * Return the type of this measurement. Examples are "mass", 79 * "length", "speed", etc. Measurements can only be converted 80 * to measurements of the same type.<p> 81 * 82 * The type of the units is determined automatically from the 83 * units. For example, the unit "grams" is type "mass". Use the 84 * static call {@link Measurement.getAvailableUnits} 85 * to find out what units this version of ilib supports. 86 * 87 * @return {string} the name of the type of this measurement 88 */ 89 VolumeUnit.prototype.getMeasure = function() { 90 return "volume"; 91 }; 92 93 VolumeUnit.aliases = { 94 "US gal": "gallon", 95 "US gallon": "gallon", 96 "US Gal": "gallon", 97 "US Gallons": "gallon", 98 "Gal(US)": "gallon", 99 "gal(US)": "gallon", 100 "gallon": "gallon", 101 "gallons": "gallon", 102 "quart": "quart", 103 "quarts": "quart", 104 "US quart": "quart", 105 "US quarts": "quart", 106 "US Quart": "quart", 107 "US Quarts": "quart", 108 "US qt": "quart", 109 "Qt(US)": "quart", 110 "qt(US)": "quart", 111 "US pint": "pint", 112 "US Pint": "pint", 113 "pint": "pint", 114 "pint(US)": "pint", 115 "Pint(US)": "pint", 116 "US pints": "pint", 117 "US Pints": "pint", 118 "pints": "pint", 119 "pints(US)": "pint", 120 "Pints(US)": "pint", 121 "US cup": "cup", 122 "US Cup": "cup", 123 "cup(US)": "cup", 124 "Cup(US)": "cup", 125 "cup": "cup", 126 "US cups": "cup", 127 "US Cups": "cup", 128 "cups(US)": "cup", 129 "Cups(US)": "cup", 130 "cups": "cup", 131 "us ounce": "fluid-ounce", 132 "US ounce": "fluid-ounce", 133 "us ounces": "fluid-ounce", 134 "US ounces": "fluid-ounce", 135 "fluid ounce": "fluid-ounce", 136 "fluid ounces": "fluid-ounce", 137 "Fluid Ounce": "fluid-ounce", 138 "Fluid Ounces": "fluid-ounce", 139 "℥": "fluid-ounce", 140 "US Oz": "fluid-ounce", 141 "oz(US)": "fluid-ounce", 142 "Oz(US)": "fluid-ounce", 143 "US tbsp": "tablespoon", 144 "tbsp": "tablespoon", 145 "tbsp(US)": "tablespoon", 146 "US tablespoon": "tablespoon", 147 "US tsp": "teaspoon", 148 "US teaspoon": "teaspoon", 149 "tsp(US)": "teaspoon", 150 "tsp": "teaspoon", 151 "Cubic meter": "cubic-meter", 152 "cubic meter": "cubic-meter", 153 "Cubic metre": "cubic-meter", 154 "cubic metre": "cubic-meter", 155 "cu meter": "cubic-meter", 156 "cu metre": "cubic-meter", 157 "Cubic meters": "cubic-meter", 158 "cubic meters": "cubic-meter", 159 "Cubic metres": "cubic-meter", 160 "cubic metres": "cubic-meter", 161 "cu meters": "cubic-meter", 162 "cu metres": "cubic-meter", 163 "cu m": "cubic-meter", 164 "m3": "cubic-meter", 165 "m³": "cubic-meter", 166 "Cubic Centimeter": "cubic-centimeter", 167 "cubic centimeter": "cubic-centimeter", 168 "Cubic Centimetre": "cubic-centimeter", 169 "cubic centimetre": "cubic-centimeter", 170 "cu centimeter": "cubic-centimeter", 171 "cu centimetre": "cubic-centimeter", 172 "Cubic Centimeters": "cubic-centimeter", 173 "cubic centimeters": "cubic-centimeter", 174 "Cubic Centimetres": "cubic-centimeter", 175 "cubic centimetres": "cubic-centimeter", 176 "cu centimeters": "cubic-centimeter", 177 "cu centimetres": "cubic-centimeter", 178 "cu cm": "cubic-centimeter", 179 "cm3": "cubic-centimeter", 180 "cm³": "cubic-centimeter", 181 "cc": "cubic-centimeter", 182 "Liter": "liter", 183 "Liters": "liter", 184 "liter": "liter", 185 "liters": "liter", 186 "L": "liter", 187 "l": "liter", 188 "Milliliter": "milliliter", 189 "ML": "milliliter", 190 "ml": "milliliter", 191 "milliliter": "milliliter", 192 "milliliters": "milliliter", 193 "mL": "milliliter", 194 "Imperial gal": "gallon-imperial", 195 "imperial gallon": "gallon-imperial", 196 "Imperial gallon": "gallon-imperial", 197 "imperial gallons": "gallon-imperial", 198 "Imperial gallons": "gallon-imperial", 199 "gallon(imperial)": "gallon-imperial", 200 "gallon(imp)": "gallon-imperial", 201 "gallons(imperial)": "gallon-imperial", 202 "gallons(imp)": "gallon-imperial", 203 "gal(imperial)": "gallon-imperial", 204 "gal(imp)": "gallon-imperial", 205 "gallon (imperial)": "gallon-imperial", 206 "gallon (imp)": "gallon-imperial", 207 "gallons (imperial)": "gallon-imperial", 208 "gallons (imp)": "gallon-imperial", 209 "gal (imperial)": "gallon-imperial", 210 "gal (imp)": "gallon-imperial", 211 "Imperial quart": "quart-imperial", 212 "imperial quart": "quart-imperial", 213 "Imperial Quart": "quart-imperial", 214 "Imperial quarts": "quart-imperial", 215 "imperial quarts": "quart-imperial", 216 "Imperial Quarts": "quart-imperial", 217 "Imperial qt": "quart-imperial", 218 "qt(Imperial)": "quart-imperial", 219 "qt(Imp)": "quart-imperial", 220 "qt (Imperial)": "quart-imperial", 221 "qt (Imp)": "quart-imperial", 222 "quart(imperial)": "quart-imperial", 223 "quart(imp)": "quart-imperial", 224 "quart (imperial)": "quart-imperial", 225 "quart (imp)": "quart-imperial", 226 "quarts(imperial)": "quart-imperial", 227 "quarts(imp)": "quart-imperial", 228 "quarts (imperial)": "quart-imperial", 229 "quarts (imp)": "quart-imperial", 230 "Imperial pint": "pint-imperial", 231 "imperial pint": "pint-imperial", 232 "Imperial pints": "pint-imperial", 233 "imperial pints": "pint-imperial", 234 "pint(Imperial)": "pint-imperial", 235 "pints(Imperial)": "pint-imperial", 236 "pint(Imp)": "pint-imperial", 237 "pints(Imp)": "pint-imperial", 238 "pint (Imperial)": "pint-imperial", 239 "pints (Imperial)": "pint-imperial", 240 "pint (Imp)": "pint-imperial", 241 "pints (Imp)": "pint-imperial", 242 "imperial cup": "cup-imperial", 243 "Imperial Cup": "cup-imperial", 244 "cup(imperial)": "cup-imperial", 245 "Cup(Imperial)": "cup-imperial", 246 "cup (imperial)": "cup-imperial", 247 "Cup (Imperial)": "cup-imperial", 248 "cup(imp)": "cup-imperial", 249 "Cup(Imp)": "cup-imperial", 250 "cup (imp)": "cup-imperial", 251 "Cup (Imp)": "cup-imperial", 252 "imperial cups": "cup-imperial", 253 "Imperial Cups": "cup-imperial", 254 "cups(imperial)": "cup-imperial", 255 "Cups(Imperial)": "cup-imperial", 256 "cups (imperial)": "cup-imperial", 257 "Cups (Imperial)": "cup-imperial", 258 "cups(imp)": "cup-imperial", 259 "Cups(Imp)": "cup-imperial", 260 "cups (imp)": "cup-imperial", 261 "Cups (Imp)": "cup-imperial", 262 "imperial oz": "ounce-imperial", 263 "imperial ounce": "ounce-imperial", 264 "Imperial Ounce": "ounce-imperial", 265 "imperial ounces": "ounce-imperial", 266 "Imperial Ounces": "ounce-imperial", 267 "Imperial tablespoon": "tablespoon-imperial", 268 "imperial tablespoon": "tablespoon-imperial", 269 "tablespoon(Imperial)": "tablespoon-imperial", 270 "tablespoon(Imp)": "tablespoon-imperial", 271 "tablespoon (Imperial)": "tablespoon-imperial", 272 "tablespoon (Imp)": "tablespoon-imperial", 273 "Imperial tablespoons": "tablespoon-imperial", 274 "imperial tablespoons": "tablespoon-imperial", 275 "tablespoons(Imperial)": "tablespoon-imperial", 276 "tablespoons(Imp)": "tablespoon-imperial", 277 "tablespoons (Imperial)": "tablespoon-imperial", 278 "tablespoons (Imp)": "tablespoon-imperial", 279 "Imperial tbsp": "tablespoon-imperial", 280 "imperial tbsp": "tablespoon-imperial", 281 "tbsp(Imperial)": "tablespoon-imperial", 282 "tbsp(Imp)": "tablespoon-imperial", 283 "tbsp (Imperial)": "tablespoon-imperial", 284 "tbsp (Imp)": "tablespoon-imperial", 285 "Imperial teaspoon": "teaspoon-imperial", 286 "imperial teaspoon": "teaspoon-imperial", 287 "Imperial teaspoons": "teaspoon-imperial", 288 "imperial teaspoons": "teaspoon-imperial", 289 "Imperial tsp": "teaspoon-imperial", 290 "imperial tsp": "teaspoon-imperial", 291 "teaspoon(Imperial)": "teaspoon-imperial", 292 "teaspoon(Imp)": "teaspoon-imperial", 293 "teaspoons(Imperial)": "teaspoon-imperial", 294 "teaspoons(Imp)": "teaspoon-imperial", 295 "tsp(Imperial)": "teaspoon-imperial", 296 "tsp(Imp)": "teaspoon-imperial", 297 "teaspoon (Imperial)": "teaspoon-imperial", 298 "teaspoon (Imp)": "teaspoon-imperial", 299 "teaspoons (Imperial)": "teaspoon-imperial", 300 "teaspoons (Imp)": "teaspoon-imperial", 301 "tsp (Imperial)": "teaspoon-imperial", 302 "tsp (Imp)": "teaspoon-imperial", 303 "Cubic foot": "cubic-foot", 304 "cubic foot": "cubic-foot", 305 "Cubic Foot": "cubic-foot", 306 "Cubic feet": "cubic-foot", 307 "cubic Feet": "cubic-foot", 308 "cubic ft": "cubic-foot", 309 "ft3": "cubic-foot", 310 "Cubic inch": "cubic-inch", 311 "Cubic inches": "cubic-inch", 312 "cubic inches": "cubic-inch", 313 "cubic inch": "cubic-inch", 314 "cubic in": "cubic-inch", 315 "cu in": "cubic-inch", 316 "cu inch": "cubic-inch", 317 "inch³": "cubic-inch", 318 "in³": "cubic-inch", 319 "inch^3": "cubic-inch", 320 "in^3": "cubic-inch", 321 "c.i": "cubic-inch", 322 "CI": "cubic-inch", 323 "cui": "cubic-inch" 324 }; 325 326 (function() { 327 VolumeUnit.aliasesLower = {}; 328 for (var a in VolumeUnit.aliases) { 329 VolumeUnit.aliasesLower[a.toLowerCase()] = VolumeUnit.aliases[a]; 330 } 331 })(); 332 333 334 /** 335 * Convert a volume to another measure. 336 * @static 337 * @param to {string} unit to convert to 338 * @param from {string} unit to convert from 339 * @param volume {number} amount to be convert 340 * @returns {number|undefined} the converted amount 341 */ 342 VolumeUnit.convert = function(to, from, volume) { 343 from = Measurement.getUnitIdCaseInsensitive(VolumeUnit, from) || from; 344 to = Measurement.getUnitIdCaseInsensitive(VolumeUnit, to) || to; 345 var fromRow = VolumeUnit.ratios[from]; 346 var toRow = VolumeUnit.ratios[to]; 347 if (typeof(from) === 'undefined' || typeof(to) === 'undefined') { 348 return undefined; 349 } 350 var result = volume * fromRow[toRow[0]]; 351 return result; 352 }; 353 354 /** 355 * Return a new instance of this type of measurement. 356 * 357 * @param {Object} params parameters to the constructor 358 * @return {Measurement} a measurement subclass instance 359 */ 360 VolumeUnit.prototype.newUnit = function(params) { 361 return new VolumeUnit(params); 362 }; 363 364 /** 365 * @private 366 * @static 367 */ 368 VolumeUnit.getMeasures = function () { 369 return Object.keys(VolumeUnit.ratios); 370 }; 371 372 VolumeUnit.systems = { 373 "metric": [ 374 "milliliter", 375 "liter", 376 "cubic-meter" 377 ], 378 "uscustomary": [ 379 "teaspoon", 380 "tablespoon", 381 "cubic-inch", 382 "fluid-ounce", 383 "cup", 384 "pint", 385 "quart", 386 "gallon", 387 "cubic-foot" 388 ], 389 "imperial": [ 390 "teaspoon-imperial", 391 "tablespoon-imperial", 392 "ounce-imperial", 393 "cup-imperial", 394 "pint-imperial", 395 "quart-imperial", 396 "gallon-imperial" 397 ], 398 "conversions": { 399 "metric": { 400 "uscustomary": { 401 "milliliter": "teaspoon", 402 "cubic-centimeter": "teaspoon", 403 "liter": "quart", 404 "cubic-meter": "cubic-foot" 405 }, 406 "imperial": { 407 "milliliter": "teaspoon-imperial", 408 "cubic-centimeter": "teaspoon-imperial", 409 "liter": "quart-imperial", 410 "cubic-meter": "gallon-imperial" 411 } 412 }, 413 "imperial": { 414 "metric": { 415 "teaspoon-imperial": "milliliter", 416 "tablespoon-imperial": "milliliter", 417 "ounce-imperial": "milliliter", 418 "pint-imperial": "liter", 419 "quart-imperial": "liter", 420 "gallon-imperial": "cubic-meter" 421 }, 422 "uscustomary": { 423 "teaspoon-imperial": "teaspoon", 424 "tablespoon-imperial": "tablespoon", 425 "ounce-imperial": "fluid-ounce", 426 "pint-imperial": "pint", 427 "quart-imperial": "quart", 428 "gallon-imperial": "gallon" 429 } 430 }, 431 "uscustomary": { 432 "imperial": { 433 "teaspoon": "teaspoon-imperial", 434 "tablespoon": "tablespoon-imperial", 435 "cubic-inch": "tablespoon-imperial", 436 "fluid-ounce": "ounce-imperial", 437 "cup": "ounce-imperial", 438 "pint": "pint-imperial", 439 "quart": "quart-imperial", 440 "gallon": "gallon-imperial", 441 "cubic-foot": "gallon-imperial" 442 }, 443 "metric": { 444 "teaspoon": "milliliter", 445 "tablespoon": "milliliter", 446 "cubic-inch": "milliliter", 447 "fluid-ounce": "milliliter", 448 "cup": "milliliter", 449 "pint": "liter", 450 "quart": "liter", 451 "gallon": "cubic-meter", 452 "cubic-foot": "cubic-meter" 453 } 454 } 455 } 456 }; 457 458 //register with the factory method 459 Measurement._constructors["volume"] = VolumeUnit; 460 461 module.exports = VolumeUnit; 462