@lhncbc/ucum-lhc 6.0.2 → 7.1.0
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 +61 -49
- package/browser-dist/ucum-lhc.min.js +2 -0
- package/browser-dist/ucum-lhc.min.js.map +1 -0
- package/package.json +2 -5
- package/source/ucumLhcUtils.js +90 -59
- package/source/unit.js +215 -162
- package/source/unitString.js +2 -0
- package/source-cjs/ucumLhcUtils.js +79 -46
- package/source-cjs/ucumLhcUtils.js.map +1 -1
- package/source-cjs/unit.js +204 -160
- package/source-cjs/unit.js.map +1 -1
- package/source-cjs/unitString.js +2 -0
- package/source-cjs/unitString.js.map +1 -1
- package/browser-dist/ucum-lhc.js +0 -5234
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lhncbc/ucum-lhc",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.1.0",
|
|
4
4
|
"description": "Implements Unified Code for Units of Measure (UCUM) functions in a javascript library",
|
|
5
5
|
"main": "source-cjs/ucumPkg.js",
|
|
6
6
|
"homepage": "https://lhncbc.github.io/ucum-lhc/",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"test": "test"
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
|
-
"browser-dist/ucum-lhc.
|
|
11
|
+
"browser-dist/ucum-lhc.min.*",
|
|
12
12
|
"source-cjs/*",
|
|
13
13
|
"source/*",
|
|
14
14
|
"data/ucumDefs.min.json"
|
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
"escape-html": "^1.0.3",
|
|
21
21
|
"is-integer": "^1.0.6",
|
|
22
22
|
"jsonfile": "^2.2.3",
|
|
23
|
-
"stream": "0.0.2",
|
|
24
23
|
"stream-transform": "^0.1.1",
|
|
25
24
|
"string-to-stream": "^1.1.0",
|
|
26
25
|
"xmldoc": "^0.4.0"
|
|
@@ -34,14 +33,12 @@
|
|
|
34
33
|
"@babel/register": "^7.6.2",
|
|
35
34
|
"babel-loader": "^8.0.6",
|
|
36
35
|
"babelify": "^10.0.0",
|
|
37
|
-
"bootstrap": "^4.3.1",
|
|
38
36
|
"bower": "^1.8.8",
|
|
39
37
|
"grunt": "^1.5.3",
|
|
40
38
|
"grunt-babel": "^8.0.0",
|
|
41
39
|
"grunt-browserify": "^6.0.0",
|
|
42
40
|
"grunt-cli": "^1.4.3",
|
|
43
41
|
"grunt-contrib-clean": "^1.0.0",
|
|
44
|
-
"grunt-contrib-connect": "^2.1.0",
|
|
45
42
|
"grunt-contrib-cssmin": "^3.0.0",
|
|
46
43
|
"grunt-contrib-uglify": "^5.2.2",
|
|
47
44
|
"grunt-extract-sourcemap": "^0.1.19",
|
package/source/ucumLhcUtils.js
CHANGED
|
@@ -23,16 +23,18 @@ export class UcumLhcUtils {
|
|
|
23
23
|
*/
|
|
24
24
|
constructor() {
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
if (UnitTables.getInstance().unitsCount() === 0) {
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
// Load the prefix and unit objects
|
|
29
|
+
ucumJsonDefs.loadJsonDefs();
|
|
30
|
+
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
// Get the UnitString parser that will be used with this instance
|
|
33
|
+
// of the LHC Utilities
|
|
34
|
+
this.uStrParser_ = UnitString.getInstance();
|
|
35
35
|
|
|
36
|
+
// Get a copy of the mass dimension index.
|
|
37
|
+
this.massDimIndex_ = UnitTables.getInstance().getMassDimensionIndex();
|
|
36
38
|
} // end constructor
|
|
37
39
|
|
|
38
40
|
|
|
@@ -145,20 +147,19 @@ export class UcumLhcUtils {
|
|
|
145
147
|
} // end validateUnitString
|
|
146
148
|
|
|
147
149
|
|
|
148
|
-
|
|
150
|
+
// Note that below when the value of ConversionType is mol|mass, it refers to
|
|
151
|
+
// either a conversion from mol to mass or from mass to mol.
|
|
149
152
|
/**
|
|
150
153
|
* @typedef {
|
|
151
|
-
* 'normal',
|
|
152
|
-
* 'mol
|
|
153
|
-
* 'mass
|
|
154
|
-
* 'eq
|
|
155
|
-
* 'mass
|
|
156
|
-
* 'eq->mol',
|
|
157
|
-
* 'mol->eq',
|
|
154
|
+
* 'normal',
|
|
155
|
+
* 'mol|mass',
|
|
156
|
+
* 'eq|mass',
|
|
157
|
+
* 'eq|mol',
|
|
158
|
+
* 'eq|mol|mass'
|
|
158
159
|
* } ConversionType
|
|
159
160
|
*/
|
|
160
161
|
|
|
161
|
-
|
|
162
|
+
/**
|
|
162
163
|
* Detects the type of conversion between two units.
|
|
163
164
|
*
|
|
164
165
|
* @param {Object} fromUnit - The unit to convert from.
|
|
@@ -167,33 +168,46 @@ export class UcumLhcUtils {
|
|
|
167
168
|
*/
|
|
168
169
|
detectConversionType(fromUnit, toUnit) {
|
|
169
170
|
/** @type {ConversionType} */
|
|
170
|
-
let conversionType
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
!(fromUnit.isEquivalentUnit() || toUnit.isEquivalentUnit()))) {
|
|
171
|
+
let conversionType;
|
|
172
|
+
if (fromUnit.moleExp_ == toUnit.moleExp_ &&
|
|
173
|
+
fromUnit.equivalentExp_ == toUnit.equivalentExp_) {
|
|
174
|
+
// Since the powers of the equivalents and mole in the units are the same in both
|
|
175
|
+
// units, no conversion is going to happen between mass, mol, and eq.
|
|
176
|
+
// There is the possibility that someone is trying to convert 'g' to 'g2',
|
|
177
|
+
// but we will handle that as the "normal" case (and later find it invalid).
|
|
178
178
|
conversionType = 'normal';
|
|
179
179
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
conversionType =
|
|
180
|
+
else if (fromUnit.equivalentExp_ == toUnit.equivalentExp_) {
|
|
181
|
+
// In this case, the units have the same power of equivalents, so (because
|
|
182
|
+
// it is not the first case) the units have different powers of mol and mass,
|
|
183
|
+
// so this must be a conversion between mol and mass.
|
|
184
|
+
conversionType = 'mol|mass';
|
|
185
185
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
conversionType = 'mass
|
|
186
|
+
else if (fromUnit.moleExp_ == toUnit.moleExp_) {
|
|
187
|
+
// In this case, the units have the same power of mol, so (because
|
|
188
|
+
// it is not the first case) the units have different powers of equivalents and mass,
|
|
189
|
+
// so this must be a conversion between equivalents and mass.
|
|
190
|
+
conversionType = 'eq|mass';
|
|
191
|
+
}
|
|
192
|
+
else if (fromUnit.dim_.getElementAt(this.massDimIndex_) ==
|
|
193
|
+
toUnit.dim_.getElementAt(this.massDimIndex_)) {
|
|
194
|
+
// In this case, the units have the same power of mass, so (because
|
|
195
|
+
// it is not the first case) the units have different powers of equivalents and mol,
|
|
196
|
+
// so this must be a conversion between equivalents and moles.
|
|
197
|
+
conversionType = 'eq|mol';
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
// In this case, the units have different powers of mass, mol, and
|
|
201
|
+
// equivalents, so there is a conversion between mass, mol, and
|
|
202
|
+
// equivalents.
|
|
203
|
+
conversionType = 'eq|mol|mass';
|
|
191
204
|
}
|
|
192
205
|
|
|
193
206
|
return conversionType;
|
|
194
207
|
} // end detectConversionType
|
|
195
208
|
|
|
196
|
-
|
|
209
|
+
|
|
210
|
+
/**
|
|
197
211
|
* @typedef {{
|
|
198
212
|
* status: 'succeeded' | 'failed' | 'error',
|
|
199
213
|
* toVal: number | null,
|
|
@@ -215,7 +229,7 @@ export class UcumLhcUtils {
|
|
|
215
229
|
* }} ConvertUnitResult
|
|
216
230
|
*/
|
|
217
231
|
|
|
218
|
-
|
|
232
|
+
|
|
219
233
|
/**
|
|
220
234
|
* This method converts one unit to another
|
|
221
235
|
*
|
|
@@ -232,7 +246,7 @@ export class UcumLhcUtils {
|
|
|
232
246
|
* - molecularWeight: the molecular weight of the substance in question when a conversion is being requested from mass to moles and vice versa.
|
|
233
247
|
* This is required when one of the units represents a value in moles. It is ignored if neither unit includes a measurement in moles.
|
|
234
248
|
* - charge: the absolute value of the charge of the substance in question when a conversion is being requested from mass/moles to
|
|
235
|
-
* equivalents and vice versa. It is required when one of the units represents a value in equivalents and the other in mass or moles.
|
|
249
|
+
* equivalents and vice versa. It is required when one of the units represents a value in equivalents and the other in mass or moles.
|
|
236
250
|
* It is ignored if neither unit includes an equivalent unit.
|
|
237
251
|
* @returns {ConvertUnitResult}
|
|
238
252
|
* - a hash with six elements:
|
|
@@ -335,27 +349,23 @@ export class UcumLhcUtils {
|
|
|
335
349
|
|
|
336
350
|
if (fromUnit && toUnit) {
|
|
337
351
|
try {
|
|
338
|
-
const convertType = this.detectConversionType(fromUnit, toUnit);
|
|
352
|
+
const convertType = this.detectConversionType(fromUnit, toUnit);
|
|
339
353
|
|
|
340
354
|
switch (convertType) {
|
|
341
355
|
case 'normal':
|
|
342
356
|
returnObj['toVal'] = toUnit.convertFrom(fromVal, fromUnit);
|
|
343
357
|
break;
|
|
344
|
-
case 'mol
|
|
345
|
-
case 'mass->mol':
|
|
358
|
+
case 'mol|mass':
|
|
346
359
|
if (!molecularWeight) {
|
|
347
360
|
throw new Error(Ucum.needMoleWeightMsg_);
|
|
348
361
|
}
|
|
349
|
-
if (!fromUnit.
|
|
362
|
+
if (!fromUnit.isMolMassCommensurable(toUnit)) {
|
|
350
363
|
throw new Error(`Sorry. ${fromUnitCode} cannot be ` +
|
|
351
364
|
`converted to ${toUnitCode}.`);
|
|
352
365
|
}
|
|
353
|
-
returnObj['toVal'] =
|
|
354
|
-
fromUnit.convertMolToMass(fromVal, toUnit, molecularWeight) :
|
|
355
|
-
fromUnit.convertMassToMol(fromVal, toUnit, molecularWeight);
|
|
366
|
+
returnObj['toVal'] = fromUnit.convertMolMass(fromVal, toUnit, molecularWeight);
|
|
356
367
|
break;
|
|
357
|
-
case 'eq
|
|
358
|
-
case 'mass->eq':
|
|
368
|
+
case 'eq|mass':
|
|
359
369
|
if (!molecularWeight) {
|
|
360
370
|
throw new Error(Ucum.needEqWeightMsg_);
|
|
361
371
|
}
|
|
@@ -366,18 +376,30 @@ export class UcumLhcUtils {
|
|
|
366
376
|
throw new Error(`Sorry. ${fromUnitCode} cannot be ` +
|
|
367
377
|
`converted to ${toUnitCode}.`);
|
|
368
378
|
}
|
|
369
|
-
returnObj['toVal'] =
|
|
370
|
-
fromUnit.convertEqToMass(fromVal, toUnit, molecularWeight, charge) :
|
|
371
|
-
fromUnit.convertMassToEq(fromVal, toUnit, molecularWeight, charge);
|
|
379
|
+
returnObj['toVal'] = fromUnit.convertEqMass(fromVal, toUnit, molecularWeight, charge);
|
|
372
380
|
break;
|
|
373
|
-
case 'eq
|
|
374
|
-
|
|
381
|
+
case 'eq|mol':
|
|
382
|
+
if (!fromUnit.isEqMolCommensurable(toUnit)) {
|
|
383
|
+
throw new Error(`Sorry. ${fromUnitCode} cannot be ` +
|
|
384
|
+
`converted to ${toUnitCode}.`);
|
|
385
|
+
}
|
|
375
386
|
if (!charge) {
|
|
376
387
|
throw new Error(Ucum.needEqChargeMsg_);
|
|
377
388
|
}
|
|
378
|
-
returnObj['toVal'] =
|
|
379
|
-
|
|
380
|
-
|
|
389
|
+
returnObj['toVal'] = fromUnit.convertEqMol(fromVal, toUnit, charge);
|
|
390
|
+
break;
|
|
391
|
+
case 'eq|mol|mass':
|
|
392
|
+
if (!molecularWeight) {
|
|
393
|
+
throw new Error(Ucum.needEqWeightMsg_);
|
|
394
|
+
}
|
|
395
|
+
if (!charge) {
|
|
396
|
+
throw new Error(Ucum.needEqChargeMsg_);
|
|
397
|
+
}
|
|
398
|
+
if (!fromUnit.isEqMolMassCommensurable(toUnit)) {
|
|
399
|
+
throw new Error(`Sorry. ${fromUnitCode} cannot be ` +
|
|
400
|
+
`converted to ${toUnitCode}.`);
|
|
401
|
+
}
|
|
402
|
+
returnObj['toVal'] = fromUnit.convertEqMolMass(fromVal, toUnit, molecularWeight, charge);
|
|
381
403
|
break;
|
|
382
404
|
default:
|
|
383
405
|
throw new Error("Unknown conversion type. No conversion was attempted.");
|
|
@@ -393,7 +415,7 @@ export class UcumLhcUtils {
|
|
|
393
415
|
returnObj['msg'].push(err.message);
|
|
394
416
|
}
|
|
395
417
|
} // end if we have the from and to units
|
|
396
|
-
}
|
|
418
|
+
}
|
|
397
419
|
catch (err) {
|
|
398
420
|
if (err.message == Ucum.needMoleWeightMsg_)
|
|
399
421
|
returnObj['status'] = 'failed';
|
|
@@ -640,14 +662,18 @@ export class UcumLhcUtils {
|
|
|
640
662
|
/**
|
|
641
663
|
* This method retrieves a list of units commensurable, i.e., that can be
|
|
642
664
|
* converted from and to, a specified unit. Returns an error if the "from"
|
|
643
|
-
* unit cannot be found.
|
|
665
|
+
* unit cannot be found. If necessary, you can filter the list of units by
|
|
666
|
+
* specifying a list of unit categories that should be in the resulting list.
|
|
644
667
|
*
|
|
645
|
-
* @param fromName the name/unit string of the "from" unit
|
|
668
|
+
* @param {string} fromName - the name/unit string of the "from" unit
|
|
669
|
+
* @param {string[] | null} [categoryList] - the list of unit categories;
|
|
670
|
+
* this parameter is optional, defaults to null if not specified;
|
|
671
|
+
* possible list values: 'Clinical', 'Nonclinical', 'Obsolete', 'Constant'
|
|
646
672
|
* @returns an array containing two elements;
|
|
647
|
-
* first element is the list of commensurable units if any were found
|
|
673
|
+
* first element is the list of commensurable units if any were found;
|
|
648
674
|
* second element is an error message if the "from" unit is not found
|
|
649
675
|
*/
|
|
650
|
-
commensurablesList(fromName) {
|
|
676
|
+
commensurablesList(fromName, categoryList = null) {
|
|
651
677
|
|
|
652
678
|
let retMsg = [];
|
|
653
679
|
let commUnits = null ;
|
|
@@ -677,6 +703,11 @@ export class UcumLhcUtils {
|
|
|
677
703
|
if (dimVec) {
|
|
678
704
|
let utab = UnitTables.getInstance();
|
|
679
705
|
commUnits = utab.getUnitsByDimension(dimVec);
|
|
706
|
+
if (categoryList) {
|
|
707
|
+
commUnits = commUnits.filter((item) => {
|
|
708
|
+
return categoryList.indexOf(item.category_) !== -1;
|
|
709
|
+
});
|
|
710
|
+
}
|
|
680
711
|
}
|
|
681
712
|
} // end if the from unit has a dimension vector
|
|
682
713
|
} // end if we found a "from" unit
|