@lhncbc/ucum-lhc 4.1.8 → 5.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/source/unit.js CHANGED
@@ -340,6 +340,7 @@ export class Unit {
340
340
  return match ;
341
341
  }// end of fullEquals
342
342
 
343
+
343
344
  /**
344
345
  * This returns the value of the property named by the parameter
345
346
  * passed in.
@@ -380,9 +381,9 @@ export class Unit {
380
381
  let newNum = 0.0 ;
381
382
 
382
383
  if (this.isArbitrary_)
383
- throw (new Error(`Attempt to convert arbitrary unit ${this.name_}`));
384
+ throw (new Error(`Attempt to convert to arbitrary unit "${this.csCode_}"`));
384
385
  if (fromUnit.isArbitrary_)
385
- throw (new Error(`Attempt to convert to arbitrary unit ${fromUnit.name_}`));
386
+ throw (new Error(`Attempt to convert arbitrary unit "${fromUnit.csCode_}"`));
386
387
 
387
388
  // reject request if both units have dimensions that are not equal
388
389
  if (fromUnit.dim_ && this.dim_ && !(fromUnit.dim_.equals(this.dim_))) {
@@ -410,39 +411,27 @@ export class Unit {
410
411
  let fromCnv = fromUnit.cnv_ ;
411
412
  let fromMag = fromUnit.magnitude_ ;
412
413
 
413
- // If the same conversion function is specified for both units, which
414
- // includes neither unit having a conversion function, multiply the
415
- // "from" unit's magnitude by the number passed in and then divide
416
- // that result by this unit's magnitude. Do this for units with
417
- // and without dimension vectors. PROBLEM with 2 non-commensurable
418
- // units with no dimension vector or function, e.g., byte to mol
419
- if (fromCnv === this.cnv_) {
420
- newNum = (num * fromMag) / this.magnitude_;
414
+ let x;
415
+ if (fromCnv != null) {
416
+ // turn num * fromUnit.magnitude into its ratio scale equivalent,
417
+ // e.g., convert Celsius to Kelvin
418
+ let fromFunc = funcs.forName(fromCnv);
419
+ x = fromFunc.cnvFrom(num * fromUnit.cnvPfx_) * fromMag;
420
+ //x = fromFunc.cnvFrom(num * fromMag) * fromUnit.cnvPfx_;
421
421
  }
422
- // else use a function to get the number to be returned
423
422
  else {
424
- let x = 0.0 ;
425
- if (fromCnv != null) {
426
- // turn num * fromUnit.magnitude into its ratio scale equivalent,
427
- // e.g., convert Celsius to Kelvin
428
- let fromFunc = funcs.forName(fromCnv);
429
- x = fromFunc.cnvFrom(num * fromUnit.cnvPfx_) * fromMag;
430
- //x = fromFunc.cnvFrom(num * fromMag) * fromUnit.cnvPfx_;
431
- }
432
- else {
433
- x = num * fromMag;
434
- }
423
+ x = num * fromMag;
424
+ }
435
425
 
436
- if (this.cnv_ != null) {
437
- // turn mag * origUnit on ratio scale into a non-ratio unit,
438
- // e.g. convert Kelvin to Fahrenheit
439
- let toFunc = funcs.forName(this.cnv_);
440
- newNum = toFunc.cnvTo(x / this.magnitude_) / this.cnvPfx_;
441
- }
442
- else {
443
- newNum = x / this.magnitude_;
444
- }
445
- } // end if either unit has a conversion function
426
+ if (this.cnv_ != null) {
427
+ // turn mag * origUnit on ratio scale into a non-ratio unit,
428
+ // e.g. convert Kelvin to Fahrenheit
429
+ let toFunc = funcs.forName(this.cnv_);
430
+ newNum = toFunc.cnvTo(x / this.magnitude_) / this.cnvPfx_;
431
+ }
432
+ else {
433
+ newNum = x / this.magnitude_;
434
+ }
446
435
 
447
436
  return newNum;
448
437
 
@@ -666,6 +655,7 @@ export class Unit {
666
655
  else if (unit2.cnv_ != null) {
667
656
  if (!retUnit.dim_ || retUnit.dim_.isZero()) {
668
657
  retUnit.cnvPfx_ = unit2.cnvPfx_ * retUnit.magnitude_;
658
+ retUnit.magnitude_ = unit2.magnitude_;
669
659
  retUnit.cnv_ = unit2.cnv_ ;
670
660
  }
671
661
  else
@@ -716,8 +706,12 @@ export class Unit {
716
706
  // via an arithmetic operation. Taint accordingly
717
707
  // if (!retUnit.isMole_)
718
708
  // retUnit.isMole_ = unit2.isMole_ ;
719
- if (!retUnit.isArbitrary_)
720
- retUnit.isArbitrary_ = unit2.isArbitrary_;
709
+ if (!retUnit.isArbitrary_)
710
+ retUnit.isArbitrary_ = unit2.isArbitrary_;
711
+
712
+ // Likewise for special units
713
+ if (!retUnit.isSpecial_)
714
+ retUnit.isSpecial_ = unit2.isSpecial_;
721
715
 
722
716
  return retUnit ;
723
717
 
@@ -218,6 +218,7 @@ export class UnitString {
218
218
  if (intUtils_.isIntegerUnit(finalUnit) || typeof finalUnit === 'number') {
219
219
  finalUnit = new Unit({
220
220
  'csCode_': origString,
221
+ 'ciCode_': origString,
221
222
  'magnitude_': finalUnit,
222
223
  'name_': origString
223
224
  });
@@ -1329,24 +1330,31 @@ export class UnitString {
1329
1330
  let tryBrackets = '[' + annoText.substring(1, annoText.length - 1) + ']';
1330
1331
  let mkUnitRet = this._makeUnit(tryBrackets, origString);
1331
1332
 
1332
- // If we got back a unit, assign it to the returned unit, and add
1333
- // a message to advise the user that brackets should enclose the code
1333
+ // Nearly anything inside braces is valid, so we don't want to change the
1334
+ // unit, but we can put the found unit in the message as a sort of
1335
+ // warning.
1334
1336
  if (mkUnitRet[0]) {
1335
- retUnit = mkUnitRet[0];
1336
- origString = origString.replace(annoText, tryBrackets);
1337
- this.retMsg_.push(`${annoText} is not a valid unit expression, but ` +
1338
- `${tryBrackets} is.\n` + this.vcMsgStart_ +
1339
- `${tryBrackets} (${retUnit.name_})${this.vcMsgEnd_}`);
1337
+ retUnit = uCode;
1338
+ this.retMsg_.push(`${annoText} is a valid unit expression, but ` +
1339
+ `did you mean ${tryBrackets} (${mkUnitRet[0].name_})?`);
1340
1340
  }
1341
- // Otherwise assume that this should be interpreted as a 1
1342
1341
  else {
1343
1342
  // remove error message generated for trybrackets
1344
1343
  if (this.retMsg_.length > msgLen) {
1345
1344
  this.retMsg_.pop();
1346
1345
  }
1347
- uCode = 1;
1348
- retUnit = 1;
1349
1346
  }
1347
+
1348
+ // This is the case where the string is only this annotation.
1349
+ // Create and return a unit object, as we do for numeric units in
1350
+ // parseString.
1351
+ retUnit = new Unit({
1352
+ 'csCode_': annoText,
1353
+ 'ciCode_': annoText,
1354
+ 'magnitude_': 1,
1355
+ 'name_': annoText
1356
+ });
1357
+
1350
1358
  } // end if it's only an annotation
1351
1359
 
1352
1360
  else {
@@ -1442,7 +1450,7 @@ export class UnitString {
1442
1450
 
1443
1451
  let finalUnit = uArray[0]['un'];
1444
1452
  if (intUtils_.isIntegerUnit(finalUnit)) {
1445
- finalUnit = new Unit({'csCode_' : finalUnit,
1453
+ finalUnit = new Unit({'csCode_' : finalUnit, 'ciCode_' : finalUnit,
1446
1454
  'magnitude_' : Number(finalUnit),
1447
1455
  'name_' : finalUnit}) ;
1448
1456
  }
@@ -1453,7 +1461,7 @@ export class UnitString {
1453
1461
  for (let u2 = 1; (u2 < uLen) && !endProcessing; u2++) {
1454
1462
  let nextUnit = uArray[u2]['un'];
1455
1463
  if (intUtils_.isIntegerUnit(nextUnit)) {
1456
- nextUnit = new Unit({'csCode_' : nextUnit ,
1464
+ nextUnit = new Unit({'csCode_' : nextUnit, 'ciCode_' : nextUnit,
1457
1465
  'magnitude_' : Number(nextUnit),
1458
1466
  'name_': nextUnit});
1459
1467
  }
@@ -97,6 +97,11 @@ class UnitTablesFactory {
97
97
  * @type integer
98
98
  */
99
99
  this.massDimIndex_ = 0;
100
+
101
+ /**
102
+ * Map of indices in the dimension vector to base unit symbols.
103
+ */
104
+ this.dimVecIndexToBaseUnit_ = {};
100
105
  }
101
106
 
102
107
 
@@ -138,6 +143,15 @@ class UnitTablesFactory {
138
143
  // and that's OK here.
139
144
  }
140
145
 
146
+ if (theUnit.isBase_) {
147
+ const dimVec = theUnit.dim_.dimVec_;
148
+ let nonZeroIndex;
149
+ for (let i=0, len=dimVec.length; nonZeroIndex==undefined && i<len; ++i) {
150
+ if (dimVec[i] != 0)
151
+ nonZeroIndex = i;
152
+ }
153
+ this.dimVecIndexToBaseUnit_[nonZeroIndex] = theUnit.csCode_;
154
+ }
141
155
  } // end addUnit
142
156
 
143
157
 
