1 /* 2 * UnknownUnit.js - Dummy unit conversions for unknown types 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 // !depends Measurement.js 21 22 var Measurement = require("./Measurement.js"); 23 24 /** 25 * @class 26 * Create a new unknown measurement instance. 27 * 28 * @constructor 29 * @extends Measurement 30 * @param options {{unit:string,amount:number|string|undefined}} Options controlling 31 * the construction of this instance 32 */ 33 var UnknownUnit = function (options) { 34 35 this.ratios = {}; 36 this.aliases = UnknownUnit.aliases; 37 this.aliasesLower = UnknownUnit.aliases; 38 this.systems = UnknownUnit.systems; 39 40 if (options) { 41 this.unit = options.unit; 42 this.amount = options.amount; 43 } 44 }; 45 46 UnknownUnit.prototype = new Measurement(); 47 UnknownUnit.prototype.parent = Measurement; 48 UnknownUnit.prototype.constructor = UnknownUnit; 49 50 UnknownUnit.systems = { 51 "metric": [], 52 "uscustomary": [], 53 "imperial": [], 54 "conversions": { 55 "metric": {}, 56 "uscustomary": {}, 57 "imperial": {} 58 } 59 }; 60 61 UnknownUnit.aliases = { 62 "unknown":"unknown" 63 }; 64 65 /** 66 * Return the type of this measurement. Examples are "mass", 67 * "length", "speed", etc. Measurements can only be converted 68 * to measurements of the same type.<p> 69 * 70 * The type of the units is determined automatically from the 71 * units. For example, the unit "grams" is type "mass". Use the 72 * static call {@link Measurement.getAvailableUnits} 73 * to find out what units this version of ilib supports. 74 * 75 * @return {string} the name of the type of this measurement 76 */ 77 UnknownUnit.prototype.getMeasure = function() { 78 return "unknown"; 79 }; 80 81 /** 82 * Return a new measurement instance that is converted to a new 83 * measurement unit. Measurements can only be converted 84 * to measurements of the same type.<p> 85 * 86 * @param {string} to The name of the units to convert to 87 * @return {number|undefined} the converted measurement 88 * or undefined if the requested units are for a different 89 * measurement type 90 */ 91 UnknownUnit.prototype.convert = function(to) { 92 return undefined; 93 }; 94 95 /** 96 * Convert a unknown to another measure. 97 * @static 98 * @param {string} to unit to convert to 99 * @param {string} from unit to convert from 100 * @param {number} unknown amount to be convert 101 * @returns {number|undefined} the converted amount 102 */ 103 UnknownUnit.convert = function(to, from, unknown) { 104 return undefined; 105 }; 106 107 /** 108 * Localize the measurement to the commonly used measurement in that locale. For example 109 * If a user's locale is "en-US" and the measurement is given as "60 kmh", 110 * the formatted number should be automatically converted to the most appropriate 111 * measure in the other system, in this case, mph. The formatted result should 112 * appear as "37.3 mph". 113 * 114 * @param {string} locale current locale string 115 * @returns {Measurement} a new instance that is converted to locale 116 */ 117 UnknownUnit.prototype.localize = function(locale) { 118 return new UnknownUnit({ 119 unit: this.unit, 120 amount: this.amount 121 }); 122 }; 123 124 /** 125 * Scale the measurement unit to an acceptable level. The scaling 126 * happens so that the integer part of the amount is as small as 127 * possible without being below zero. This will result in the 128 * largest units that can represent this measurement without 129 * fractions. Measurements can only be scaled to other measurements 130 * of the same type. 131 * 132 * @param {string=} measurementsystem system to use (uscustomary|imperial|metric), 133 * or undefined if the system can be inferred from the current measure 134 * @return {Measurement} a new instance that is scaled to the 135 * right level 136 */ 137 UnknownUnit.prototype.scale = function(measurementsystem) { 138 return new UnknownUnit({ 139 unit: this.unit, 140 amount: this.amount 141 }); 142 }; 143 144 /** 145 * Expand the current measurement such that any fractions of the current unit 146 * are represented in terms of smaller units in the same system instead of fractions 147 * of the current unit. For example, "6.25 feet" may be represented as 148 * "6 feet 4 inches" instead. The return value is an array of measurements which 149 * are progressively smaller until the smallest unit in the system is reached 150 * or until there is a whole number of any unit along the way. 151 * 152 * @param {string=} measurementsystem system to use (uscustomary|imperial|metric), 153 * or undefined if the system can be inferred from the current measure 154 * @return {Array.<Measurement>} an array of new measurements in order from 155 * the current units to the smallest units in the system which together are the 156 * same measurement as this one 157 */ 158 UnknownUnit.prototype.expand = function(measurementsystem) { 159 return [this]; // nothing to expand 160 } 161 162 /** 163 * @private 164 * @static 165 */ 166 UnknownUnit.getMeasures = function () { 167 return []; 168 }; 169 170 module.exports = UnknownUnit; 171