@lhncbc/ucum-lhc 6.0.1 → 7.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lhncbc/ucum-lhc",
3
- "version": "6.0.1",
3
+ "version": "7.0.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.js",
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",
@@ -23,16 +23,18 @@ export class UcumLhcUtils {
23
23
  */
24
24
  constructor() {
25
25
 
26
- if (UnitTables.getInstance().unitsCount() === 0) {
26
+ if (UnitTables.getInstance().unitsCount() === 0) {
27
27
 
28
- // Load the prefix and unit objects
29
- ucumJsonDefs.loadJsonDefs();
30
- }
28
+ // Load the prefix and unit objects
29
+ ucumJsonDefs.loadJsonDefs();
30
+ }
31
31
 
32
- // Get the UnitString parser that will be used with this instance
33
- // of the LHC Utilities
34
- this.uStrParser_ = UnitString.getInstance();
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->mass',
153
- * 'mass->mol',
154
- * 'eq->mass',
155
- * 'mass->eq',
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 = 'normal';
171
-
172
- // detect mol <-> mass conversions and eq <-> eq conversions,
173
- // and non-mol <-> eq <-> mass conversions
174
- if ((fromUnit.isMolarUnit() && toUnit.isMolarUnit()) ||
175
- (fromUnit.isEquivalentUnit() && toUnit.isEquivalentUnit()) ||
176
- (!(fromUnit.isMolarUnit() || toUnit.isMolarUnit()) &&
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
- // handle eq <-> mol/mass conversions
181
- else if (fromUnit.isEquivalentUnit()) {
182
- conversionType = toUnit.isMolarUnit() ? 'eq->mol' : 'eq->mass';
183
- } else if (toUnit.isEquivalentUnit()) {
184
- conversionType = fromUnit.isMolarUnit() ? 'mol->eq' : 'mass->eq';
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
+ }
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';
185
198
  }
186
- // handle mol <-> mass conversions
187
- else if (fromUnit.isMolarUnit()) {
188
- conversionType = 'mol->mass';
189
- } else if (toUnit.isMolarUnit()) {
190
- conversionType = 'mass->mol';
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->mass':
345
- case 'mass->mol':
358
+ case 'mol|mass':
346
359
  if (!molecularWeight) {
347
360
  throw new Error(Ucum.needMoleWeightMsg_);
348
361
  }
349
- if (!fromUnit.isMoleMassCommensurable(toUnit)) {
362
+ if (!fromUnit.isMolMassCommensurable(toUnit)) {
350
363
  throw new Error(`Sorry. ${fromUnitCode} cannot be ` +
351
364
  `converted to ${toUnitCode}.`);
352
365
  }
353
- returnObj['toVal'] = convertType === "mol->mass" ?
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->mass':
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'] = convertType === "eq->mass" ?
370
- fromUnit.convertEqToMass(fromVal, toUnit, molecularWeight, charge) :
371
- fromUnit.convertMassToEq(fromVal, toUnit, molecularWeight, charge);
379
+ returnObj['toVal'] = fromUnit.convertEqMass(fromVal, toUnit, molecularWeight, charge);
380
+ break;
381
+ case 'eq|mol':
382
+ if (!fromUnit.isEqMolCommensurable(toUnit)) {
383
+ throw new Error(`Sorry. ${fromUnitCode} cannot be ` +
384
+ `converted to ${toUnitCode}.`);
385
+ }
386
+ if (!charge) {
387
+ throw new Error(Ucum.needEqChargeMsg_);
388
+ }
389
+ returnObj['toVal'] = fromUnit.convertEqMol(fromVal, toUnit, charge);
372
390
  break;
373
- case 'eq->mol':
374
- case 'mol->eq':
391
+ case 'eq|mol|mass':
392
+ if (!molecularWeight) {
393
+ throw new Error(Ucum.needEqWeightMsg_);
394
+ }
375
395
  if (!charge) {
376
396
  throw new Error(Ucum.needEqChargeMsg_);
377
397
  }
378
- returnObj['toVal'] = convertType === "eq->mol" ?
379
- fromUnit.convertEqToMol(fromVal, toUnit, charge) :
380
- fromUnit.convertMolToEq(fromVal, toUnit, charge);
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';