@lhncbc/ucum-lhc 4.1.6 → 4.1.8

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.
@@ -11,6 +11,8 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
11
11
 
12
12
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
13
 
14
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
+
14
16
  /**
15
17
  * This class handles the parsing of a unit string into a unit object
16
18
  */
@@ -64,6 +66,8 @@ class UnitString {
64
66
 
65
67
  this.suggestions = [];
66
68
  } // end constructor
69
+ // The start of an error message about an invalid annotation character.
70
+
67
71
 
68
72
  /**
69
73
  * Sets the emphasis strings to the HTML used in the webpage display - or
@@ -72,8 +76,6 @@ class UnitString {
72
76
  * @param use flag indicating whether or not to use the html message format;
73
77
  * defaults to true
74
78
  */
75
-
76
-
77
79
  useHTMLInMessages(use) {
78
80
  if (use === undefined || use) {
79
81
  this.openEmph_ = Ucum.openEmphHTML_;
@@ -194,7 +196,7 @@ class UnitString {
194
196
  } // end if blanks were found in the string
195
197
  // assign the array returned to retObj. It will contain 2 elements:
196
198
  // the unit returned in position 0; and the origString (possibly
197
- // modified in position 1. The origString in position 1 will not
199
+ // modified) in position 1. The origString in position 1 will not
198
200
  // be changed by subsequent processing.
199
201
 
200
202
 
@@ -369,18 +371,28 @@ class UnitString {
369
371
  this.retMsg_.push('Missing closing brace for annotation starting at ' + this.openEmph_ + uString.substr(openBrace) + this.closeEmph_);
370
372
  openBrace = -1;
371
373
  } else {
372
- let braceStr = uString.substring(openBrace, closeBrace + 1);
373
- let aIdx = this.annotations_.length.toString();
374
- uString = uString.replace(braceStr, this.braceFlag_ + aIdx + this.braceFlag_);
375
- this.annotations_.push(braceStr);
376
- openBrace = uString.indexOf('{');
374
+ let braceStr = uString.substring(openBrace, closeBrace + 1); // Check for valid characters in the annotation.
375
+
376
+ if (!UnitString.VALID_ANNOTATION_REGEX.test(braceStr)) {
377
+ this.retMsg_.push(UnitString.INVALID_ANNOTATION_CHAR_MSG + this.openEmph_ + braceStr + this.closeEmph_);
378
+ openBrace = -1; // end search for annotations
379
+ } else {
380
+ let aIdx = this.annotations_.length.toString();
381
+ uString = uString.replace(braceStr, this.braceFlag_ + aIdx + this.braceFlag_);
382
+ this.annotations_.push(braceStr);
383
+ openBrace = uString.indexOf('{');
384
+ }
377
385
  }
378
386
  } // end do while we have an opening brace
379
387
  // check for a stray/unmatched closing brace
380
388
 
381
389
 
382
- let closeBrace = uString.indexOf('}');
383
- if (closeBrace >= 0) this.retMsg_.push('Missing opening brace for closing brace found at ' + this.openEmph_ + uString.substring(0, closeBrace + 1) + this.closeEmph_);
390
+ if (this.retMsg_.length == 0) {
391
+ // if there were no other errors above
392
+ let closeBrace = uString.indexOf('}');
393
+ if (closeBrace >= 0) this.retMsg_.push('Missing opening brace for closing brace found at ' + this.openEmph_ + uString.substring(0, closeBrace + 1) + this.closeEmph_);
394
+ }
395
+
384
396
  return uString;
385
397
  } // end _getAnnotations
386
398
 
@@ -1142,7 +1154,14 @@ class UnitString {
1142
1154
  uCode = uCode.substr(1); // try one more time for the unit
1143
1155
 
1144
1156
  origUnit = this.utabs_.getUnitByCode(uCode);
1145
- }
1157
+ } // Reject the unit we found if it might have another prefix.
1158
+ // Such things are in our tables through the LOINC source_
1159
+ // (ucum.csv) which has guidance and synonyms. I think it should be
1160
+ // safe to exclude anything whose source is LOINC from having a
1161
+ // prefix.
1162
+
1163
+
1164
+ if (origUnit && origUnit.source_ == 'LOINC') origUnit = null;
1146
1165
  } // end if we found a prefix
1147
1166
 
1148
1167
  } // end if we didn't get a unit after removing an exponent
@@ -1163,8 +1182,11 @@ class UnitString {
1163
1182
  } else {
1164
1183
  // Otherwise we found a unit object. Clone it and then apply the
1165
1184
  // prefix and exponent, if any, to it. And remove the guidance.
1166
- retUnit = origUnit.clone();
1167
- retUnit.guidance_ = '';
1185
+ retUnit = origUnit.clone(); // If we are here, this is only part of the full unit string, so it is
1186
+ // not a base unit, and the synonyms will mostly likely not be correct for the full
1187
+ // string.
1188
+
1189
+ retUnit.resetFieldsForDerivedUnit();
1168
1190
  let theDim = retUnit.getProperty('dim_');
1169
1191
  let theMag = retUnit.getProperty('magnitude_');
1170
1192
  let theName = retUnit.getProperty('name_');
@@ -1499,6 +1521,11 @@ class UnitString {
1499
1521
 
1500
1522
  exports.UnitString = UnitString;
1501
1523
 
1524
+ _defineProperty(UnitString, "INVALID_ANNOTATION_CHAR_MSG", 'An invalid character was found in the annotation ');
1525
+
1526
+ // A regular expression for validating annotation strings.
1527
+ _defineProperty(UnitString, "VALID_ANNOTATION_REGEX", /^\{[!-z|~]*\}$/);
1528
+
1502
1529
  UnitString.getInstance = function () {
1503
1530
  return new UnitString();
1504
1531
  };