@lhncbc/ucum-lhc 5.0.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -12
- package/browser-dist/ucum-lhc.js +731 -1265
- package/data/ucumDefs.min.json +1 -1
- package/package.json +1 -1
- package/source/ucumLhcUtils.js +2 -2
- package/source/ucumXmlDocument.js +5 -2
- package/source/unit.js +2 -1
- package/source/unitString.js +142 -130
- package/source-cjs/config.js +1 -12
- package/source-cjs/config.js.map +1 -1
- package/source-cjs/dimension.js +20 -63
- package/source-cjs/dimension.js.map +1 -1
- package/source-cjs/jsonArrayPack.js +7 -25
- package/source-cjs/jsonArrayPack.js.map +1 -1
- package/source-cjs/prefix.js +12 -26
- package/source-cjs/prefix.js.map +1 -1
- package/source-cjs/prefixTables.js +10 -24
- package/source-cjs/prefixTables.js.map +1 -1
- package/source-cjs/ucumFunctions.js +35 -32
- package/source-cjs/ucumFunctions.js.map +1 -1
- package/source-cjs/ucumInternalUtils.js +5 -13
- package/source-cjs/ucumInternalUtils.js.map +1 -1
- package/source-cjs/ucumJsonDefs.js +1 -16
- package/source-cjs/ucumJsonDefs.js.map +1 -1
- package/source-cjs/ucumLhcUtils.js +28 -92
- package/source-cjs/ucumLhcUtils.js.map +1 -1
- package/source-cjs/ucumPkg.js +1 -6
- package/source-cjs/ucumPkg.js.map +1 -1
- package/source-cjs/ucumXmlDocument.js +162 -184
- package/source-cjs/ucumXmlDocument.js.map +1 -1
- package/source-cjs/unit.js +97 -181
- package/source-cjs/unit.js.map +1 -1
- package/source-cjs/unitString.js +521 -608
- package/source-cjs/unitString.js.map +1 -1
- package/source-cjs/unitTables.js +33 -139
- package/source-cjs/unitTables.js.map +1 -1
package/source-cjs/unit.js
CHANGED
|
@@ -4,17 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.Unit = void 0;
|
|
7
|
-
|
|
8
7
|
var _ucumFunctions = _interopRequireDefault(require("./ucumFunctions.js"));
|
|
9
|
-
|
|
10
8
|
var intUtils_ = _interopRequireWildcard(require("./ucumInternalUtils.js"));
|
|
11
|
-
|
|
12
9
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
13
|
-
|
|
14
10
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
-
|
|
16
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
|
|
18
12
|
/**
|
|
19
13
|
* This class represents one unit of measure. It includes
|
|
20
14
|
* functions to cover constructor, accessor, and assignment tasks as
|
|
@@ -25,13 +19,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
25
19
|
*
|
|
26
20
|
*/
|
|
27
21
|
var Ucum = require('./config.js').Ucum;
|
|
28
|
-
|
|
29
22
|
var Dimension = require('./dimension.js').Dimension;
|
|
30
|
-
|
|
31
23
|
var UnitTables;
|
|
32
|
-
|
|
33
24
|
var isInteger = require("is-integer");
|
|
34
|
-
|
|
35
25
|
class Unit {
|
|
36
26
|
/**
|
|
37
27
|
* Constructor.
|
|
@@ -63,26 +53,27 @@ class Unit {
|
|
|
63
53
|
* Flag indicating whether or not this is a base unit
|
|
64
54
|
*/
|
|
65
55
|
this.isBase_ = attrs['isBase_'] || false;
|
|
56
|
+
|
|
66
57
|
/*
|
|
67
58
|
* The unit name, e.g., meter
|
|
68
59
|
*/
|
|
69
|
-
|
|
70
60
|
this.name_ = attrs['name_'] || '';
|
|
61
|
+
|
|
71
62
|
/*
|
|
72
63
|
* The unit's case-sensitive code, e.g., m
|
|
73
64
|
*/
|
|
74
|
-
|
|
75
65
|
this.csCode_ = attrs['csCode_'] || '';
|
|
66
|
+
|
|
76
67
|
/*
|
|
77
68
|
* The unit's case-insensitive code, e.g., M
|
|
78
69
|
*/
|
|
79
|
-
|
|
80
70
|
this.ciCode_ = attrs['ciCode_'] || '';
|
|
71
|
+
|
|
81
72
|
/*
|
|
82
73
|
* The unit's property, e.g., length
|
|
83
74
|
*/
|
|
84
|
-
|
|
85
75
|
this.property_ = attrs['property_'] || '';
|
|
76
|
+
|
|
86
77
|
/*
|
|
87
78
|
* The magnitude of the unit, e.g., 3600/3937 for a yard,
|
|
88
79
|
* where a yard - 3600/3973 * m(eter). The Dimension
|
|
@@ -90,70 +81,68 @@ class Unit {
|
|
|
90
81
|
* a yard is based, and this magnitude specifies how to figure
|
|
91
82
|
* this unit based on the base unit.
|
|
92
83
|
*/
|
|
93
|
-
|
|
94
84
|
this.magnitude_ = attrs['magnitude_'] || 1;
|
|
85
|
+
|
|
95
86
|
/*
|
|
96
87
|
* The Dimension object of the unit
|
|
97
88
|
*/
|
|
98
|
-
|
|
99
89
|
if (attrs['dim_'] === undefined || attrs['dim_'] === null) {
|
|
100
90
|
this.dim_ = new Dimension();
|
|
101
|
-
}
|
|
91
|
+
}
|
|
92
|
+
// When the unit data stored in json format is reloaded, the dimension data
|
|
102
93
|
// is recognized as a a hash, not as a Dimension object.
|
|
103
94
|
else if (attrs['dim_']['dimVec_'] !== undefined) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
95
|
+
this.dim_ = new Dimension(attrs['dim_']['dimVec_']);
|
|
96
|
+
} else if (attrs['dim_'] instanceof Dimension) {
|
|
97
|
+
this.dim_ = attrs['dim_'];
|
|
98
|
+
} else if (attrs['dim_'] instanceof Array || isInteger(attrs['dim_'])) {
|
|
99
|
+
this.dim_ = new Dimension(attrs['dim_']);
|
|
100
|
+
} else {
|
|
101
|
+
this.dim_ = new Dimension();
|
|
102
|
+
}
|
|
112
103
|
/*
|
|
113
104
|
* The print symbol of the unit, e.g., m
|
|
114
105
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
106
|
this.printSymbol_ = attrs['printSymbol_'] || null;
|
|
107
|
+
|
|
118
108
|
/*
|
|
119
109
|
* The class of the unit, where given, e.g., dimless
|
|
120
110
|
*/
|
|
121
|
-
|
|
122
111
|
this.class_ = attrs['class_'] || null;
|
|
112
|
+
|
|
123
113
|
/*
|
|
124
114
|
* A flag indicating whether or not the unit is metric
|
|
125
115
|
*/
|
|
126
|
-
|
|
127
116
|
this.isMetric_ = attrs['isMetric_'] || false;
|
|
117
|
+
|
|
128
118
|
/*
|
|
129
119
|
* The "variable" - which I think is used only for base units
|
|
130
120
|
* The symbol for the variable as used in equations, e.g., s for distance
|
|
131
121
|
*/
|
|
132
|
-
|
|
133
122
|
this.variable_ = attrs['variable_'] || null; // comes from 'dim' in XML
|
|
134
123
|
|
|
135
124
|
/*
|
|
136
125
|
* The conversion function
|
|
137
126
|
*/
|
|
138
|
-
|
|
139
127
|
this.cnv_ = attrs['cnv_'] || null;
|
|
128
|
+
|
|
140
129
|
/*
|
|
141
130
|
* The conversion prefix
|
|
142
131
|
*/
|
|
143
|
-
|
|
144
132
|
this.cnvPfx_ = attrs['cnvPfx_'] || 1;
|
|
133
|
+
|
|
145
134
|
/*
|
|
146
135
|
* Flag indicating whether or not this is a "special" unit, i.e., is
|
|
147
136
|
* constructed using a function specific to the measurement, e.g.,
|
|
148
137
|
* fahrenheit and celsius
|
|
149
138
|
*/
|
|
150
|
-
|
|
151
139
|
this.isSpecial_ = attrs['isSpecial_'] || false;
|
|
140
|
+
|
|
152
141
|
/*
|
|
153
142
|
* Flag indicating whether or not this is an arbitrary unit
|
|
154
143
|
*/
|
|
155
|
-
|
|
156
144
|
this.isArbitrary_ = attrs['isArbitrary_'] || false;
|
|
145
|
+
|
|
157
146
|
/*
|
|
158
147
|
* Integer indicating what level of exponent applies to a mole-based portion
|
|
159
148
|
* of the unit. So, for the unit "mol", this will be 1. For "mol2" this
|
|
@@ -161,8 +150,8 @@ class Unit {
|
|
|
161
150
|
* a mole will have a 0 in this field. This is used to determine
|
|
162
151
|
* commensurability for mole<->mass conversions.
|
|
163
152
|
*/
|
|
164
|
-
|
|
165
153
|
this.moleExp_ = attrs['moleExp_'] || 0;
|
|
154
|
+
|
|
166
155
|
/*
|
|
167
156
|
* Added when added LOINC list of units
|
|
168
157
|
* synonyms are used by the autocompleter to enhance lookup capabilities
|
|
@@ -170,17 +159,16 @@ class Unit {
|
|
|
170
159
|
* UCUM - which are units from the unitsofmeasure.org list and LOINC -
|
|
171
160
|
* which are units from the LOINC data.
|
|
172
161
|
*/
|
|
173
|
-
|
|
174
162
|
this.synonyms_ = attrs['synonyms_'] || null;
|
|
175
163
|
this.source_ = attrs['source_'] || null;
|
|
176
164
|
this.loincProperty_ = attrs['loincProperty_'] || null;
|
|
177
165
|
this.category_ = attrs['category_'] || null;
|
|
178
166
|
this.guidance_ = attrs['guidance_'] || null;
|
|
167
|
+
|
|
179
168
|
/*
|
|
180
169
|
* Used to compute dimension; storing for now until I complete
|
|
181
170
|
* unit definition parsing
|
|
182
171
|
*/
|
|
183
|
-
|
|
184
172
|
/*
|
|
185
173
|
* Case sensitive (cs) and case insensitive (ci) base unit strings,
|
|
186
174
|
* includes exponent and prefix if applicable - specified in
|
|
@@ -193,16 +181,16 @@ class Unit {
|
|
|
193
181
|
* (unit code h) is created from the unit min (minute) with a value
|
|
194
182
|
* of 60.
|
|
195
183
|
*/
|
|
196
|
-
|
|
197
184
|
this.csUnitString_ = attrs['csUnitString_'] || null;
|
|
198
185
|
this.ciUnitString_ = attrs['ciUnitString_'] || null;
|
|
186
|
+
|
|
199
187
|
/*
|
|
200
188
|
* String and numeric versions of factor applied to unit specified in
|
|
201
189
|
* <value Unit=x UNIT=X value="nnn">nnn</value> -- the value part
|
|
202
190
|
*/
|
|
203
|
-
|
|
204
191
|
this.baseFactorStr_ = attrs['baseFactorStr_'] || null;
|
|
205
192
|
this.baseFactor_ = attrs['baseFactor_'] || null;
|
|
193
|
+
|
|
206
194
|
/*
|
|
207
195
|
* Flag used to indicate units where the definition process failed
|
|
208
196
|
* when parsing units from the official units definitions file
|
|
@@ -215,7 +203,6 @@ class Unit {
|
|
|
215
203
|
* This is NOT used when trying to validate a unit string
|
|
216
204
|
* submitted during a conversion or validation attempt.
|
|
217
205
|
*/
|
|
218
|
-
|
|
219
206
|
this.defError_ = attrs['defError_'] || false;
|
|
220
207
|
} // end constructor
|
|
221
208
|
|
|
@@ -224,8 +211,6 @@ class Unit {
|
|
|
224
211
|
*
|
|
225
212
|
* @return this unit
|
|
226
213
|
*/
|
|
227
|
-
|
|
228
|
-
|
|
229
214
|
assignUnity() {
|
|
230
215
|
this.name_ = "";
|
|
231
216
|
this.magnitude_ = 1;
|
|
@@ -246,8 +231,6 @@ class Unit {
|
|
|
246
231
|
* of name_.
|
|
247
232
|
* @return nothing
|
|
248
233
|
*/
|
|
249
|
-
|
|
250
|
-
|
|
251
234
|
assignVals(vals) {
|
|
252
235
|
for (let key in vals) {
|
|
253
236
|
let uKey = !key.charAt(key.length - 1) === '_' ? key + '_' : key;
|
|
@@ -260,8 +243,6 @@ class Unit {
|
|
|
260
243
|
*
|
|
261
244
|
* @return the clone
|
|
262
245
|
*/
|
|
263
|
-
|
|
264
|
-
|
|
265
246
|
clone() {
|
|
266
247
|
let retUnit = new Unit();
|
|
267
248
|
Object.getOwnPropertyNames(this).forEach(val => {
|
|
@@ -278,8 +259,6 @@ class Unit {
|
|
|
278
259
|
* @param unit2 the unit whose properties are to be assigned to this one.
|
|
279
260
|
* @return nothing; this unit is updated
|
|
280
261
|
*/
|
|
281
|
-
|
|
282
|
-
|
|
283
262
|
assign(unit2) {
|
|
284
263
|
Object.getOwnPropertyNames(unit2).forEach(val => {
|
|
285
264
|
if (val === 'dim_') {
|
|
@@ -299,8 +278,6 @@ class Unit {
|
|
|
299
278
|
* @param unit2 the unit whose properties are to be checked.
|
|
300
279
|
* @return boolean indicating whether or not they match
|
|
301
280
|
*/
|
|
302
|
-
|
|
303
|
-
|
|
304
281
|
equals(unit2) {
|
|
305
282
|
return this.magnitude_ === unit2.magnitude_ && this.cnv_ === unit2.cnv_ && this.cnvPfx_ === unit2.cnvPfx_ && (this.dim_ === null && unit2.dim_ === null || this.dim_.equals(unit2.dim_));
|
|
306
283
|
} // end equals
|
|
@@ -312,22 +289,19 @@ class Unit {
|
|
|
312
289
|
* @param unit2 the unit that is to be compared to this unit
|
|
313
290
|
* @return boolean indicating whether or not every attribute matches
|
|
314
291
|
*/
|
|
315
|
-
|
|
316
|
-
|
|
317
292
|
fullEquals(unit2) {
|
|
318
293
|
let thisAttr = Object.keys(this).sort();
|
|
319
294
|
let u2Attr = Object.keys(unit2).sort();
|
|
320
295
|
let keyLen = thisAttr.length;
|
|
321
|
-
let match = keyLen === u2Attr.length;
|
|
322
|
-
// the equals function of the Dimension class.
|
|
296
|
+
let match = keyLen === u2Attr.length;
|
|
323
297
|
|
|
298
|
+
// check each attribute. Dimension objects have to checked using
|
|
299
|
+
// the equals function of the Dimension class.
|
|
324
300
|
for (let k = 0; k < keyLen && match; k++) {
|
|
325
301
|
if (thisAttr[k] === u2Attr[k]) {
|
|
326
302
|
if (thisAttr[k] === 'dim_') match = this.dim_.equals(unit2.dim_);else match = this[thisAttr[k]] === unit2[thisAttr[k]];
|
|
327
303
|
} else match = false;
|
|
328
304
|
} // end do for each key and attribute
|
|
329
|
-
|
|
330
|
-
|
|
331
305
|
return match;
|
|
332
306
|
} // end of fullEquals
|
|
333
307
|
|
|
@@ -340,8 +314,6 @@ class Unit {
|
|
|
340
314
|
* @return the requested property, if found for this unit
|
|
341
315
|
* @throws an error if the property is not found for this unit
|
|
342
316
|
*/
|
|
343
|
-
|
|
344
|
-
|
|
345
317
|
getProperty(propertyName) {
|
|
346
318
|
let uProp = propertyName.charAt(propertyName.length - 1) === '_' ? propertyName : propertyName + '_';
|
|
347
319
|
return this[uProp];
|
|
@@ -366,13 +338,12 @@ class Unit {
|
|
|
366
338
|
* @throws an error if the dimension of the fromUnit differs from this unit's
|
|
367
339
|
* dimension
|
|
368
340
|
*/
|
|
369
|
-
|
|
370
|
-
|
|
371
341
|
convertFrom(num, fromUnit) {
|
|
372
342
|
let newNum = 0.0;
|
|
373
343
|
if (this.isArbitrary_) throw new Error(`Attempt to convert to arbitrary unit "${this.csCode_}"`);
|
|
374
|
-
if (fromUnit.isArbitrary_) throw new Error(`Attempt to convert arbitrary unit "${fromUnit.csCode_}"`);
|
|
344
|
+
if (fromUnit.isArbitrary_) throw new Error(`Attempt to convert arbitrary unit "${fromUnit.csCode_}"`);
|
|
375
345
|
|
|
346
|
+
// reject request if both units have dimensions that are not equal
|
|
376
347
|
if (fromUnit.dim_ && this.dim_ && !fromUnit.dim_.equals(this.dim_)) {
|
|
377
348
|
// check first to see if a mole<->mass conversion is appropriate
|
|
378
349
|
if (this.isMoleMassCommensurable(fromUnit)) {
|
|
@@ -380,42 +351,36 @@ class Unit {
|
|
|
380
351
|
} else {
|
|
381
352
|
throw new Error(`Sorry. ${fromUnit.csCode_} cannot be converted ` + `to ${this.csCode_}.`);
|
|
382
353
|
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
|
|
354
|
+
}
|
|
355
|
+
// reject request if there is a "from" dimension but no "to" dimension
|
|
386
356
|
if (fromUnit.dim_ && (!this.dim_ || this.dim_.isNull())) {
|
|
387
357
|
throw new Error(`Sorry. ${fromUnit.csCode_} cannot be converted ` + `to ${this.csCode_}.`);
|
|
388
|
-
}
|
|
389
|
-
|
|
358
|
+
}
|
|
390
359
|
|
|
360
|
+
// reject request if there is a "to" dimension but no "from" dimension
|
|
391
361
|
if (this.dim_ && (!fromUnit.dim_ || fromUnit.dim_.isNull())) {
|
|
392
362
|
throw new Error(`Sorry. ${fromUnit.csCode_} cannot be converted ` + `to ${this.csCode_}.`);
|
|
393
363
|
}
|
|
394
|
-
|
|
395
364
|
let fromCnv = fromUnit.cnv_;
|
|
396
365
|
let fromMag = fromUnit.magnitude_;
|
|
397
366
|
let x;
|
|
398
|
-
|
|
399
367
|
if (fromCnv != null) {
|
|
400
368
|
// turn num * fromUnit.magnitude into its ratio scale equivalent,
|
|
401
369
|
// e.g., convert Celsius to Kelvin
|
|
402
370
|
let fromFunc = _ucumFunctions.default.forName(fromCnv);
|
|
403
|
-
|
|
404
|
-
|
|
371
|
+
x = fromFunc.cnvFrom(num * fromUnit.cnvPfx_) * fromMag;
|
|
372
|
+
//x = fromFunc.cnvFrom(num * fromMag) * fromUnit.cnvPfx_;
|
|
405
373
|
} else {
|
|
406
374
|
x = num * fromMag;
|
|
407
375
|
}
|
|
408
|
-
|
|
409
376
|
if (this.cnv_ != null) {
|
|
410
377
|
// turn mag * origUnit on ratio scale into a non-ratio unit,
|
|
411
378
|
// e.g. convert Kelvin to Fahrenheit
|
|
412
379
|
let toFunc = _ucumFunctions.default.forName(this.cnv_);
|
|
413
|
-
|
|
414
380
|
newNum = toFunc.cnvTo(x / this.magnitude_) / this.cnvPfx_;
|
|
415
381
|
} else {
|
|
416
382
|
newNum = x / this.magnitude_;
|
|
417
383
|
}
|
|
418
|
-
|
|
419
384
|
return newNum;
|
|
420
385
|
} // end convertFrom
|
|
421
386
|
|
|
@@ -435,8 +400,6 @@ class Unit {
|
|
|
435
400
|
* @throws an error if the dimension of the toUnit differs from this unit's
|
|
436
401
|
* dimension
|
|
437
402
|
*/
|
|
438
|
-
|
|
439
|
-
|
|
440
403
|
convertTo(num, toUnit) {
|
|
441
404
|
return toUnit.convertFrom(num, this);
|
|
442
405
|
} // end convertTo
|
|
@@ -451,8 +414,6 @@ class Unit {
|
|
|
451
414
|
* @param num the number for the coherent version of this unit
|
|
452
415
|
* @return the number for the coherent version of this unit
|
|
453
416
|
*/
|
|
454
|
-
|
|
455
|
-
|
|
456
417
|
convertCoherent(num) {
|
|
457
418
|
// convert mag' * u' into canonical number * u on ratio scale
|
|
458
419
|
if (this.cnv_ !== null) num = this.cnv_.f_from(num / this.cnvPfx_) * this.magnitude_;
|
|
@@ -467,30 +428,27 @@ class Unit {
|
|
|
467
428
|
* @return the number of this unit after conversion
|
|
468
429
|
* @throws an error if the dimensions differ
|
|
469
430
|
*/
|
|
470
|
-
|
|
471
|
-
|
|
472
431
|
mutateCoherent(num) {
|
|
473
432
|
// convert mu' * u' into canonical mu * u on ratio scale
|
|
474
|
-
num = this.convertCoherent(num);
|
|
433
|
+
num = this.convertCoherent(num);
|
|
475
434
|
|
|
435
|
+
// mutate to coherent unit
|
|
476
436
|
this.magnitude_ = 1;
|
|
477
437
|
this.cnv_ = null;
|
|
478
438
|
this.cnvPfx_ = 1;
|
|
479
|
-
this.name_ = "";
|
|
439
|
+
this.name_ = "";
|
|
440
|
+
|
|
441
|
+
// build a name as a term of coherent base units
|
|
480
442
|
// This is probably ALL WRONG and a HORRIBLE MISTAKE
|
|
481
443
|
// but until we figure out what the heck the name being
|
|
482
444
|
// built here really is, it will have to stay.
|
|
483
|
-
|
|
484
445
|
for (let i = 0, max = Dimension.getMax(); i < max; i++) {
|
|
485
446
|
let elem = this.dim_.getElementAt(i);
|
|
486
|
-
|
|
487
447
|
let tabs = this._getUnitTables();
|
|
488
|
-
|
|
489
448
|
let uA = tabs.getUnitsByDimension(new Dimension(i));
|
|
490
449
|
if (uA == null) throw new Error(`Can't find base unit for dimension ${i}`);
|
|
491
450
|
this.name_ = uA.name + elem;
|
|
492
451
|
}
|
|
493
|
-
|
|
494
452
|
return num;
|
|
495
453
|
} // end mutateCoherent
|
|
496
454
|
|
|
@@ -508,24 +466,22 @@ class Unit {
|
|
|
508
466
|
* conversion is being made
|
|
509
467
|
* @return the equivalent amount in molUnit
|
|
510
468
|
*/
|
|
511
|
-
|
|
512
|
-
|
|
513
469
|
convertMassToMol(amt, molUnit, molecularWeight) {
|
|
514
470
|
// The prefix values that have been applied to this unit, which is the mass
|
|
515
471
|
// (grams) unit, are reflected in the magnitude. So the number of moles
|
|
516
472
|
// represented by this unit equals the number of grams -- amount * magnitude
|
|
517
473
|
// divided by the molecular Weight
|
|
518
|
-
let molAmt = this.magnitude_ * amt / molecularWeight;
|
|
474
|
+
let molAmt = this.magnitude_ * amt / molecularWeight;
|
|
475
|
+
// The molUnit's basic magnitude, before prefixes are applied,
|
|
519
476
|
// is avogadro's number, get that and divide it out of the current magnitude.
|
|
520
|
-
|
|
521
477
|
let tabs = this._getUnitTables();
|
|
522
|
-
|
|
523
478
|
let avoNum = tabs.getUnitByCode('mol').magnitude_;
|
|
524
|
-
let molesFactor = molUnit.magnitude_ / avoNum;
|
|
479
|
+
let molesFactor = molUnit.magnitude_ / avoNum;
|
|
480
|
+
// return the molAmt divided by the molesFactor as the number of moles
|
|
525
481
|
// for the molUnit
|
|
526
|
-
|
|
527
482
|
return molAmt / molesFactor;
|
|
528
483
|
}
|
|
484
|
+
|
|
529
485
|
/**
|
|
530
486
|
* Calculates the number of units that would result from converting a unit
|
|
531
487
|
* expressed in moles to a unit expressed in mass (grams). The "this" unit
|
|
@@ -540,29 +496,27 @@ class Unit {
|
|
|
540
496
|
* conversion is being made
|
|
541
497
|
* @return the equivalent amount in massUnit
|
|
542
498
|
*/
|
|
543
|
-
|
|
544
|
-
|
|
545
499
|
convertMolToMass(amt, massUnit, molecularWeight) {
|
|
546
500
|
// A simple mole unit has a magnitude of avogadro's number. Get that
|
|
547
501
|
// number now (since not everyone agrees on what it is, and what is
|
|
548
502
|
// being used in this system might change).
|
|
549
503
|
let tabs = this._getUnitTables();
|
|
550
|
-
|
|
551
|
-
|
|
504
|
+
let avoNum = tabs.getUnitByCode('mol').magnitude_;
|
|
505
|
+
// Determine what prefix values (mg or mg/dL, etc.) have been applied to
|
|
552
506
|
// this unit by dividing the simple mole unit magnitude out of the
|
|
553
507
|
// current mole unit magnitude.
|
|
554
|
-
|
|
555
|
-
|
|
508
|
+
let molesFactor = this.magnitude_ / avoNum;
|
|
509
|
+
// The number of grams (mass) is equal to the number of moles (amt)
|
|
556
510
|
// times the molecular weight. We also multiply that by the prefix values
|
|
557
511
|
// applied to the current unit (molesFactor) to get the grams for this
|
|
558
512
|
// particular unit.
|
|
559
|
-
|
|
560
|
-
|
|
513
|
+
let massAmt = molesFactor * amt * molecularWeight;
|
|
514
|
+
// Finally, we return the mass amount/grams for this particular unit
|
|
561
515
|
// divided by any effects of prefixes applied to the "to" unit, which
|
|
562
516
|
// is assumed to be some form of a gram unit
|
|
563
|
-
|
|
564
517
|
return massAmt / massUnit.magnitude_;
|
|
565
518
|
}
|
|
519
|
+
|
|
566
520
|
/**
|
|
567
521
|
* Mutates this unit into a unit on a ratio scale and converts a specified
|
|
568
522
|
* number of units to an appropriate value for this converted unit
|
|
@@ -571,8 +525,6 @@ class Unit {
|
|
|
571
525
|
* @return the magnitude of this unit after it's converted
|
|
572
526
|
* @throw an error if the dimensions differ
|
|
573
527
|
*/
|
|
574
|
-
|
|
575
|
-
|
|
576
528
|
mutateRatio(num) {
|
|
577
529
|
if (this.cnv_ == null) return this.mutateCoherent(num);else return num;
|
|
578
530
|
} // end mutateRatio
|
|
@@ -586,8 +538,6 @@ class Unit {
|
|
|
586
538
|
* @param s the value by which this unit is to be multiplied
|
|
587
539
|
* @return a copy this unit multiplied by s
|
|
588
540
|
* */
|
|
589
|
-
|
|
590
|
-
|
|
591
541
|
multiplyThis(s) {
|
|
592
542
|
let retUnit = this.clone();
|
|
593
543
|
if (retUnit.cnv_ != null) retUnit.cnvPfx_ *= s;else retUnit.magnitude_ *= s;
|
|
@@ -610,53 +560,54 @@ class Unit {
|
|
|
610
560
|
* @throws an error if one of the units is not on a ratio-scale
|
|
611
561
|
* and the other is not dimensionless.
|
|
612
562
|
*/
|
|
613
|
-
|
|
614
|
-
|
|
615
563
|
multiplyThese(unit2) {
|
|
616
564
|
var retUnit = this.clone();
|
|
617
|
-
|
|
618
565
|
if (retUnit.cnv_ != null) {
|
|
619
566
|
if (unit2.cnv_ == null && (!unit2.dim_ || unit2.dim_.isZero())) retUnit.cnvPfx_ *= unit2.magnitude_;else throw new Error(`Attempt to multiply non-ratio unit ${retUnit.name_} ` + 'failed.');
|
|
620
567
|
} // end if this unit has a conversion function
|
|
621
568
|
else if (unit2.cnv_ != null) {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
569
|
+
if (!retUnit.dim_ || retUnit.dim_.isZero()) {
|
|
570
|
+
retUnit.cnvPfx_ = unit2.cnvPfx_ * retUnit.magnitude_;
|
|
571
|
+
retUnit.magnitude_ = unit2.magnitude_;
|
|
572
|
+
retUnit.cnv_ = unit2.cnv_;
|
|
573
|
+
} else throw new Error(`Attempt to multiply non-ratio unit ${unit2.name_}`);
|
|
574
|
+
} // end if unit2 has a conversion function
|
|
575
|
+
|
|
576
|
+
// else neither unit has a conversion function
|
|
577
|
+
else {
|
|
578
|
+
retUnit.magnitude_ *= unit2.magnitude_;
|
|
579
|
+
} // end if unit2 does not have a conversion function
|
|
580
|
+
|
|
632
581
|
// If this.dim_ isn't there, clone the dimension in unit2 - if dimVec_
|
|
633
582
|
// is a dimension in unit2.dim_; else just transfer it to this dimension
|
|
634
|
-
|
|
635
|
-
|
|
636
583
|
if (!retUnit.dim_ || retUnit.dim_ && !retUnit.dim_.dimVec_) {
|
|
637
584
|
if (unit2.dim_) retUnit.dim_ = unit2.dim_.clone();else retUnit.dim_ = unit2.dim_;
|
|
638
|
-
}
|
|
585
|
+
}
|
|
586
|
+
// Else this.dim_ is there. If there is a dimension for unit2,
|
|
639
587
|
// add it to this one.
|
|
640
588
|
else if (unit2.dim_ && unit2.dim_ instanceof Dimension) {
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
// where the multiplication was performed (an error wasn't thrown)
|
|
644
|
-
|
|
589
|
+
retUnit.dim_.add(unit2.dim_);
|
|
590
|
+
}
|
|
645
591
|
|
|
592
|
+
// Concatenate the unit info (name, code, etc) for all cases
|
|
593
|
+
// where the multiplication was performed (an error wasn't thrown)
|
|
646
594
|
retUnit.name_ = this._concatStrs(retUnit.name_, '*', unit2.name_, '[', ']');
|
|
647
595
|
retUnit.csCode_ = this._concatStrs(retUnit.csCode_, '.', unit2.csCode_, '(', ')');
|
|
648
596
|
if (retUnit.ciCode_ && unit2.ciCode_) retUnit.ciCode_ = this._concatStrs(retUnit.ciCode_, '.', unit2.ciCode_, '(', ')');else if (unit2.ciCode_) retUnit.ciCode_ = unit2.ciCode_;
|
|
649
597
|
retUnit.resetFieldsForDerivedUnit();
|
|
650
|
-
if (retUnit.printSymbol_ && unit2.printSymbol_) retUnit.printSymbol_ = this._concatStrs(retUnit.printSymbol_, '.', unit2.printSymbol_, '(', ')');else if (unit2.printSymbol_) retUnit.printSymbol_ = unit2.printSymbol_;
|
|
598
|
+
if (retUnit.printSymbol_ && unit2.printSymbol_) retUnit.printSymbol_ = this._concatStrs(retUnit.printSymbol_, '.', unit2.printSymbol_, '(', ')');else if (unit2.printSymbol_) retUnit.printSymbol_ = unit2.printSymbol_;
|
|
599
|
+
|
|
600
|
+
// Update the mole exponent count by adding the count for unit2 to the
|
|
651
601
|
// count for this unit.
|
|
602
|
+
retUnit.moleExp_ = retUnit.moleExp_ + unit2.moleExp_;
|
|
652
603
|
|
|
653
|
-
|
|
604
|
+
// A unit that has the arbitrary attribute taints any unit created from it
|
|
654
605
|
// via an arithmetic operation. Taint accordingly
|
|
655
606
|
// if (!retUnit.isMole_)
|
|
656
607
|
// retUnit.isMole_ = unit2.isMole_ ;
|
|
608
|
+
if (!retUnit.isArbitrary_) retUnit.isArbitrary_ = unit2.isArbitrary_;
|
|
657
609
|
|
|
658
|
-
|
|
659
|
-
|
|
610
|
+
// Likewise for special units
|
|
660
611
|
if (!retUnit.isSpecial_) retUnit.isSpecial_ = unit2.isSpecial_;
|
|
661
612
|
return retUnit;
|
|
662
613
|
} // end multiplyThese
|
|
@@ -665,13 +616,12 @@ class Unit {
|
|
|
665
616
|
* Clears fields like isBase_, synonyms_, etc. when a unit has been cloned
|
|
666
617
|
* from a known unit but it being used to construct a derived unit.
|
|
667
618
|
*/
|
|
668
|
-
|
|
669
|
-
|
|
670
619
|
resetFieldsForDerivedUnit() {
|
|
671
620
|
this.guidance_ = '';
|
|
672
621
|
this.synonyms_ = null;
|
|
673
622
|
this.isBase_ = false;
|
|
674
623
|
}
|
|
624
|
+
|
|
675
625
|
/**
|
|
676
626
|
* Divides this unit by another unit. If this unit is not on a ratio
|
|
677
627
|
* scale an exception is raised. Mutating to a ratio scale unit
|
|
@@ -682,8 +632,6 @@ class Unit {
|
|
|
682
632
|
* @return this unit after it is divided by unit2
|
|
683
633
|
* @throws an error if either of the units is not on a ratio scale.
|
|
684
634
|
* */
|
|
685
|
-
|
|
686
|
-
|
|
687
635
|
divide(unit2) {
|
|
688
636
|
var retUnit = this.clone();
|
|
689
637
|
if (retUnit.cnv_ != null) throw new Error(`Attempt to divide non-ratio unit ${retUnit.name_}`);
|
|
@@ -693,29 +641,31 @@ class Unit {
|
|
|
693
641
|
if (retUnit.ciCode_ && unit2.ciCode_) retUnit.ciCode_ = this._concatStrs(retUnit.ciCode_, '/', unit2.ciCode_, '(', ')');else if (unit2.ciCode_) retUnit.ciCode_ = unit2.invertString(unit2.ciCode_);
|
|
694
642
|
retUnit.resetFieldsForDerivedUnit();
|
|
695
643
|
retUnit.magnitude_ /= unit2.magnitude_;
|
|
696
|
-
if (retUnit.printSymbol_ && unit2.printSymbol_) retUnit.printSymbol_ = this._concatStrs(retUnit.printSymbol_, '/', unit2.printSymbol_, '(', ')');else if (unit2.printSymbol_) retUnit.printSymbol_ = unit2.invertString(unit2.printSymbol_);
|
|
644
|
+
if (retUnit.printSymbol_ && unit2.printSymbol_) retUnit.printSymbol_ = this._concatStrs(retUnit.printSymbol_, '/', unit2.printSymbol_, '(', ')');else if (unit2.printSymbol_) retUnit.printSymbol_ = unit2.invertString(unit2.printSymbol_);
|
|
645
|
+
|
|
646
|
+
// Continue if unit2 has a dimension object.
|
|
697
647
|
// If this object has a dimension object, subtract unit2's dim_ object from
|
|
698
648
|
// this one. The sub method will take care of cases where the dimVec_ arrays
|
|
699
649
|
// are missing on one or both dim_ objects.
|
|
700
|
-
|
|
701
650
|
if (unit2.dim_) {
|
|
702
651
|
if (retUnit.dim_) {
|
|
703
652
|
if (retUnit.dim_.isNull()) retUnit.dim_.assignZero();
|
|
704
653
|
retUnit.dim_ = retUnit.dim_.sub(unit2.dim_);
|
|
705
654
|
} // end if this.dim_ exists
|
|
655
|
+
|
|
706
656
|
// Else if this dim_ object is missing, clone unit2's dim_ object
|
|
707
657
|
// and give the inverted clone to this unit.
|
|
708
658
|
else retUnit.dim_ = unit2.dim_.clone().minus();
|
|
709
659
|
} // end if unit2 has a dimension object
|
|
660
|
+
|
|
710
661
|
// Update the mole exponent count by subtracting the count for unit2 from
|
|
711
662
|
// the // count for this unit.
|
|
663
|
+
retUnit.moleExp_ = retUnit.moleExp_ - unit2.moleExp_;
|
|
712
664
|
|
|
713
|
-
|
|
714
|
-
retUnit.moleExp_ = retUnit.moleExp_ - unit2.moleExp_; // A unit that has the arbitrary attribute taints any unit created from
|
|
665
|
+
// A unit that has the arbitrary attribute taints any unit created from
|
|
715
666
|
// it via an arithmetic operation. Taint accordingly
|
|
716
667
|
// if (!retUnit.isMole_)
|
|
717
668
|
// retUnit.isMole_ = unit2.isMole_ ;
|
|
718
|
-
|
|
719
669
|
if (!retUnit.isArbitrary_) retUnit.isArbitrary_ = unit2.isArbitrary_;
|
|
720
670
|
return retUnit;
|
|
721
671
|
} // end divide
|
|
@@ -730,8 +680,6 @@ class Unit {
|
|
|
730
680
|
* @return this unit after being inverted
|
|
731
681
|
* @throws and error if this unit is not on a ratio scale
|
|
732
682
|
*/
|
|
733
|
-
|
|
734
|
-
|
|
735
683
|
invert() {
|
|
736
684
|
if (this.cnv_ != null) throw new Error(`Attempt to invert a non-ratio unit - ${this.name_}`);
|
|
737
685
|
this.name_ = this.invertString(this.name_);
|
|
@@ -748,26 +696,21 @@ class Unit {
|
|
|
748
696
|
* @param the string to be inverted
|
|
749
697
|
* @return the inverted string
|
|
750
698
|
*/
|
|
751
|
-
|
|
752
|
-
|
|
753
699
|
invertString(theString) {
|
|
754
700
|
if (theString.length > 0) {
|
|
755
|
-
|
|
756
|
-
|
|
701
|
+
// replace('<!', '</') is here to make sure closing html tags like </sup> are intact. See LF-2830.
|
|
702
|
+
let stringRep = theString.replace('/', "!").replace('.', '/').replace('<!', '</').replace("!", '.');
|
|
757
703
|
switch (stringRep.charAt(0)) {
|
|
758
704
|
case '.':
|
|
759
705
|
theString = stringRep.substr(1);
|
|
760
706
|
break;
|
|
761
|
-
|
|
762
707
|
case '/':
|
|
763
708
|
theString = stringRep;
|
|
764
709
|
break;
|
|
765
|
-
|
|
766
710
|
default:
|
|
767
711
|
theString = "/" + stringRep;
|
|
768
712
|
}
|
|
769
713
|
}
|
|
770
|
-
|
|
771
714
|
return theString;
|
|
772
715
|
} // end invertString
|
|
773
716
|
|
|
@@ -785,11 +728,10 @@ class Unit {
|
|
|
785
728
|
* a string
|
|
786
729
|
* @returns the built string
|
|
787
730
|
*/
|
|
788
|
-
|
|
789
|
-
|
|
790
731
|
_concatStrs(str1, operator, str2, startChar, endChar) {
|
|
791
732
|
return this._buildOneString(str1, startChar, endChar) + operator + this._buildOneString(str2, startChar, endChar);
|
|
792
733
|
}
|
|
734
|
+
|
|
793
735
|
/**
|
|
794
736
|
* This function handles creation of one string to be included in a
|
|
795
737
|
* concatenated string. Basically it checks to see if the string
|
|
@@ -804,11 +746,8 @@ class Unit {
|
|
|
804
746
|
* @param endChar ending enclosing character
|
|
805
747
|
* @returns the string
|
|
806
748
|
*/
|
|
807
|
-
|
|
808
|
-
|
|
809
749
|
_buildOneString(str, startChar, endChar) {
|
|
810
750
|
let ret = '';
|
|
811
|
-
|
|
812
751
|
if (intUtils_.isNumericString(str)) {
|
|
813
752
|
ret = str;
|
|
814
753
|
} else {
|
|
@@ -820,9 +759,9 @@ class Unit {
|
|
|
820
759
|
ret = str;
|
|
821
760
|
}
|
|
822
761
|
}
|
|
823
|
-
|
|
824
762
|
return ret;
|
|
825
763
|
}
|
|
764
|
+
|
|
826
765
|
/**
|
|
827
766
|
* Raises the unit to a power. For example
|
|
828
767
|
* kg.m/s2 raised to the -2 power would be kg-2.m-2/s-4
|
|
@@ -840,34 +779,28 @@ class Unit {
|
|
|
840
779
|
* @return this unit after it is raised
|
|
841
780
|
* @throws an error if this unit is not on a ratio scale.
|
|
842
781
|
*/
|
|
843
|
-
|
|
844
|
-
|
|
845
782
|
power(p) {
|
|
846
|
-
if (this.cnv_ != null) throw new Error(`Attempt to raise a non-ratio unit, ${this.name_}, ` + 'to a power.');
|
|
783
|
+
if (this.cnv_ != null) throw new Error(`Attempt to raise a non-ratio unit, ${this.name_}, ` + 'to a power.');
|
|
784
|
+
|
|
785
|
+
//this.name_ = UnitString.pow(this.name_, p);
|
|
847
786
|
// the above line is replaced with the code below, as the pow method
|
|
848
787
|
// never actually existing in the UnitString class. (Tried to use
|
|
849
788
|
// Schadow java code but this way ended up being a lot easier).
|
|
850
|
-
|
|
851
789
|
let uStr = this.csCode_;
|
|
852
790
|
let uArray = uStr.match(/([./]|[^./]+)/g);
|
|
853
791
|
let arLen = uArray.length;
|
|
854
|
-
|
|
855
792
|
for (let i = 0; i < arLen; i++) {
|
|
856
793
|
let un = uArray[i];
|
|
857
|
-
|
|
858
794
|
if (un !== '/' && un !== '.') {
|
|
859
795
|
let nun = parseInt(un);
|
|
860
796
|
if (isInteger(nun)) uArray[i] = Math.pow(nun, p).toString();else {
|
|
861
797
|
let uLen = un.length;
|
|
862
|
-
|
|
863
798
|
for (let u = uLen - 1; u >= 0; u--) {
|
|
864
799
|
let uChar = parseInt(un[u]);
|
|
865
|
-
|
|
866
800
|
if (!isInteger(uChar)) {
|
|
867
801
|
if (un[u] === '-' || un[u] === '+') {
|
|
868
802
|
u--;
|
|
869
803
|
}
|
|
870
|
-
|
|
871
804
|
if (u < uLen - 1) {
|
|
872
805
|
let exp = parseInt(un.substr(u));
|
|
873
806
|
exp = Math.pow(exp, p);
|
|
@@ -877,27 +810,19 @@ class Unit {
|
|
|
877
810
|
uArray[i] += p.toString();
|
|
878
811
|
u = -1;
|
|
879
812
|
} // end if there are/aren't some numbers at the end
|
|
880
|
-
|
|
881
|
-
|
|
882
813
|
u = -1;
|
|
883
814
|
} // end if this character is not a number
|
|
884
|
-
|
|
885
815
|
} // end searching backwards for start of exponent
|
|
886
|
-
|
|
887
816
|
} // end if this element is not a number
|
|
888
817
|
} // end if the current element is not an operator
|
|
889
|
-
|
|
890
818
|
} // end do for each element of the units array
|
|
891
|
-
// reassemble the updated units array to a string
|
|
892
|
-
|
|
893
819
|
|
|
820
|
+
// reassemble the updated units array to a string
|
|
894
821
|
this.csCode_ = uArray.join('');
|
|
895
822
|
this.magnitude_ = Math.pow(this.magnitude_, p);
|
|
896
|
-
|
|
897
823
|
if (this.dim_) {
|
|
898
824
|
this.dim_.mul(p);
|
|
899
825
|
}
|
|
900
|
-
|
|
901
826
|
return this;
|
|
902
827
|
} // end power
|
|
903
828
|
|
|
@@ -917,14 +842,10 @@ class Unit {
|
|
|
917
842
|
* @param unit2 the unit to be compared to this one
|
|
918
843
|
* @returns boolean indicating commensurability
|
|
919
844
|
*/
|
|
920
|
-
|
|
921
|
-
|
|
922
845
|
isMoleMassCommensurable(unit2) {
|
|
923
846
|
let tabs = this._getUnitTables();
|
|
924
|
-
|
|
925
847
|
let d = tabs.getMassDimensionIndex();
|
|
926
848
|
let commensurable = false;
|
|
927
|
-
|
|
928
849
|
if (this.moleExp_ === 1 && unit2.moleExp_ === 0) {
|
|
929
850
|
let testDim = this.dim_.clone();
|
|
930
851
|
let curVal = testDim.getElementAt(d);
|
|
@@ -936,9 +857,9 @@ class Unit {
|
|
|
936
857
|
testDim.setElementAt(d, curVal + unit2.moleExp_);
|
|
937
858
|
commensurable = testDim.equals(this.dim_);
|
|
938
859
|
}
|
|
939
|
-
|
|
940
860
|
return commensurable;
|
|
941
861
|
}
|
|
862
|
+
|
|
942
863
|
/**
|
|
943
864
|
* This returns the UnitTables singleton object. Including the require
|
|
944
865
|
* statement included here causes a circular dependency condition that
|
|
@@ -947,15 +868,10 @@ class Unit {
|
|
|
947
868
|
*
|
|
948
869
|
* @private
|
|
949
870
|
*/
|
|
950
|
-
|
|
951
|
-
|
|
952
871
|
_getUnitTables() {
|
|
953
872
|
if (!UnitTables) UnitTables = require('./unitTables.js').UnitTables;
|
|
954
873
|
return UnitTables.getInstance();
|
|
955
874
|
}
|
|
956
|
-
|
|
957
875
|
} // end Unit class
|
|
958
|
-
|
|
959
|
-
|
|
960
876
|
exports.Unit = Unit;
|
|
961
877
|
//# sourceMappingURL=unit.js.map
|