1 /*
  2  * MassUnit.js - Unit conversions for weight/mass 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 mass 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 MassUnit = function (options) {
 37     this.unit = "gram";
 38     this.amount = 0;
 39 
 40     this.ratios = MassUnit.ratios;
 41     this.aliases = MassUnit.aliases;
 42     this.aliasesLower = MassUnit.aliasesLower;
 43     this.systems = MassUnit.systems;
 44 
 45     this.parent.call(this, options);
 46 };
 47 
 48 MassUnit.prototype = new Measurement();
 49 MassUnit.prototype.parent = Measurement;
 50 MassUnit.prototype.constructor = MassUnit;
 51 
 52 MassUnit.ratios = {
 53     /*             index  µg          mg         g          oz          lb           kg          st            sh ton       mt ton        ln ton      */
 54     "microgram":   [ 1,   1,          0.001,     1e-6,      3.5274e-8,  2.2046e-9,   1e-9,       1.5747e-10,   1.1023e-12,  1e-12,        9.8421e-13   ],
 55     "milligram":   [ 2,   1000,       1,         0.001,     3.5274e-5,  2.2046e-6,   1e-6,       1.5747e-7,    1.1023e-9,   1e-9,         9.8421e-10   ],
 56     "gram":        [ 3,   1e+6,       1000,      1,         0.035274,   0.00220462,  0.001,      0.000157473,  1.1023e-6,   1e-6,         9.8421e-7    ],
 57     "ounce":       [ 4,   2.835e+7,   28349.5,   28.3495,   1,          0.0625,      0.0283495,  0.00446429,   3.125e-5,    2.835e-5,     2.7902e-5    ],
 58     "pound":       [ 5,   4.536e+8,   453592,    453.592,   16,         1,           0.453592,   0.0714286,    0.0005,      0.000453592,  0.000446429  ],
 59     "kilogram":    [ 6,   1e+9,       1e+6,      1000,      35.274,     2.20462,     1,          0.157473,     0.00110231,  0.001,        0.000984207  ],
 60     "stone":       [ 7,   6.35e+9,    6.35e+6,   6350.29,   224,        14,          6.35029,    1,            0.007,       0.00635029,   0.00625      ],
 61     "short-ton":   [ 8,   9.072e+11,  9.072e+8,  907185,    32000,      2000,        907.185,    142.857,      1,           0.907185,     0.892857     ],
 62     "metric-ton":  [ 9,   1e+12,      1e+9,      1e+6,      35274,      2204.62,     1000,       157.473,      1.10231,     1,            0.984207     ],
 63     "long-ton":    [ 10,  1.016e+12,  1.016e+9,  1.016e+6,  35840,      2240,        1016.05,    160,          1.12,        1.01605,      1            ]
 64 };
 65 
 66 /**
 67  * Return a new instance of this type of measurement.
 68  *
 69  * @param {Object} params parameters to the constructor
 70  * @return {Measurement} a measurement subclass instance
 71  */
 72 MassUnit.prototype.newUnit = function(params) {
 73     return new MassUnit(params);
 74 };
 75 
 76 MassUnit.systems = {
 77     "metric": [
 78         "microgram",
 79         "milligram",
 80         "gram",
 81         "kilogram",
 82         "metric-ton"
 83     ],
 84     "imperial": [
 85         "ounce",
 86         "pound",
 87         "stone",
 88         "long-ton"
 89     ],
 90     "uscustomary": [
 91         "ounce",
 92         "pound",
 93         "short-ton"
 94     ],
 95     "conversions": {
 96         "metric": {
 97             "uscustomary": {
 98                 "microgram": "ounce",
 99                 "milligram": "ounce",
100                 "gram": "ounce",
101                 "kilogram": "pound",
102                 "metric-ton": "short-ton"
103             },
104             "imperial": {
105                 "microgram": "ounce",
106                 "milligram": "ounce",
107                 "gram": "ounce",
108                 "kilogram": "pound",
109                 "metric-ton": "long-ton"
110             }
111         },
112         "uscustomary": {
113             "imperial": {
114                 "ounce": "ounce",
115                 "pound": "pound",
116                 "short-ton": "long-ton"
117             },
118             "metric": {
119                 "ounce": "gram",
120                 "pound": "kilogram",
121                 "short-ton": "metric-ton"
122             }
123         },
124         "imperial": {
125             "uscustomary": {
126                 "ounce": "ounce",
127                 "pound": "pound",
128                 "stone": "pound",
129                 "long-ton": "short-ton"
130             },
131             "metric": {
132                 "ounce": "gram",
133                 "pound": "kilogram",
134                 "stone": "kilogram",
135                 "long-ton": "metric-ton"
136             }
137         }
138     }
139 };
140 
141 /**
142  * Return the type of this measurement. Examples are "mass",
143  * "length", "speed", etc. Measurements can only be converted
144  * to measurements of the same type.<p>
145  *
146  * The type of the units is determined automatically from the
147  * units. For example, the unit "grams" is type "mass". Use the
148  * static call {@link Measurement.getAvailableUnits}
149  * to find out what units this version of ilib supports.
150  *
151  * @return {string} the name of the type of this measurement
152  */
153 MassUnit.prototype.getMeasure = function() {
154     return "mass";
155 };
156 
157 MassUnit.aliases = {
158     "µg":"microgram",
159     "microgram":"microgram",
160     "mcg":"microgram",
161     "milligram":"milligram",
162     "mg":"milligram",
163     "milligrams":"milligram",
164     "Milligram":"milligram",
165     "Milligrams":"milligram",
166     "MilliGram":"milligram",
167     "MilliGrams":"milligram",
168     "g":"gram",
169     "gram":"gram",
170     "grams":"gram",
171     "Gram":"gram",
172     "Grams":"gram",
173     "ounce":"ounce",
174     "oz":"ounce",
175     "Ounce":"ounce",
176     "ounces":"ounce",
177     "Ounces":"ounce",
178     "℥":"ounce",
179     "pound":"pound",
180     "poundm":"pound",
181     "℔":"pound",
182     "lb":"pound",
183     "lbs":"pound",
184     "pounds":"pound",
185     "Pound":"pound",
186     "Pounds":"pound",
187     "kilogram":"kilogram",
188     "kg":"kilogram",
189     "kilograms":"kilogram",
190     "kilo grams":"kilogram",
191     "kilo gram":"kilogram",
192     "Kilogram":"kilogram",
193     "Kilograms":"kilogram",
194     "KiloGram":"kilogram",
195     "KiloGrams":"kilogram",
196     "Kilo gram":"kilogram",
197     "Kilo grams":"kilogram",
198     "Kilo Gram":"kilogram",
199     "Kilo Grams":"kilogram",
200     "stone":"stone",
201     "st":"stone",
202     "stones":"stone",
203     "Stone":"stone",
204     "metric ton":"metric-ton",
205     "metricton":"metric-ton",
206     "t":"metric-ton",
207     "tonne":"metric-ton",
208     "tonnes":"metric-ton",
209     "Tonne":"metric-ton",
210     "Metric Ton":"metric-ton",
211     "MetricTon":"metric-ton",
212     "long ton":"long-ton",
213     "longton":"long-ton",
214     "Longton":"long-ton",
215     "Long ton":"long-ton",
216     "Long Ton":"long-ton",
217     "short ton":"short-ton",
218     "short tons":"short-ton",
219     "Short ton":"short-ton",
220     "Short Ton":"short-ton",
221     "ton":"short-ton",
222     "tons":"short-ton",
223     "Ton":"short-ton"
224 };
225 
226 (function() {
227     MassUnit.aliasesLower = {};
228     for (var a in MassUnit.aliases) {
229         MassUnit.aliasesLower[a.toLowerCase()] = MassUnit.aliases[a];
230     }
231 })();
232 
233 /**
234  * Convert a mass to another measure.
235  * @static
236  * @param to {string} unit to convert to
237  * @param from {string} unit to convert from
238  * @param mass {number} amount to be convert
239  * @returns {number|undefined} the converted amount
240  */
241 MassUnit.convert = function(to, from, mass) {
242     from = Measurement.getUnitIdCaseInsensitive(MassUnit, from) || from;
243     to = Measurement.getUnitIdCaseInsensitive(MassUnit, to) || to;
244     var fromRow = MassUnit.ratios[from];
245     var toRow = MassUnit.ratios[to];
246     if (typeof(from) === 'undefined' || typeof(to) === 'undefined') {
247         return undefined;
248     }
249     return mass * fromRow[toRow[0]];
250 };
251 
252 /**
253  * @private
254  * @static
255  */
256 MassUnit.getMeasures = function () {
257     return Object.keys(MassUnit.ratios);
258 };
259 
260 //register with the factory method
261 Measurement._constructors["mass"] = MassUnit;
262 
263 module.exports = MassUnit;
264