@@ -65,14 +65,14 @@ var Ucum = {
65
65
  * displaying messages on a web site; should be blank when output is
66
66
  * to a file.
67
67
  */
68
- openEmphHTML_: '<span class="emphSpan">',
68
+ openEmphHTML_: ' <span class="emphSpan">',
69
69
 
70
70
  /**
71
71
  * Closing HTML used to emphasize portions of error messages. Used when
72
72
  * displaying messages on a web site; should be blank when output is
73
73
  * to a file.
74
74
  */
75
- closeEmphHTML_: '</span>',
75
+ closeEmphHTML_: '</span> ',
76
76
 
77
77
  /**
78
78
  * Message that is displayed when annotations are included in a unit
@@ -1 +1 @@
1
- {"version":3,"sources":["../source/config.js"],"names":["Ucum","dimLen_","validOps_","codeSep_","valMsgStart_","valMsgEnd_","cnvMsgStart_","cnvMsgEnd_","openEmph_","closeEmph_","openEmphHTML_","closeEmphHTML_","bracesMsg_","needMoleWeightMsg_","csvCols_","inputKey_","specUnits_"],"mappings":";;;;;;;AAAA;;;;;;;AAQO,IAAIA,IAAI,GAAG;AAEhB;;;;;;AAMA;;AAEA;;;;;;AAMAC,EAAAA,OAAO,EAAE,CAhBO;;AAkBhB;;;;AAIAC,EAAAA,SAAS,EAAE,CAAC,GAAD,EAAM,GAAN,CAtBK;;AAyBhB;;;;AAIAC,EAAAA,QAAQ,EAAG,IA7BK;AA+BhB;AACAC,EAAAA,YAAY,EAAG,eAhCC;AAiChBC,EAAAA,UAAU,EAAG,GAjCG;AAkChBC,EAAAA,YAAY,EAAG,uBAlCC;AAmChBC,EAAAA,UAAU,EAAG,GAnCG;;AAsClB;;;;;AAKEC,EAAAA,SAAS,EAAG,KA3CI;;AA6ChB;;;;;AAKAC,EAAAA,UAAU,EAAG,KAlDG;;AAoDhB;;;;;AAKAC,EAAAA,aAAa,EAAG,yBAzDA;;AA2DhB;;;;;AAKAC,EAAAA,cAAc,EAAG,SAhED;;AAkEhB;;;;AAIAC,EAAAA,UAAU,EAAG,8DACA,6DADA,GAEA,iCAxEG;;AA0EhB;;;;;AAKAC,EAAAA,kBAAkB,EAAG,0DACA,uDADA,GAEA,8CAjFL;;AAmFhB;;;;AAIAC,EAAAA,QAAQ,EAAG;AACT,2BAAwB,SADf;AAET,sBAAmB,gBAFV;AAGT,sBAAmB,OAHV;AAIT,gBAAa,WAJJ;AAKT,cAAW,SALF;AAMT,gBAAa,WANJ;AAOT,gBAAa;AAPJ,GAvFK;;AAiGhB;;;AAGAC,EAAAA,SAAS,EAAG,qBApGI;;AAsGhB;;;;;;;AAOCC,EAAAA,UAAU,EAAG;AAAE,gBAAa,gBAAf;AACE,uBAAoB;AADtB;AA7GE,CAAX","sourcesContent":["/*\n * This defines the namespace for the UCUM classes and provides\n * a place for the definition of global variables and constants.\n *\n * The javascript for this UCUM implementation uses syntax as\n * defined by the ECMAScript 6 standard\n */\n\nexport var Ucum = {\n\n /**\n * Flag indicating whether or not we're using case sensitive labels\n * I don't think we need this. I think we're just going with\n * case sensitive, per Clem. Gunther's code has this flag, but I\n * am removing it, at least for now. lm, 6/2016\n */\n //caseSensitive_: true ,\n\n /**\n * The number of elements in a Dimension array. Currently this\n * is set as a configuration variable, but when we get to the point\n * of loading the unit definitions from a file, this value will be\n * set from that.\n */\n dimLen_: 7,\n\n /**\n * The characters used as valid operators in a UCUM unit expression,\n * where '.' is for multiplication and '/' is for division.\n */\n validOps_: ['.', '/'],\n\n\n /**\n * The string used to separate a unit code and unit name when they\n * are displayed together\n */\n codeSep_ : ': ',\n\n // Message text variations for validation methods and conversion methods\n valMsgStart_ : 'Did you mean ',\n valMsgEnd_ : '?' ,\n cnvMsgStart_ : 'We assumed you meant ',\n cnvMsgEnd_ : '.',\n\n\n/**\n * Default opening string used to emphasize portions of error messages.\n * Used when NOT displaying messages on a web site, i.e., for output\n * from the library methods or to a file.\n */\n openEmph_ : ' ->',\n\n /**\n * Default closing string used to emphasize portions of error messages.\n * Used when NOT displaying messages on a web site, i.e., for output\n * from the library methods or to a file.\n */\n closeEmph_ : '<- ' ,\n\n /**\n * Opening HTML used to emphasize portions of error messages. Used when\n * displaying messages on a web site; should be blank when output is\n * to a file.\n */\n openEmphHTML_ : '<span class=\"emphSpan\">',\n\n /**\n * Closing HTML used to emphasize portions of error messages. Used when\n * displaying messages on a web site; should be blank when output is\n * to a file.\n */\n closeEmphHTML_ : '</span>' ,\n\n /**\n * Message that is displayed when annotations are included in a unit\n * string, to let the user know how they are interpreted.\n */\n bracesMsg_ : 'FYI - annotations (text in curly braces {}) are ignored, ' +\n 'except that an annotation without a leading symbol implies ' +\n 'the default unit 1 (the unity).',\n\n /**\n * Message that is displayed or returned when a conversion is requested\n * for two units where (only) a mass<->moles conversion is appropriate\n * but no molecular weight was specified.\n */\n needMoleWeightMsg_ : 'Did you wish to convert between mass and moles? The ' +\n 'molecular weight of the substance represented by the ' +\n 'units is required to perform the conversion.',\n\n /**\n * Hash that matches unit column names to names used in the csv file\n * that is submitted to the data updater.\n */\n csvCols_ : {\n 'case-sensitive code' : 'csCode_',\n 'LOINC property' : 'loincProperty_',\n 'name (display)' : 'name_',\n 'synonyms' : 'synonyms_',\n 'source' : 'source_',\n 'category' : 'category_',\n 'Guidance' : 'guidance_'\n } ,\n\n /**\n * Name of the column in the csv file that serves as the key\n */\n inputKey_ : 'case-sensitive code' ,\n\n /**\n * Special codes that contain operators within brackets. The operator\n * within these codes causes them to parse incorrectly if they are preceded\n * by a prefix, because the parsing algorithm splits them up on the operator.\n * So we use this object to identify them and substitute placeholders to\n * avoid that.\n */\n specUnits_ : { 'B[10.nV]' : 'specialUnitOne',\n '[m/s2/Hz^(1/2)]' : 'specialUnitTwo'}\n} ;\n\n\n"],"file":"config.js"}
1
+ {"version":3,"sources":["../source/config.js"],"names":["Ucum","dimLen_","validOps_","codeSep_","valMsgStart_","valMsgEnd_","cnvMsgStart_","cnvMsgEnd_","openEmph_","closeEmph_","openEmphHTML_","closeEmphHTML_","bracesMsg_","needMoleWeightMsg_","csvCols_","inputKey_","specUnits_"],"mappings":";;;;;;;AAAA;;;;;;;AAQO,IAAIA,IAAI,GAAG;AAEhB;;;;;;AAMA;;AAEA;;;;;;AAMAC,EAAAA,OAAO,EAAE,CAhBO;;AAmBhB;;;;AAIAC,EAAAA,SAAS,EAAE,CAAC,GAAD,EAAM,GAAN,CAvBK;;AA0BhB;;;;AAIAC,EAAAA,QAAQ,EAAG,IA9BK;AAgChB;AACAC,EAAAA,YAAY,EAAG,eAjCC;AAkChBC,EAAAA,UAAU,EAAG,GAlCG;AAmChBC,EAAAA,YAAY,EAAG,uBAnCC;AAoChBC,EAAAA,UAAU,EAAG,GApCG;;AAuClB;;;;;AAKEC,EAAAA,SAAS,EAAG,KA5CI;;AA8ChB;;;;;AAKAC,EAAAA,UAAU,EAAG,KAnDG;;AAqDhB;;;;;AAKAC,EAAAA,aAAa,EAAG,0BA1DA;;AA4DhB;;;;;AAKAC,EAAAA,cAAc,EAAG,UAjED;;AAmEhB;;;;AAIAC,EAAAA,UAAU,EAAG,8DACA,6DADA,GAEA,iCAzEG;;AA2EhB;;;;;AAKAC,EAAAA,kBAAkB,EAAG,0DACA,uDADA,GAEA,8CAlFL;;AAoFhB;;;;AAIAC,EAAAA,QAAQ,EAAG;AACT,2BAAwB,SADf;AAET,sBAAmB,gBAFV;AAGT,sBAAmB,OAHV;AAIT,gBAAa,WAJJ;AAKT,cAAW,SALF;AAMT,gBAAa,WANJ;AAOT,gBAAa;AAPJ,GAxFK;;AAkGhB;;;AAGAC,EAAAA,SAAS,EAAG,qBArGI;;AAuGhB;;;;;;;AAOCC,EAAAA,UAAU,EAAG;AAAE,gBAAa,gBAAf;AACE,uBAAoB;AADtB;AA9GE,CAAX","sourcesContent":["/*\n * This defines the namespace for the UCUM classes and provides\n * a place for the definition of global variables and constants.\n *\n * The javascript for this UCUM implementation uses syntax as\n * defined by the ECMAScript 6 standard\n */\n\nexport var Ucum = {\n\n /**\n * Flag indicating whether or not we're using case sensitive labels\n * I don't think we need this. I think we're just going with\n * case sensitive, per Clem. Gunther's code has this flag, but I\n * am removing it, at least for now. lm, 6/2016\n */\n //caseSensitive_: true ,\n\n /**\n * The number of elements in a Dimension array. Currently this\n * is set as a configuration variable, but when we get to the point\n * of loading the unit definitions from a file, this value will be\n * set from that.\n */\n dimLen_: 7,\n\n\n /**\n * The characters used as valid operators in a UCUM unit expression,\n * where '.' is for multiplication and '/' is for division.\n */\n validOps_: ['.', '/'],\n\n\n /**\n * The string used to separate a unit code and unit name when they\n * are displayed together\n */\n codeSep_ : ': ',\n\n // Message text variations for validation methods and conversion methods\n valMsgStart_ : 'Did you mean ',\n valMsgEnd_ : '?' ,\n cnvMsgStart_ : 'We assumed you meant ',\n cnvMsgEnd_ : '.',\n\n\n/**\n * Default opening string used to emphasize portions of error messages.\n * Used when NOT displaying messages on a web site, i.e., for output\n * from the library methods or to a file.\n */\n openEmph_ : ' ->',\n\n /**\n * Default closing string used to emphasize portions of error messages.\n * Used when NOT displaying messages on a web site, i.e., for output\n * from the library methods or to a file.\n */\n closeEmph_ : '<- ' ,\n\n /**\n * Opening HTML used to emphasize portions of error messages. Used when\n * displaying messages on a web site; should be blank when output is\n * to a file.\n */\n openEmphHTML_ : ' <span class=\"emphSpan\">',\n\n /**\n * Closing HTML used to emphasize portions of error messages. Used when\n * displaying messages on a web site; should be blank when output is\n * to a file.\n */\n closeEmphHTML_ : '</span> ' ,\n\n /**\n * Message that is displayed when annotations are included in a unit\n * string, to let the user know how they are interpreted.\n */\n bracesMsg_ : 'FYI - annotations (text in curly braces {}) are ignored, ' +\n 'except that an annotation without a leading symbol implies ' +\n 'the default unit 1 (the unity).',\n\n /**\n * Message that is displayed or returned when a conversion is requested\n * for two units where (only) a mass<->moles conversion is appropriate\n * but no molecular weight was specified.\n */\n needMoleWeightMsg_ : 'Did you wish to convert between mass and moles? The ' +\n 'molecular weight of the substance represented by the ' +\n 'units is required to perform the conversion.',\n\n /**\n * Hash that matches unit column names to names used in the csv file\n * that is submitted to the data updater.\n */\n csvCols_ : {\n 'case-sensitive code' : 'csCode_',\n 'LOINC property' : 'loincProperty_',\n 'name (display)' : 'name_',\n 'synonyms' : 'synonyms_',\n 'source' : 'source_',\n 'category' : 'category_',\n 'Guidance' : 'guidance_'\n } ,\n\n /**\n * Name of the column in the csv file that serves as the key\n */\n inputKey_ : 'case-sensitive code' ,\n\n /**\n * Special codes that contain operators within brackets. The operator\n * within these codes causes them to parse incorrectly if they are preceded\n * by a prefix, because the parsing algorithm splits them up on the operator.\n * So we use this object to identify them and substitute placeholders to\n * avoid that.\n */\n specUnits_ : { 'B[10.nV]' : 'specialUnitOne',\n '[m/s2/Hz^(1/2)]' : 'specialUnitTwo'}\n} ;\n\n\n"],"file":"config.js"}
@@ -134,24 +134,17 @@ class UcumLhcUtils {
134
134
  if (valConv === undefined) valConv = 'validate';
135
135
  let resp = this.getSpecifiedUnit(uStr, valConv, suggest);
136
136
  let theUnit = resp['unit'];
137
- let retObj = {};
138
-
139
- if (!theUnit) {
140
- retObj = {
141
- 'status': !resp['origString'] || resp['origString'] === null ? 'error' : 'invalid',
142
- 'ucumCode': null
143
- };
144
- } else {
145
- retObj = {
146
- 'status': resp['origString'] === uStr ? 'valid' : 'invalid',
147
- 'ucumCode': resp['origString'],
148
- 'unit': {
149
- 'code': theUnit.csCode_,
150
- 'name': theUnit.name_,
151
- 'guidance': theUnit.guidance_
152
- }
153
- };
154
- }
137
+ let retObj = !theUnit ? {
138
+ 'ucumCode': null
139
+ } : {
140
+ 'ucumCode': resp['origString'],
141
+ 'unit': {
142
+ 'code': theUnit.csCode_,
143
+ 'name': theUnit.name_,
144
+ 'guidance': theUnit.guidance_
145
+ }
146
+ };
147
+ retObj.status = resp.status;
155
148
 
156
149
  if (resp['suggestions']) {
157
150
  retObj['suggestions'] = resp['suggestions'];
@@ -239,10 +232,7 @@ class UcumLhcUtils {
239
232
  returnObj['msg'].push('No "from" unit expression specified.');
240
233
  }
241
234
 
242
- if (fromVal === null || isNaN(fromVal) || typeof fromVal !== 'number' && !intUtils_.isNumericString(fromVal)) {
243
- returnObj['status'] = 'error';
244
- returnObj['msg'].push('No "from" value, or an invalid "from" value, ' + 'was specified.');
245
- }
235
+ this._checkFromVal(fromVal, returnObj);
246
236
 
247
237
  if (toUnitCode) {
248
238
  toUnitCode = toUnitCode.trim();
@@ -333,6 +323,114 @@ class UcumLhcUtils {
333
323
  return returnObj;
334
324
  } // end convertUnitTo
335
325
 
326
+ /**
327
+ * Converts the given unit string into its base units, their exponents, and
328
+ * a magnitude, and returns that data.
329
+ * @param fromUnit the unit string to be converted to base units information
330
+ * @param fromVal the number of "from" units to be converted
331
+ * @returns an object with the properties:
332
+ * 'status' indicates whether the result succeeded. The value will be one of:
333
+ * 'succeeded': the conversion was successfully calculated (which can be
334
+ * true even if it was already in base units);
335
+ * 'invalid': fromUnit is not a valid UCUM code;
336
+ * 'failed': the conversion could not be made (e.g., if it is an "arbitrary" unit);
337
+ * 'error': if an error occurred (an input or programming error)
338
+ * 'msg': an array of one or more messages, if the string is invalid or
339
+ * an error occurred, indicating the problem, or a suggestion of a
340
+ * substitution such as the substitution of 'G' for 'Gauss', or
341
+ * an empty array if no messages were generated. There can also be a
342
+ * message that is just informational or warning.
343
+ * 'magnitude': the new value when fromVal units of fromUnits is expressed in the base units.
344
+ * 'fromUnitIsSpecial': whether the input unit fromUnit is a "special unit"
345
+ * as defined in UCUM. This means there is some function applied to convert
346
+ * between fromUnit and the base units, so the returned magnitude is likely not
347
+ * useful as a scale factor for other conversions (i.e., it only has validity
348
+ * and usefulness for the input values that produced it).
349
+ * 'unitToExp': a map of base units in uStr to their exponent
350
+ */
351
+
352
+
353
+ convertToBaseUnits(fromUnit, fromVal) {
354
+ let retObj = {};
355
+
356
+ this._checkFromVal(fromVal, retObj);
357
+
358
+ if (!retObj.status) {
359
+ // could be set to 'error' by _checkFromVal
360
+ let inputUnitLookup = this.getSpecifiedUnit(fromUnit, 'validate');
361
+ retObj = {
362
+ status: inputUnitLookup.status == 'valid' ? 'succeeded' : inputUnitLookup.status
363
+ };
364
+ let unit = inputUnitLookup.unit;
365
+ retObj.msg = inputUnitLookup.retMsg || [];
366
+
367
+ if (!unit) {
368
+ if (inputUnitLookup.retMsg?.length == 0) retObj.msg.push('Could not find unit information for ' + fromUnit);
369
+ } else if (unit.isArbitrary_) {
370
+ retObj.msg.push('Arbitrary units cannot be converted to base units or other units.');
371
+ retObj.status = 'failed';
372
+ } else if (retObj.status == 'succeeded') {
373
+ let unitToExp = {};
374
+ let dimVec = unit.dim_?.dimVec_;
375
+ let baseUnitString = '1';
376
+
377
+ if (dimVec) {
378
+ let dimVecIndexToBaseUnit = UnitTables.getInstance().dimVecIndexToBaseUnit_;
379
+
380
+ for (let i = 0, len = dimVec.length; i < len; ++i) {
381
+ let exp = dimVec[i];
382
+
383
+ if (exp) {
384
+ unitToExp[dimVecIndexToBaseUnit[i]] = exp;
385
+ baseUnitString += '.' + dimVecIndexToBaseUnit[i] + exp;
386
+ }
387
+ }
388
+ } // The unit might have a conversion function, which has to be applied; we
389
+ // cannot just assume unit_.magnitude_ is the magnitude in base units.
390
+
391
+
392
+ let retUnitLookup = this.getSpecifiedUnit(baseUnitString, 'validate'); // There should not be any error in retUnitLookup, unless there is a bug.
393
+
394
+ let retUnit = retUnitLookup.unit;
395
+
396
+ if (retUnitLookup.status !== 'valid') {
397
+ retObj.msg.push('Unable construct base unit string; tried ' + baseUnitString);
398
+ retObj.status = 'error';
399
+ } else {
400
+ try {
401
+ retObj.magnitude = retUnit.convertFrom(fromVal, unit);
402
+ } catch (e) {
403
+ retObj.msg.push(e.toString());
404
+ retObj.status = 'error';
405
+ }
406
+
407
+ if (retObj.status == 'succeeded') {
408
+ retObj.unitToExp = unitToExp;
409
+ retObj.fromUnitIsSpecial = unit.isSpecial_;
410
+ }
411
+ }
412
+ }
413
+ }
414
+
415
+ return retObj;
416
+ }
417
+ /**
418
+ * Checks the given value as to whether it is suitable as a "from" value in a
419
+ * unit conversion. If it is not, the responseObj will have its status set
420
+ * to 'error' and a message added.
421
+ * @param fromVal The value to check
422
+ * @param responseObj the object that will be updated if the value is not
423
+ * usable.
424
+ */
425
+
426
+
427
+ _checkFromVal(fromVal, responseObj) {
428
+ if (fromVal === null || isNaN(fromVal) || typeof fromVal !== 'number' && !intUtils_.isNumericString(fromVal)) {
429
+ responseObj.status = 'error';
430
+ if (!responseObj.msg) responseObj.msg = [];
431
+ responseObj.msg.push('No "from" value, or an invalid "from" value, ' + 'was specified.');
432
+ }
433
+ }
336
434
  /**
337
435
  * This method accepts a term and looks for units that include it as
338
436
  * a synonym - or that include the term in its name.
@@ -377,13 +475,18 @@ class UcumLhcUtils {
377
475
  * true indicates suggestions are wanted; false indicates they are not,
378
476
  * and is the default if the parameter is not specified;
379
477
  * @returns a hash containing:
478
+ * 'status' will be 'valid' (uName is a valid UCUM code), 'invalid'
479
+ * (the uStr is not a valid UCUM code, and substitutions or
480
+ * suggestions may or may not be returned, depending on what was
481
+ * requested and found); or 'error' (an input or programming error
482
+ * occurred);
380
483
  * 'unit' the unit object (or null if there were problems creating the
381
484
  * unit);
382
485
  * 'origString' the possibly updated unit string passed in;
383
486
  * 'retMsg' an array of user messages (informational, error or warning) if
384
487
  * any were generated (IF any were generated, otherwise will be an
385
488
  * empty array); and
386
- * 'suggestions' is an array of 1 or more hash objects. Each hash
489
+ * 'suggestions' is an array of 1 or more hash objects. Each hash
387
490
  * contains three elements:
388
491
  * 'msg' which is a message indicating what unit expression the
389
492
  * suggestions are for;
@@ -429,8 +532,18 @@ class UcumLhcUtils {
429
532
  } // end if the unit was not found as a unit name
430
533
 
431
534
  } // end if a unit expression was specified
535
+ // Set the status field
432
536
 
433
537
 
538
+ if (!retObj.unit) {
539
+ // No unit was found; check whether origString has a value
540
+ retObj.status = !retObj.origString ? 'error' : 'invalid';
541
+ } else {
542
+ // Check whether substitutions were made to the unit string in order to
543
+ // find the unit
544
+ retObj.status = retObj.origString === uName ? 'valid' : 'invalid';
545
+ }
546
+
434
547
  return retObj;
435
548
  } // end getSpecifiedUnit
436
549
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../source/ucumLhcUtils.js"],"names":["Ucum","require","UnitTables","UnitString","UcumLhcUtils","constructor","getInstance","unitsCount","ucumJsonDefs","loadJsonDefs","uStrParser_","useHTMLInMessages","use","undefined","useBraceMsgForEachString","validateUnitString","uStr","suggest","valConv","resp","getSpecifiedUnit","theUnit","retObj","csCode_","name_","guidance_","convertUnitTo","fromUnitCode","fromVal","toUnitCode","molecularWeight","returnObj","trim","push","isNaN","intUtils_","isNumericString","fromUnit","parseResp","concat","toUnit","convertFrom","moleExp_","Error","isMoleMassCommensurable","convertMolToMass","convertMassToMol","err","message","needMoleWeightMsg_","checkSynonyms","theSyn","getSynonyms","uName","utab","getUnitByCode","parseString","console","log","unshift","commensurablesList","fromName","retMsg","commUnits","length","dimVec","fromDim","getProperty","getUnitsByDimension"],"mappings":";;;;;;;AAOA;;AAIA;;;;;;AAXA;;;;;;AAMA,IAAIA,IAAI,GAAGC,OAAO,CAAC,aAAD,CAAP,CAAuBD,IAAlC;;AAEA,IAAIE,UAAU,GAAGD,OAAO,CAAC,iBAAD,CAAP,CAA2BC,UAA5C;;AACA,IAAIC,UAAU,GAAGF,OAAO,CAAC,iBAAD,CAAP,CAA2BE,UAA5C;;AAIA;;;AAGO,MAAMC,YAAN,CAAmB;AAExB;;;;;AAKAC,EAAAA,WAAW,GAAG;AAEV,QAAIH,UAAU,CAACI,WAAX,GAAyBC,UAAzB,OAA0C,CAA9C,EAAiD;AAE/C;AACAC,iCAAaC,YAAb;AACD,KANS,CAQV;AACA;;;AACA,SAAKC,WAAL,GAAmBP,UAAU,CAACG,WAAX,EAAnB;AAEH,GAnBuB,CAmBtB;;AAGF;;;;;;;;;;AAQAK,EAAAA,iBAAiB,CAACC,GAAD,EAAM;AACrB,QAAIA,GAAG,KAAKC,SAAZ,EACED,GAAG,GAAG,IAAN;AACF,SAAKF,WAAL,CAAiBC,iBAAjB,CAAmCC,GAAnC;AACD;AAGD;;;;;;;;;;;AASAE,EAAAA,wBAAwB,CAACF,GAAD,EAAM;AAC5B,QAAIA,GAAG,KAAKC,SAAZ,EACED,GAAG,GAAG,IAAN;AACF,SAAKF,WAAL,CAAiBI,wBAAjB,CAA0CF,GAA1C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C0B,CAgCzB;;;AACF,WAAO,CAACF,SAAD,EAAaD,MAAb,CAAP;AACD,GA5duB,CA4dtB;;;AA5dsB,C,CA8dxB;;AAGF;;;;;;;;;;;;;;;;;;AAcA1D,YAAY,CAACE,WAAb,GAA2B,YAAU;AACnC,SAAO,IAAIF,YAAJ,EAAP;AACD,CAFD","sourcesContent":["/**\n * This class provides a single point of access to the LHC UCUM utilities\n *\n * @author Lee Mericle\n *\n */\nvar Ucum = require('./config.js').Ucum;\nimport {ucumJsonDefs} from './ucumJsonDefs.js';\nvar UnitTables = require('./unitTables.js').UnitTables;\nvar UnitString = require('./unitString.js').UnitString;\n\nimport * as intUtils_ from \"./ucumInternalUtils.js\";\n\n/**\n * UCUM external utilities class\n */\nexport class UcumLhcUtils {\n\n /**\n * Constructor. This loads the json prefix and unit definitions if\n * they haven't been loaded already and creates itself as a singleton object.\n *\n */\n constructor() {\n\n if (UnitTables.getInstance().unitsCount() === 0) {\n\n // Load the prefix and unit objects\n ucumJsonDefs.loadJsonDefs();\n }\n\n // Get the UnitString parser that will be used with this instance\n // of the LHC Utilities\n this.uStrParser_ = UnitString.getInstance();\n\n } // end constructor\n\n\n /**\n * This method calls the useHTMLInMessages method on the UnitString\n * object. It should be called by web applications that use\n * these utilities.\n *\n * @param use flag indicating whether or not to use the braces message;\n * defaults to true\n */\n useHTMLInMessages(use) {\n if (use === undefined)\n use = true ;\n this.uStrParser_.useHTMLInMessages(use);\n }\n\n\n /**\n * This method calls the useBraceMsgForEachString method on the UnitString\n * object. It should be called by web applications where unit\n * strings are validated individually (as opposed to validating a whole\n * file of unit strings).\n *\n * @param use flag indicating whether or not to use the braces message;\n * defaults to true\n */\n useBraceMsgForEachString(use) {\n if (use === undefined)\n use = true ;\n this.uStrParser_.useBraceMsgForEachString(use);\n }\n\n\n /**\n * This method validates a unit string. It first checks to see if the\n * string passed in is a unit code that is found in the unit codes table.\n * If it is not found it parses the string to see if it resolves to a\n * valid unit string.\n *\n * If a valid unit cannot be found, the string is tested for some common\n * errors, such as missing brackets or a missing multiplication operator.\n * If found, the error is reported in the messages array that is returned.\n *\n * If a valid unit cannot be found and an error cannot be discerned, this\n * may return, if requested, a list of suggested units in the messages\n * array that is returned. Suggestions are based on matching the expression\n * with unit names and synonyms.\n *\n * @param uStr the string to be validated\n * @param suggest a boolean to indicate whether or not suggestions are\n * requested for a string that cannot be resolved to a valid unit;\n * true indicates suggestions are wanted; false indicates they are not,\n * and is the default if the parameter is not specified;\n * @param valConv a string indicating if this validation request was initiated\n * by a validation task ('validate') or a conversion task ('convert'),\n * used only for the demo code, and the default is 'Validator' if the\n * parameter is not specified;\n * @returns an object with five properties:\n * 'status' will be 'valid' (the uStr is a valid UCUM code), 'invalid'\n * (the uStr is not a valid UCUM code, and substitutions or\n * suggestions may or may not be returned, depending on what was\n * requested and found); or 'error' (an input or programming error\n * occurred);\n * 'ucumCode' the valid ucum code, which may differ from what was passed\n * in (e.g., if 'Gauss' is passed in, this will contain 'G') OR null if\n * the string was flagged as invalid or an error occurred;\n * 'msg' is an array of one or more messages, if the string is invalid or\n * an error occurred, indicating the problem, or an explanation of a\n * substitution such as the substitution of 'G' for 'Gauss', or\n * an empty array if no messages were generated;\n * 'unit' which is null if no unit is found, or a hash for a unit found:\n * 'code' is the unit's ucum code (G in the above example;\n * 'name' is the unit's name (Gauss in the above example); and\n * 'guidance' is the unit's guidance/description data; and\n * 'suggestions' if suggestions were requested and found, this is an array\n * of one or more hash objects. Each hash contains three elements:\n * 'msg' which is a message indicating what part of the uStr input\n * parameter the suggestions are for;\n * 'invalidUnit' which is the unit expression the suggestions are\n * for; and\n * 'units' which is an array of data for each suggested unit found.\n * Each array will contain the unit code, the unit name and the\n * unit guidance (if any).\n * If no suggestions were requested and found, this property is not\n * returned.\n */\n validateUnitString(uStr, suggest, valConv) {\n\n if (suggest === undefined)\n suggest = false ;\n\n if (valConv === undefined)\n valConv = 'validate' ;\n\n let resp = this.getSpecifiedUnit(uStr, valConv, suggest);\n let theUnit = resp['unit'];\n let retObj = {};\n if (!theUnit) {\n retObj = {'status': (!resp['origString'] || resp['origString'] === null) ?\n 'error' : 'invalid',\n 'ucumCode': null};\n }\n else {\n retObj = {'status': resp['origString'] === uStr ? 'valid': 'invalid',\n 'ucumCode': resp['origString'],\n 'unit': {'code': theUnit.csCode_,\n 'name': theUnit.name_,\n 'guidance': theUnit.guidance_ }};\n }\n if (resp['suggestions']) {\n retObj['suggestions'] = resp['suggestions'];\n }\n retObj['msg'] = resp['retMsg'];\n return retObj;\n\n } // end validateUnitString\n\n\n /**\n * This method converts one unit to another\n *\n * @param fromUnitCode the unit code/expression/string of the unit to be converted\n * @param fromVal the number of \"from\" units to be converted to \"to\" units\n * @param toUnitCode the unit code/expression/string of the unit that the from\n * field is to be converted to\n * @param suggest a boolean to indicate whether or not suggestions are\n * requested for a string that cannot be resolved to a valid unit;\n * true indicates suggestions are wanted; false indicates they are not,\n * and is the default if the parameter is not specified;\n * @param molecularWeight the molecular weight of the substance in question\n * when a conversion is being requested from mass to moles and vice versa.\n * This is required when one of the units represents a value in moles. It is\n * ignored if neither unit includes a measurement in moles.\n * @returns a hash with six elements:\n * 'status' that will be: 'succeeded' if the conversion was successfully\n * calculated; 'failed' if the conversion could not be made, e.g., if\n * the units are not commensurable; or 'error' if an error occurred;\n * 'toVal' the numeric value indicating the conversion amount, or null\n * if the conversion failed (e.g., if the units are not commensurable);\n * 'msg' is an array message, if the string is invalid or an error occurred,\n * indicating the problem, or an explanation of a substitution such as\n * the substitution of 'G' for 'Gauss', or an empty array if no\n * messages were generated;\n * 'suggestions' if suggestions were requested and found, this is a hash\n * that contains at most two elements:\n * 'from' which, if the fromUnitCode input parameter or one or more of\n * its components could not be found, is an array one or more hash\n * objects. Each hash contains three elements:\n * 'msg' which is a message indicating what unit expression the\n * suggestions are for;\n * 'invalidUnit' which is the unit expression the suggestions\n * are for; and\n * 'units' which is an array of data for each suggested unit found.\n * Each array will contain the unit code, the unit name and the\n * unit guidance (if any).\n * If no suggestions were found for the fromUnitCode this element\n * will not be included.\n * 'to' which, if the \"to\" unit expression or one or more of its\n * components could not be found, is an array one or more hash objects. Each hash\n * contains three elements:\n * 'msg' which is a message indicating what toUnitCode input\n * parameter the suggestions are for;\n * 'invalidUnit' which is the unit expression the suggestions\n * are for; and\n * 'units' which is an array of data for each suggested unit found.\n * Each array will contain the unit code, the unit name and the\n * unit guidance (if any).\n * If no suggestions were found for the toUnitCode this element\n * will not be included.\n * No 'suggestions' element will be included in the returned hash\n * object if none were found, whether or not they were requested.\n * 'fromUnit' the unit object for the fromUnitCode passed in; returned\n * in case it's needed for additional data from the object; and\n * 'toUnit' the unit object for the toUnitCode passed in; returned\n * in case it's needed for additional data from the object.\n */\n convertUnitTo(fromUnitCode, fromVal, toUnitCode, suggest, molecularWeight) {\n if (suggest === undefined)\n suggest = false ;\n\n if (molecularWeight === undefined)\n molecularWeight = null ;\n\n let returnObj = {'status' : 'failed',\n 'toVal' : null,\n 'msg' : []} ;\n\n if (fromUnitCode) {\n fromUnitCode = fromUnitCode.trim();\n }\n if (!fromUnitCode || fromUnitCode == '') {\n returnObj['status'] = 'error';\n returnObj['msg'].push('No \"from\" unit expression specified.');\n }\n if (fromVal === null || isNaN(fromVal) || (typeof fromVal !== 'number' &&\n !intUtils_.isNumericString(fromVal))) {\n returnObj['status'] = 'error';\n returnObj['msg'].push('No \"from\" value, or an invalid \"from\" value, ' +\n 'was specified.');\n }\n if (toUnitCode) {\n toUnitCode = toUnitCode.trim();\n }\n if (!toUnitCode || toUnitCode == '') {\n returnObj['status'] = 'error';\n returnObj['msg'].push('No \"to\" unit expression specified.');\n }\n if (returnObj['status'] !== 'error') {\n try {\n let fromUnit = null;\n\n let parseResp = this.getSpecifiedUnit(fromUnitCode, 'convert', suggest);\n fromUnit = parseResp['unit'];\n if (parseResp['retMsg'])\n returnObj['msg'] = returnObj['msg'].concat(parseResp['retMsg']);\n if (parseResp['suggestions']) {\n returnObj['suggestions'] = {};\n returnObj['suggestions']['from'] = parseResp['suggestions'];\n }\n if (!fromUnit) {\n returnObj['msg'].push(`Unable to find a unit for ${fromUnitCode}, ` +\n `so no conversion could be performed.`);\n }\n\n let toUnit = null;\n parseResp = this.getSpecifiedUnit(toUnitCode, 'convert', suggest);\n toUnit = parseResp['unit'];\n if (parseResp['retMsg'])\n returnObj['msg'] = returnObj['msg'].concat(parseResp['retMsg']);\n if (parseResp['suggestions']) {\n if (!returnObj['suggestions'])\n returnObj['suggestions'] = {} ;\n returnObj['suggestions']['to'] = parseResp['suggestions'];\n }\n if (!toUnit) {\n returnObj['msg'].push(`Unable to find a unit for ${toUnitCode}, ` +\n `so no conversion could be performed.`);\n }\n\n if (fromUnit && toUnit) {\n try {\n // if no molecular weight was specified perform a normal conversion\n if (!molecularWeight) {\n returnObj['toVal'] = toUnit.convertFrom(fromVal, fromUnit);\n }\n else {\n if (fromUnit.moleExp_ !== 0 && toUnit.moleExp_ !== 0) {\n throw(new Error('A molecular weight was specified ' +\n 'but a mass <-> mole conversion cannot be executed for two ' +\n 'mole-based units. No conversion was attempted.'));\n }\n if (fromUnit.moleExp_ === 0 && toUnit.moleExp_ === 0) {\n throw(new Error('A molecular weight was specified ' +\n 'but a mass <-> mole conversion cannot be executed when ' +\n 'neither unit is mole-based. No conversion was attempted.'));\n }\n if (!fromUnit.isMoleMassCommensurable(toUnit)) {\n throw(new Error(`Sorry. ${fromUnitCode} cannot be ` +\n `converted to ${toUnitCode}.`));\n }\n\n // if the \"from\" unit is a mole-based unit, assume a mole to mass\n // request\n if (fromUnit.moleExp_ !== 0) {\n returnObj['toVal'] =\n fromUnit.convertMolToMass(fromVal, toUnit, molecularWeight);\n }\n // else the \"to\" unit must be the mole-based unit, so assume a\n // mass to mole request\n else {\n returnObj['toVal'] =\n fromUnit.convertMassToMol(fromVal, toUnit, molecularWeight);\n }\n } // end if a molecular weight was specified\n\n // if an error hasn't been thrown - either from convertFrom or here,\n // set the return object to show success\n returnObj['status'] = 'succeeded';\n returnObj['fromUnit'] = fromUnit;\n returnObj['toUnit'] = toUnit;\n }\n catch (err) {\n returnObj['status'] = 'failed';\n returnObj['msg'].push(err.message);\n }\n\n\n } // end if we have the from and to units\n }\n catch (err) {\n if (err.message == Ucum.needMoleWeightMsg_)\n returnObj['status'] = 'failed';\n else\n returnObj['status'] = 'error';\n returnObj['msg'].push(err.message);\n }\n }\n\n return returnObj ;\n\n } // end convertUnitTo\n\n\n /**\n * This method accepts a term and looks for units that include it as\n * a synonym - or that include the term in its name.\n *\n * @param theSyn the term to search for\n * @returns a hash with up to three elements:\n * 'status' contains the status of the request, which can be 'error',\n * 'failed' or succeeded';\n * 'msg' which contains a message for an error or if no units were found; and\n * 'units' which is an array that contains one hash for each unit found:\n * 'code' is the unit's csCode_\n * 'name' is the unit's name_\n * 'guidance' is the unit's guidance_\n *\n */\n checkSynonyms(theSyn) {\n let retObj = {} ;\n if (theSyn === undefined || theSyn === null) {\n retObj['status'] = 'error';\n retObj['msg'] = 'No term specified for synonym search.'\n }\n else {\n retObj = intUtils_.getSynonyms(theSyn);\n } // end if a search synonym was supplied\n\n return retObj ;\n\n } // end checkSynonyms\n\n\n /**\n * This method parses a unit string to get (or try to get) the unit\n * represented by the string. It returns an error message if no string was specified\n * or if any errors were encountered trying to get the unit.\n *\n * @param uName the expression/string representing the unit\n * @param valConv indicates what type of request this is for - a request to\n * validate (pass in 'validate') or a request to convert (pass in 'convert')\n * @param suggest a boolean to indicate whether or not suggestions are\n * requested for a string that cannot be resolved to a valid unit;\n * true indicates suggestions are wanted; false indicates they are not,\n * and is the default if the parameter is not specified;\n * @returns a hash containing:\n * 'unit' the unit object (or null if there were problems creating the\n * unit);\n * 'origString' the possibly updated unit string passed in;\n * 'retMsg' an array of user messages (informational, error or warning) if\n * any were generated (IF any were generated, otherwise will be an\n * empty array); and\n * 'suggestions' is an array of 1 or more hash objects. Each hash\n * contains three elements:\n * 'msg' which is a message indicating what unit expression the\n * suggestions are for;\n * 'invalidUnit' which is the unit expression the suggestions are\n * for; and\n * 'units' which is an array of data for each suggested unit found.\n * Each array will contain the unit code, the unit name and the\n * unit guidance (if any).\n * The return hash will not contain a suggestions array if a valid unit\n * was found or if suggestions were not requested and found.\n */\n getSpecifiedUnit(uName, valConv, suggest) {\n\n if (suggest === undefined)\n suggest = false ;\n\n let retObj = {};\n retObj['retMsg'] = [];\n\n if (!uName) {\n retObj['retMsg'].push('No unit string specified.');\n }\n else {\n let utab = UnitTables.getInstance();\n uName = uName.trim();\n\n // go ahead and just try using the name as the code. This may or may not\n // work, but if it does, it cuts out a lot of parsing.\n let theUnit = utab.getUnitByCode(uName);\n\n // If we found it, set the returned unit string to what was passed in;\n // otherwise try parsing as a unit string\n if (theUnit) {\n retObj['unit'] = theUnit ;\n retObj['origString'] = uName;\n }\n else {\n try {\n let resp = this.uStrParser_.parseString(uName, valConv, suggest);\n retObj['unit'] = resp[0];\n retObj['origString'] = resp[1];\n if (resp[2])\n retObj['retMsg'] = resp[2];\n retObj['suggestions'] = resp[3];\n }\n catch (err) {\n console.log(`Unit requested for unit string ${uName}.` +\n 'request unsuccessful; error thrown = ' + err.message);\n retObj['retMsg'].unshift(`${uName} is not a valid unit. ` +\n `${err.message}`);\n }\n } // end if the unit was not found as a unit name\n } // end if a unit expression was specified\n\n return retObj;\n\n } // end getSpecifiedUnit\n\n\n /**\n * This method retrieves a list of units commensurable, i.e., that can be\n * converted from and to, a specified unit. Returns an error if the \"from\"\n * unit cannot be found.\n *\n * @param fromName the name/unit string of the \"from\" unit\n * @returns an array containing two elements;\n * first element is the list of commensurable units if any were found\n * second element is an error message if the \"from\" unit is not found\n */\n commensurablesList(fromName) {\n\n let retMsg = [];\n let commUnits = null ;\n let parseResp = this.getSpecifiedUnit(fromName, 'validate', false);\n let fromUnit = parseResp['unit'];\n if (parseResp['retMsg'].length > 0)\n retMsg = parseResp['retMsg'] ;\n if (!fromUnit) {\n retMsg.push(`Could not find unit ${fromName}.`);\n }\n else {\n let dimVec = null ;\n let fromDim = fromUnit.getProperty('dim_');\n if (!fromDim) {\n retMsg.push('No commensurable units were found for ' + fromName) ;\n }\n else {\n try {\n dimVec = fromDim.getProperty('dimVec_');\n }\n catch (err) {\n retMsg.push(err.message);\n if (err.message ===\n \"Dimension does not have requested property(dimVec_)\")\n dimVec = null;\n }\n if (dimVec) {\n let utab = UnitTables.getInstance();\n commUnits = utab.getUnitsByDimension(dimVec);\n }\n } // end if the from unit has a dimension vector\n } // end if we found a \"from\" unit\n return [commUnits , retMsg];\n } // end commensurablesList\n\n} // end UcumLhcUtils class\n\n\n/**\n * This function exists ONLY until the original UcumLhcUtils constructor\n * is called for the first time. It's defined here in case getInstance\n * is called before the constructor. This calls the constructor.\n *\n * The constructor redefines the getInstance function to return the\n * singleton UcumLhcUtils object. This is based on the UnitTables singleton\n * implementation; see more detail in the UnitTables constructor description.\n *\n * NO LONGER TRUE - not implemented as a singleton. This method retained to\n * avoid problems with calls to it that exist throughout the code.\n *\n * @return the (formerly singleton) UcumLhcUtils object.\n */\nUcumLhcUtils.getInstance = function(){\n return new UcumLhcUtils();\n} ;\n\n\n\n\n"],"file":"ucumLhcUtils.js"}
1
+ {"version":3,"sources":["../source/ucumLhcUtils.js"],"names":["Ucum","require","UnitTables","UnitString","UcumLhcUtils","constructor","getInstance","unitsCount","ucumJsonDefs","loadJsonDefs","uStrParser_","useHTMLInMessages","use","undefined","useBraceMsgForEachString","validateUnitString","uStr","suggest","valConv","resp","getSpecifiedUnit","theUnit","retObj","csCode_","name_","guidance_","status","convertUnitTo","fromUnitCode","fromVal","toUnitCode","molecularWeight","returnObj","trim","push","_checkFromVal","fromUnit","parseResp","concat","toUnit","convertFrom","moleExp_","Error","isMoleMassCommensurable","convertMolToMass","convertMassToMol","err","message","needMoleWeightMsg_","convertToBaseUnits","inputUnitLookup","unit","msg","retMsg","length","isArbitrary_","unitToExp","dimVec","dim_","dimVec_","baseUnitString","dimVecIndexToBaseUnit","dimVecIndexToBaseUnit_","i","len","exp","retUnitLookup","retUnit","magnitude","e","toString","fromUnitIsSpecial","isSpecial_","responseObj","isNaN","intUtils_","isNumericString","checkSynonyms","theSyn","getSynonyms","uName","utab","getUnitByCode","parseString","console","log","unshift","origString","commensurablesList","fromName","commUnits","fromDim","getProperty","getUnitsByDimension"],"mappings":";;;;;;;AAOA;;AAIA;;;;;;AAXA;;;;;;AAMA,IAAIA,IAAI,GAAGC,OAAO,CAAC,aAAD,CAAP,CAAuBD,IAAlC;;AAEA,IAAIE,UAAU,GAAGD,OAAO,CAAC,iBAAD,CAAP,CAA2BC,UAA5C;;AACA,IAAIC,UAAU,GAAGF,OAAO,CAAC,iBAAD,CAAP,CAA2BE,UAA5C;;AAIA;;;AAGO,MAAMC,YAAN,CAAmB;AAExB;;;;;AAKAC,EAAAA,WAAW,GAAG;AAEV,QAAIH,UAAU,CAACI,WAAX,GAAyBC,UAAzB,OAA0C,CAA9C,EAAiD;AAE/C;AACAC,iCAAaC,YAAb;AACD,KANS,CAQV;AACA;;;AACA,SAAKC,WAAL,GAAmBP,UAAU,CAACG,WAAX,EAAnB;AAEH,GAnBuB,CAmBtB;;AAGF;;;;;;;;;;AAQAK,EAAAA,iBAAiB,CAACC,GAAD,EAAM;AACrB,QAAIA,GAAG,KAAKC,SAAZ,EACED,GAAG,GAAG,IAAN;AACF,SAAKF,WAAL,CAAiBC,iBAAjB,CAAmCC,GAAnC;AACD;AAGD;;;;;;;;;;;AASAE,EAAAA,wBAAwB,CAACF,GAAD,EAAM;AAC5B,QAAIA,GAAG,KAAKC,SAAZ,EACED,GAAG,GAAG,IAAN;AACF,SAAKF,WAAL,CAAiBI,wBAAjB,CAA0CF,GAA1C;AACD;AAGD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDAG,EAAAA,kBAAkB,CAACC,IAAD,EAAOC,OAAP,EAAgBC,OAAhB,EAAyB;AAEzC,QAAID,OAAO,KAAKJ,SAAhB,EACEI,OAAO,GAAG,KAAV;AAEF,QAAIC,OA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;;AACF,WAAO,CAACiC,SAAD,EAAarC,MAAb,CAAP;AACD,GAxkBuB,CAwkBtB;;;AAxkBsB,C,CA0kBxB;;AAGF;;;;;;;;;;;;;;;;;;AAcAjD,YAAY,CAACE,WAAb,GAA2B,YAAU;AACnC,SAAO,IAAIF,YAAJ,EAAP;AACD,CAFD","sourcesContent":["/**\n * This class provides a single point of access to the LHC UCUM utilities\n *\n * @author Lee Mericle\n *\n */\nvar Ucum = require('./config.js').Ucum;\nimport {ucumJsonDefs} from './ucumJsonDefs.js';\nvar UnitTables = require('./unitTables.js').UnitTables;\nvar UnitString = require('./unitString.js').UnitString;\n\nimport * as intUtils_ from \"./ucumInternalUtils.js\";\n\n/**\n * UCUM external utilities class\n */\nexport class UcumLhcUtils {\n\n /**\n * Constructor. This loads the json prefix and unit definitions if\n * they haven't been loaded already and creates itself as a singleton object.\n *\n */\n constructor() {\n\n if (UnitTables.getInstance().unitsCount() === 0) {\n\n // Load the prefix and unit objects\n ucumJsonDefs.loadJsonDefs();\n }\n\n // Get the UnitString parser that will be used with this instance\n // of the LHC Utilities\n this.uStrParser_ = UnitString.getInstance();\n\n } // end constructor\n\n\n /**\n * This method calls the useHTMLInMessages method on the UnitString\n * object. It should be called by web applications that use\n * these utilities.\n *\n * @param use flag indicating whether or not to use the braces message;\n * defaults to true\n */\n useHTMLInMessages(use) {\n if (use === undefined)\n use = true ;\n this.uStrParser_.useHTMLInMessages(use);\n }\n\n\n /**\n * This method calls the useBraceMsgForEachString method on the UnitString\n * object. It should be called by web applications where unit\n * strings are validated individually (as opposed to validating a whole\n * file of unit strings).\n *\n * @param use flag indicating whether or not to use the braces message;\n * defaults to true\n */\n useBraceMsgForEachString(use) {\n if (use === undefined)\n use = true ;\n this.uStrParser_.useBraceMsgForEachString(use);\n }\n\n\n /**\n * This method validates a unit string. It first checks to see if the\n * string passed in is a unit code that is found in the unit codes table.\n * If it is not found it parses the string to see if it resolves to a\n * valid unit string.\n *\n * If a valid unit cannot be found, the string is tested for some common\n * errors, such as missing brackets or a missing multiplication operator.\n * If found, the error is reported in the messages array that is returned.\n *\n * If a valid unit cannot be found and an error cannot be discerned, this\n * may return, if requested, a list of suggested units in the messages\n * array that is returned. Suggestions are based on matching the expression\n * with unit names and synonyms.\n *\n * @param uStr the string to be validated\n * @param suggest a boolean to indicate whether or not suggestions are\n * requested for a string that cannot be resolved to a valid unit;\n * true indicates suggestions are wanted; false indicates they are not,\n * and is the default if the parameter is not specified;\n * @param valConv a string indicating if this validation request was initiated\n * by a validation task ('validate') or a conversion task ('convert'),\n * used only for the demo code, and the default is 'Validator' if the\n * parameter is not specified;\n * @returns an object with five properties:\n * 'status' will be 'valid' (the uStr is a valid UCUM code), 'invalid'\n * (the uStr is not a valid UCUM code, and substitutions or\n * suggestions may or may not be returned, depending on what was\n * requested and found); or 'error' (an input or programming error\n * occurred);\n * 'ucumCode' the valid ucum code, which may differ from what was passed\n * in (e.g., if 'Gauss' is passed in, this will contain 'G') OR null if\n * the string was flagged as invalid or an error occurred;\n * 'msg' is an array of one or more messages, if the string is invalid or\n * an error occurred, indicating the problem, or an explanation of a\n * substitution such as the substitution of 'G' for 'Gauss', or\n * an empty array if no messages were generated;\n * 'unit' which is null if no unit is found, or a hash for a unit found:\n * 'code' is the unit's ucum code (G in the above example;\n * 'name' is the unit's name (Gauss in the above example); and\n * 'guidance' is the unit's guidance/description data; and\n * 'suggestions' if suggestions were requested and found, this is an array\n * of one or more hash objects. Each hash contains three elements:\n * 'msg' which is a message indicating what part of the uStr input\n * parameter the suggestions are for;\n * 'invalidUnit' which is the unit expression the suggestions are\n * for; and\n * 'units' which is an array of data for each suggested unit found.\n * Each array will contain the unit code, the unit name and the\n * unit guidance (if any).\n * If no suggestions were requested and found, this property is not\n * returned.\n */\n validateUnitString(uStr, suggest, valConv) {\n\n if (suggest === undefined)\n suggest = false ;\n\n if (valConv === undefined)\n valConv = 'validate' ;\n\n let resp = this.getSpecifiedUnit(uStr, valConv, suggest);\n let theUnit = resp['unit'];\n let retObj = !theUnit ? {'ucumCode': null} :\n {'ucumCode': resp['origString'],\n 'unit': {'code': theUnit.csCode_,\n 'name': theUnit.name_,\n 'guidance': theUnit.guidance_ }};\n retObj.status = resp.status;\n if (resp['suggestions']) {\n retObj['suggestions'] = resp['suggestions'];\n }\n retObj['msg'] = resp['retMsg'];\n return retObj;\n\n } // end validateUnitString\n\n\n /**\n * This method converts one unit to another\n *\n * @param fromUnitCode the unit code/expression/string of the unit to be converted\n * @param fromVal the number of \"from\" units to be converted to \"to\" units\n * @param toUnitCode the unit code/expression/string of the unit that the from\n * field is to be converted to\n * @param suggest a boolean to indicate whether or not suggestions are\n * requested for a string that cannot be resolved to a valid unit;\n * true indicates suggestions are wanted; false indicates they are not,\n * and is the default if the parameter is not specified;\n * @param molecularWeight the molecular weight of the substance in question\n * when a conversion is being requested from mass to moles and vice versa.\n * This is required when one of the units represents a value in moles. It is\n * ignored if neither unit includes a measurement in moles.\n * @returns a hash with six elements:\n * 'status' that will be: 'succeeded' if the conversion was successfully\n * calculated; 'failed' if the conversion could not be made, e.g., if\n * the units are not commensurable; or 'error' if an error occurred;\n * 'toVal' the numeric value indicating the conversion amount, or null\n * if the conversion failed (e.g., if the units are not commensurable);\n * 'msg' is an array message, if the string is invalid or an error occurred,\n * indicating the problem, or an explanation of a substitution such as\n * the substitution of 'G' for 'Gauss', or an empty array if no\n * messages were generated;\n * 'suggestions' if suggestions were requested and found, this is a hash\n * that contains at most two elements:\n * 'from' which, if the fromUnitCode input parameter or one or more of\n * its components could not be found, is an array one or more hash\n * objects. Each hash contains three elements:\n * 'msg' which is a message indicating what unit expression the\n * suggestions are for;\n * 'invalidUnit' which is the unit expression the suggestions\n * are for; and\n * 'units' which is an array of data for each suggested unit found.\n * Each array will contain the unit code, the unit name and the\n * unit guidance (if any).\n * If no suggestions were found for the fromUnitCode this element\n * will not be included.\n * 'to' which, if the \"to\" unit expression or one or more of its\n * components could not be found, is an array one or more hash objects. Each hash\n * contains three elements:\n * 'msg' which is a message indicating what toUnitCode input\n * parameter the suggestions are for;\n * 'invalidUnit' which is the unit expression the suggestions\n * are for; and\n * 'units' which is an array of data for each suggested unit found.\n * Each array will contain the unit code, the unit name and the\n * unit guidance (if any).\n * If no suggestions were found for the toUnitCode this element\n * will not be included.\n * No 'suggestions' element will be included in the returned hash\n * object if none were found, whether or not they were requested.\n * 'fromUnit' the unit object for the fromUnitCode passed in; returned\n * in case it's needed for additional data from the object; and\n * 'toUnit' the unit object for the toUnitCode passed in; returned\n * in case it's needed for additional data from the object.\n */\n convertUnitTo(fromUnitCode, fromVal, toUnitCode, suggest, molecularWeight) {\n if (suggest === undefined)\n suggest = false ;\n\n if (molecularWeight === undefined)\n molecularWeight = null ;\n\n let returnObj = {'status' : 'failed',\n 'toVal' : null,\n 'msg' : []} ;\n\n if (fromUnitCode) {\n fromUnitCode = fromUnitCode.trim();\n }\n if (!fromUnitCode || fromUnitCode == '') {\n returnObj['status'] = 'error';\n returnObj['msg'].push('No \"from\" unit expression specified.');\n }\n this._checkFromVal(fromVal, returnObj);\n if (toUnitCode) {\n toUnitCode = toUnitCode.trim();\n }\n if (!toUnitCode || toUnitCode == '') {\n returnObj['status'] = 'error';\n returnObj['msg'].push('No \"to\" unit expression specified.');\n }\n if (returnObj['status'] !== 'error') {\n try {\n let fromUnit = null;\n\n let parseResp = this.getSpecifiedUnit(fromUnitCode, 'convert', suggest);\n fromUnit = parseResp['unit'];\n if (parseResp['retMsg'])\n returnObj['msg'] = returnObj['msg'].concat(parseResp['retMsg']);\n if (parseResp['suggestions']) {\n returnObj['suggestions'] = {};\n returnObj['suggestions']['from'] = parseResp['suggestions'];\n }\n if (!fromUnit) {\n returnObj['msg'].push(`Unable to find a unit for ${fromUnitCode}, ` +\n `so no conversion could be performed.`);\n }\n\n let toUnit = null;\n parseResp = this.getSpecifiedUnit(toUnitCode, 'convert', suggest);\n toUnit = parseResp['unit'];\n if (parseResp['retMsg'])\n returnObj['msg'] = returnObj['msg'].concat(parseResp['retMsg']);\n if (parseResp['suggestions']) {\n if (!returnObj['suggestions'])\n returnObj['suggestions'] = {} ;\n returnObj['suggestions']['to'] = parseResp['suggestions'];\n }\n if (!toUnit) {\n returnObj['msg'].push(`Unable to find a unit for ${toUnitCode}, ` +\n `so no conversion could be performed.`);\n }\n\n if (fromUnit && toUnit) {\n try {\n // if no molecular weight was specified perform a normal conversion\n if (!molecularWeight) {\n returnObj['toVal'] = toUnit.convertFrom(fromVal, fromUnit);\n }\n else {\n if (fromUnit.moleExp_ !== 0 && toUnit.moleExp_ !== 0) {\n throw(new Error('A molecular weight was specified ' +\n 'but a mass <-> mole conversion cannot be executed for two ' +\n 'mole-based units. No conversion was attempted.'));\n }\n if (fromUnit.moleExp_ === 0 && toUnit.moleExp_ === 0) {\n throw(new Error('A molecular weight was specified ' +\n 'but a mass <-> mole conversion cannot be executed when ' +\n 'neither unit is mole-based. No conversion was attempted.'));\n }\n if (!fromUnit.isMoleMassCommensurable(toUnit)) {\n throw(new Error(`Sorry. ${fromUnitCode} cannot be ` +\n `converted to ${toUnitCode}.`));\n }\n\n // if the \"from\" unit is a mole-based unit, assume a mole to mass\n // request\n if (fromUnit.moleExp_ !== 0) {\n returnObj['toVal'] =\n fromUnit.convertMolToMass(fromVal, toUnit, molecularWeight);\n }\n // else the \"to\" unit must be the mole-based unit, so assume a\n // mass to mole request\n else {\n returnObj['toVal'] =\n fromUnit.convertMassToMol(fromVal, toUnit, molecularWeight);\n }\n } // end if a molecular weight was specified\n\n // if an error hasn't been thrown - either from convertFrom or here,\n // set the return object to show success\n returnObj['status'] = 'succeeded';\n returnObj['fromUnit'] = fromUnit;\n returnObj['toUnit'] = toUnit;\n }\n catch (err) {\n returnObj['status'] = 'failed';\n returnObj['msg'].push(err.message);\n }\n\n\n } // end if we have the from and to units\n }\n catch (err) {\n if (err.message == Ucum.needMoleWeightMsg_)\n returnObj['status'] = 'failed';\n else\n returnObj['status'] = 'error';\n returnObj['msg'].push(err.message);\n }\n }\n\n return returnObj ;\n\n } // end convertUnitTo\n\n\n /**\n * Converts the given unit string into its base units, their exponents, and\n * a magnitude, and returns that data.\n * @param fromUnit the unit string to be converted to base units information\n * @param fromVal the number of \"from\" units to be converted\n * @returns an object with the properties:\n * 'status' indicates whether the result succeeded. The value will be one of:\n * 'succeeded': the conversion was successfully calculated (which can be\n * true even if it was already in base units);\n * 'invalid': fromUnit is not a valid UCUM code;\n * 'failed': the conversion could not be made (e.g., if it is an \"arbitrary\" unit);\n * 'error': if an error occurred (an input or programming error)\n * 'msg': an array of one or more messages, if the string is invalid or\n * an error occurred, indicating the problem, or a suggestion of a\n * substitution such as the substitution of 'G' for 'Gauss', or\n * an empty array if no messages were generated. There can also be a\n * message that is just informational or warning.\n * 'magnitude': the new value when fromVal units of fromUnits is expressed in the base units.\n * 'fromUnitIsSpecial': whether the input unit fromUnit is a \"special unit\"\n * as defined in UCUM. This means there is some function applied to convert\n * between fromUnit and the base units, so the returned magnitude is likely not\n * useful as a scale factor for other conversions (i.e., it only has validity\n * and usefulness for the input values that produced it).\n * 'unitToExp': a map of base units in uStr to their exponent\n */\n convertToBaseUnits(fromUnit, fromVal) {\n let retObj = {};\n this._checkFromVal(fromVal, retObj);\n if (!retObj.status) { // could be set to 'error' by _checkFromVal\n let inputUnitLookup = this.getSpecifiedUnit(fromUnit, 'validate');\n retObj = {status: inputUnitLookup.status == 'valid' ? 'succeeded' : inputUnitLookup.status};\n let unit = inputUnitLookup.unit;\n retObj.msg = inputUnitLookup.retMsg || [];\n if (!unit) {\n if (inputUnitLookup.retMsg?.length == 0)\n retObj.msg.push('Could not find unit information for '+fromUnit);\n }\n else if (unit.isArbitrary_) {\n retObj.msg.push('Arbitrary units cannot be converted to base units or other units.');\n retObj.status = 'failed';\n }\n else if (retObj.status == 'succeeded') {\n let unitToExp = {};\n let dimVec = unit.dim_?.dimVec_\n let baseUnitString = '1';\n if (dimVec) {\n let dimVecIndexToBaseUnit = UnitTables.getInstance().dimVecIndexToBaseUnit_;\n for (let i=0, len=dimVec.length; i<len; ++i) {\n let exp = dimVec[i];\n if (exp) {\n unitToExp[dimVecIndexToBaseUnit[i]] = exp;\n baseUnitString += '.' + dimVecIndexToBaseUnit[i] + exp;\n }\n }\n }\n\n // The unit might have a conversion function, which has to be applied; we\n // cannot just assume unit_.magnitude_ is the magnitude in base units.\n let retUnitLookup = this.getSpecifiedUnit(baseUnitString, 'validate');\n // There should not be any error in retUnitLookup, unless there is a bug.\n let retUnit = retUnitLookup.unit;\n if (retUnitLookup.status !== 'valid') {\n retObj.msg.push('Unable construct base unit string; tried '+baseUnitString);\n retObj.status = 'error';\n }\n else {\n try {\n retObj.magnitude = retUnit.convertFrom(fromVal, unit);\n }\n catch (e) {\n retObj.msg.push(e.toString());\n retObj.status = 'error';\n }\n if (retObj.status == 'succeeded') {\n retObj.unitToExp = unitToExp;\n retObj.fromUnitIsSpecial = unit.isSpecial_;\n }\n }\n }\n }\n return retObj;\n }\n\n\n /**\n * Checks the given value as to whether it is suitable as a \"from\" value in a\n * unit conversion. If it is not, the responseObj will have its status set\n * to 'error' and a message added.\n * @param fromVal The value to check\n * @param responseObj the object that will be updated if the value is not\n * usable.\n */\n _checkFromVal(fromVal, responseObj) {\n if (fromVal === null || isNaN(fromVal) || (typeof fromVal !== 'number' &&\n !intUtils_.isNumericString(fromVal))) {\n responseObj.status = 'error';\n if (!responseObj.msg)\n responseObj.msg = [];\n responseObj.msg.push('No \"from\" value, or an invalid \"from\" value, ' +\n 'was specified.');\n }\n }\n\n\n /**\n * This method accepts a term and looks for units that include it as\n * a synonym - or that include the term in its name.\n *\n * @param theSyn the term to search for\n * @returns a hash with up to three elements:\n * 'status' contains the status of the request, which can be 'error',\n * 'failed' or succeeded';\n * 'msg' which contains a message for an error or if no units were found; and\n * 'units' which is an array that contains one hash for each unit found:\n * 'code' is the unit's csCode_\n * 'name' is the unit's name_\n * 'guidance' is the unit's guidance_\n *\n */\n checkSynonyms(theSyn) {\n let retObj = {} ;\n if (theSyn === undefined || theSyn === null) {\n retObj['status'] = 'error';\n retObj['msg'] = 'No term specified for synonym search.'\n }\n else {\n retObj = intUtils_.getSynonyms(theSyn);\n } // end if a search synonym was supplied\n\n return retObj ;\n\n } // end checkSynonyms\n\n\n /**\n * This method parses a unit string to get (or try to get) the unit\n * represented by the string. It returns an error message if no string was specified\n * or if any errors were encountered trying to get the unit.\n *\n * @param uName the expression/string representing the unit\n * @param valConv indicates what type of request this is for - a request to\n * validate (pass in 'validate') or a request to convert (pass in 'convert')\n * @param suggest a boolean to indicate whether or not suggestions are\n * requested for a string that cannot be resolved to a valid unit;\n * true indicates suggestions are wanted; false indicates they are not,\n * and is the default if the parameter is not specified;\n * @returns a hash containing:\n * 'status' will be 'valid' (uName is a valid UCUM code), 'invalid'\n * (the uStr is not a valid UCUM code, and substitutions or\n * suggestions may or may not be returned, depending on what was\n * requested and found); or 'error' (an input or programming error\n * occurred);\n * 'unit' the unit object (or null if there were problems creating the\n * unit);\n * 'origString' the possibly updated unit string passed in;\n * 'retMsg' an array of user messages (informational, error or warning) if\n * any were generated (IF any were generated, otherwise will be an\n * empty array); and\n * 'suggestions' is an array of 1 or more hash objects. Each hash\n * contains three elements:\n * 'msg' which is a message indicating what unit expression the\n * suggestions are for;\n * 'invalidUnit' which is the unit expression the suggestions are\n * for; and\n * 'units' which is an array of data for each suggested unit found.\n * Each array will contain the unit code, the unit name and the\n * unit guidance (if any).\n * The return hash will not contain a suggestions array if a valid unit\n * was found or if suggestions were not requested and found.\n */\n getSpecifiedUnit(uName, valConv, suggest) {\n\n if (suggest === undefined)\n suggest = false ;\n\n let retObj = {};\n retObj['retMsg'] = [];\n\n if (!uName) {\n retObj['retMsg'].push('No unit string specified.');\n }\n else {\n let utab = UnitTables.getInstance();\n uName = uName.trim();\n\n // go ahead and just try using the name as the code. This may or may not\n // work, but if it does, it cuts out a lot of parsing.\n let theUnit = utab.getUnitByCode(uName);\n\n // If we found it, set the returned unit string to what was passed in;\n // otherwise try parsing as a unit string\n if (theUnit) {\n retObj['unit'] = theUnit ;\n retObj['origString'] = uName;\n }\n else {\n try {\n let resp = this.uStrParser_.parseString(uName, valConv, suggest);\n retObj['unit'] = resp[0];\n retObj['origString'] = resp[1];\n if (resp[2])\n retObj['retMsg'] = resp[2];\n retObj['suggestions'] = resp[3];\n }\n catch (err) {\n console.log(`Unit requested for unit string ${uName}.` +\n 'request unsuccessful; error thrown = ' + err.message);\n retObj['retMsg'].unshift(`${uName} is not a valid unit. ` +\n `${err.message}`);\n }\n } // end if the unit was not found as a unit name\n } // end if a unit expression was specified\n\n // Set the status field\n if (!retObj.unit) {\n // No unit was found; check whether origString has a value\n retObj.status = !retObj.origString ? 'error' : 'invalid';\n }\n else {\n // Check whether substitutions were made to the unit string in order to\n // find the unit\n retObj.status = retObj.origString === uName ? 'valid': 'invalid';\n }\n\n return retObj;\n\n } // end getSpecifiedUnit\n\n\n /**\n * This method retrieves a list of units commensurable, i.e., that can be\n * converted from and to, a specified unit. Returns an error if the \"from\"\n * unit cannot be found.\n *\n * @param fromName the name/unit string of the \"from\" unit\n * @returns an array containing two elements;\n * first element is the list of commensurable units if any were found\n * second element is an error message if the \"from\" unit is not found\n */\n commensurablesList(fromName) {\n\n let retMsg = [];\n let commUnits = null ;\n let parseResp = this.getSpecifiedUnit(fromName, 'validate', false);\n let fromUnit = parseResp['unit'];\n if (parseResp['retMsg'].length > 0)\n retMsg = parseResp['retMsg'] ;\n if (!fromUnit) {\n retMsg.push(`Could not find unit ${fromName}.`);\n }\n else {\n let dimVec = null ;\n let fromDim = fromUnit.getProperty('dim_');\n if (!fromDim) {\n retMsg.push('No commensurable units were found for ' + fromName) ;\n }\n else {\n try {\n dimVec = fromDim.getProperty('dimVec_');\n }\n catch (err) {\n retMsg.push(err.message);\n if (err.message ===\n \"Dimension does not have requested property(dimVec_)\")\n dimVec = null;\n }\n if (dimVec) {\n let utab = UnitTables.getInstance();\n commUnits = utab.getUnitsByDimension(dimVec);\n }\n } // end if the from unit has a dimension vector\n } // end if we found a \"from\" unit\n return [commUnits , retMsg];\n } // end commensurablesList\n\n} // end UcumLhcUtils class\n\n\n/**\n * This function exists ONLY until the original UcumLhcUtils constructor\n * is called for the first time. It's defined here in case getInstance\n * is called before the constructor. This calls the constructor.\n *\n * The constructor redefines the getInstance function to return the\n * singleton UcumLhcUtils object. This is based on the UnitTables singleton\n * implementation; see more detail in the UnitTables constructor description.\n *\n * NO LONGER TRUE - not implemented as a singleton. This method retained to\n * avoid problems with calls to it that exist throughout the code.\n *\n * @return the (formerly singleton) UcumLhcUtils object.\n */\nUcumLhcUtils.getInstance = function(){\n return new UcumLhcUtils();\n} ;\n"],"file":"ucumLhcUtils.js"}