@lhncbc/ucum-lhc 4.2.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -12
- package/browser-dist/ucum-lhc.js +841 -1325
- package/data/ucumDefs.min.json +1 -1
- package/package.json +1 -1
- package/source/ucumLhcUtils.js +100 -66
- package/source/ucumXmlDocument.js +5 -2
- package/source/unit.js +2 -1
- package/source/unitString.js +159 -140
- package/source-cjs/config.js +1 -12
- package/source-cjs/config.js.map +1 -1
- package/source-cjs/dimension.js +20 -63
- package/source-cjs/dimension.js.map +1 -1
- package/source-cjs/jsonArrayPack.js +7 -25
- package/source-cjs/jsonArrayPack.js.map +1 -1
- package/source-cjs/prefix.js +12 -26
- package/source-cjs/prefix.js.map +1 -1
- package/source-cjs/prefixTables.js +10 -24
- package/source-cjs/prefixTables.js.map +1 -1
- package/source-cjs/ucumFunctions.js +35 -32
- package/source-cjs/ucumFunctions.js.map +1 -1
- package/source-cjs/ucumInternalUtils.js +5 -13
- package/source-cjs/ucumInternalUtils.js.map +1 -1
- package/source-cjs/ucumJsonDefs.js +1 -16
- package/source-cjs/ucumJsonDefs.js.map +1 -1
- package/source-cjs/ucumLhcUtils.js +117 -138
- package/source-cjs/ucumLhcUtils.js.map +1 -1
- package/source-cjs/ucumPkg.js +1 -6
- package/source-cjs/ucumPkg.js.map +1 -1
- package/source-cjs/ucumXmlDocument.js +162 -184
- package/source-cjs/ucumXmlDocument.js.map +1 -1
- package/source-cjs/unit.js +97 -181
- package/source-cjs/unit.js.map +1 -1
- package/source-cjs/unitString.js +537 -618
- package/source-cjs/unitString.js.map +1 -1
- package/source-cjs/unitTables.js +33 -139
- package/source-cjs/unitTables.js.map +1 -1
|
@@ -4,15 +4,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.UcumLhcUtils = void 0;
|
|
7
|
-
|
|
8
7
|
var _ucumJsonDefs = require("./ucumJsonDefs.js");
|
|
9
|
-
|
|
10
8
|
var intUtils_ = _interopRequireWildcard(require("./ucumInternalUtils.js"));
|
|
11
|
-
|
|
12
9
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
13
|
-
|
|
14
10
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
-
|
|
16
11
|
/**
|
|
17
12
|
* This class provides a single point of access to the LHC UCUM utilities
|
|
18
13
|
*
|
|
@@ -20,11 +15,8 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
|
|
|
20
15
|
*
|
|
21
16
|
*/
|
|
22
17
|
var Ucum = require('./config.js').Ucum;
|
|
23
|
-
|
|
24
18
|
var UnitTables = require('./unitTables.js').UnitTables;
|
|
25
|
-
|
|
26
19
|
var UnitString = require('./unitString.js').UnitString;
|
|
27
|
-
|
|
28
20
|
/**
|
|
29
21
|
* UCUM external utilities class
|
|
30
22
|
*/
|
|
@@ -38,10 +30,10 @@ class UcumLhcUtils {
|
|
|
38
30
|
if (UnitTables.getInstance().unitsCount() === 0) {
|
|
39
31
|
// Load the prefix and unit objects
|
|
40
32
|
_ucumJsonDefs.ucumJsonDefs.loadJsonDefs();
|
|
41
|
-
}
|
|
42
|
-
// of the LHC Utilities
|
|
43
|
-
|
|
33
|
+
}
|
|
44
34
|
|
|
35
|
+
// Get the UnitString parser that will be used with this instance
|
|
36
|
+
// of the LHC Utilities
|
|
45
37
|
this.uStrParser_ = UnitString.getInstance();
|
|
46
38
|
} // end constructor
|
|
47
39
|
|
|
@@ -53,12 +45,11 @@ class UcumLhcUtils {
|
|
|
53
45
|
* @param use flag indicating whether or not to use the braces message;
|
|
54
46
|
* defaults to true
|
|
55
47
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
48
|
useHTMLInMessages(use) {
|
|
59
49
|
if (use === undefined) use = true;
|
|
60
50
|
this.uStrParser_.useHTMLInMessages(use);
|
|
61
51
|
}
|
|
52
|
+
|
|
62
53
|
/**
|
|
63
54
|
* This method calls the useBraceMsgForEachString method on the UnitString
|
|
64
55
|
* object. It should be called by web applications where unit
|
|
@@ -68,12 +59,11 @@ class UcumLhcUtils {
|
|
|
68
59
|
* @param use flag indicating whether or not to use the braces message;
|
|
69
60
|
* defaults to true
|
|
70
61
|
*/
|
|
71
|
-
|
|
72
|
-
|
|
73
62
|
useBraceMsgForEachString(use) {
|
|
74
63
|
if (use === undefined) use = true;
|
|
75
64
|
this.uStrParser_.useBraceMsgForEachString(use);
|
|
76
65
|
}
|
|
66
|
+
|
|
77
67
|
/**
|
|
78
68
|
* This method validates a unit string. It first checks to see if the
|
|
79
69
|
* string passed in is a unit code that is found in the unit codes table.
|
|
@@ -127,36 +117,25 @@ class UcumLhcUtils {
|
|
|
127
117
|
* If no suggestions were requested and found, this property is not
|
|
128
118
|
* returned.
|
|
129
119
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
120
|
validateUnitString(uStr, suggest, valConv) {
|
|
133
121
|
if (suggest === undefined) suggest = false;
|
|
134
122
|
if (valConv === undefined) valConv = 'validate';
|
|
135
123
|
let resp = this.getSpecifiedUnit(uStr, valConv, suggest);
|
|
136
124
|
let theUnit = resp['unit'];
|
|
137
|
-
let retObj = {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
'
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
'unit': {
|
|
149
|
-
'code': theUnit.csCode_,
|
|
150
|
-
'name': theUnit.name_,
|
|
151
|
-
'guidance': theUnit.guidance_
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
125
|
+
let retObj = !theUnit ? {
|
|
126
|
+
'ucumCode': null
|
|
127
|
+
} : {
|
|
128
|
+
'ucumCode': resp['origString'],
|
|
129
|
+
'unit': {
|
|
130
|
+
'code': theUnit.csCode_,
|
|
131
|
+
'name': theUnit.name_,
|
|
132
|
+
'guidance': theUnit.guidance_
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
retObj.status = resp.status;
|
|
156
136
|
if (resp['suggestions']) {
|
|
157
137
|
retObj['suggestions'] = resp['suggestions'];
|
|
158
138
|
}
|
|
159
|
-
|
|
160
139
|
retObj['msg'] = resp['retMsg'];
|
|
161
140
|
return retObj;
|
|
162
141
|
} // end validateUnitString
|
|
@@ -219,8 +198,6 @@ class UcumLhcUtils {
|
|
|
219
198
|
* 'toUnit' the unit object for the toUnitCode passed in; returned
|
|
220
199
|
* in case it's needed for additional data from the object.
|
|
221
200
|
*/
|
|
222
|
-
|
|
223
|
-
|
|
224
201
|
convertUnitTo(fromUnitCode, fromVal, toUnitCode, suggest, molecularWeight) {
|
|
225
202
|
if (suggest === undefined) suggest = false;
|
|
226
203
|
if (molecularWeight === undefined) molecularWeight = null;
|
|
@@ -229,60 +206,45 @@ class UcumLhcUtils {
|
|
|
229
206
|
'toVal': null,
|
|
230
207
|
'msg': []
|
|
231
208
|
};
|
|
232
|
-
|
|
233
209
|
if (fromUnitCode) {
|
|
234
210
|
fromUnitCode = fromUnitCode.trim();
|
|
235
211
|
}
|
|
236
|
-
|
|
237
212
|
if (!fromUnitCode || fromUnitCode == '') {
|
|
238
213
|
returnObj['status'] = 'error';
|
|
239
214
|
returnObj['msg'].push('No "from" unit expression specified.');
|
|
240
215
|
}
|
|
241
|
-
|
|
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
|
-
}
|
|
246
|
-
|
|
216
|
+
this._checkFromVal(fromVal, returnObj);
|
|
247
217
|
if (toUnitCode) {
|
|
248
218
|
toUnitCode = toUnitCode.trim();
|
|
249
219
|
}
|
|
250
|
-
|
|
251
220
|
if (!toUnitCode || toUnitCode == '') {
|
|
252
221
|
returnObj['status'] = 'error';
|
|
253
222
|
returnObj['msg'].push('No "to" unit expression specified.');
|
|
254
223
|
}
|
|
255
|
-
|
|
256
224
|
if (returnObj['status'] !== 'error') {
|
|
257
225
|
try {
|
|
258
226
|
let fromUnit = null;
|
|
259
227
|
let parseResp = this.getSpecifiedUnit(fromUnitCode, 'convert', suggest);
|
|
260
228
|
fromUnit = parseResp['unit'];
|
|
261
229
|
if (parseResp['retMsg']) returnObj['msg'] = returnObj['msg'].concat(parseResp['retMsg']);
|
|
262
|
-
|
|
263
230
|
if (parseResp['suggestions']) {
|
|
264
231
|
returnObj['suggestions'] = {};
|
|
265
232
|
returnObj['suggestions']['from'] = parseResp['suggestions'];
|
|
266
233
|
}
|
|
267
|
-
|
|
268
234
|
if (!fromUnit) {
|
|
269
235
|
returnObj['msg'].push(`Unable to find a unit for ${fromUnitCode}, ` + `so no conversion could be performed.`);
|
|
270
236
|
}
|
|
271
|
-
|
|
272
237
|
let toUnit = null;
|
|
273
238
|
parseResp = this.getSpecifiedUnit(toUnitCode, 'convert', suggest);
|
|
274
239
|
toUnit = parseResp['unit'];
|
|
275
240
|
if (parseResp['retMsg']) returnObj['msg'] = returnObj['msg'].concat(parseResp['retMsg']);
|
|
276
|
-
|
|
277
241
|
if (parseResp['suggestions']) {
|
|
278
242
|
if (!returnObj['suggestions']) returnObj['suggestions'] = {};
|
|
279
243
|
returnObj['suggestions']['to'] = parseResp['suggestions'];
|
|
280
244
|
}
|
|
281
|
-
|
|
282
245
|
if (!toUnit) {
|
|
283
246
|
returnObj['msg'].push(`Unable to find a unit for ${toUnitCode}, ` + `so no conversion could be performed.`);
|
|
284
247
|
}
|
|
285
|
-
|
|
286
248
|
if (fromUnit && toUnit) {
|
|
287
249
|
try {
|
|
288
250
|
// if no molecular weight was specified perform a normal conversion
|
|
@@ -292,29 +254,27 @@ class UcumLhcUtils {
|
|
|
292
254
|
if (fromUnit.moleExp_ !== 0 && toUnit.moleExp_ !== 0) {
|
|
293
255
|
throw new Error('A molecular weight was specified ' + 'but a mass <-> mole conversion cannot be executed for two ' + 'mole-based units. No conversion was attempted.');
|
|
294
256
|
}
|
|
295
|
-
|
|
296
257
|
if (fromUnit.moleExp_ === 0 && toUnit.moleExp_ === 0) {
|
|
297
258
|
throw new Error('A molecular weight was specified ' + 'but a mass <-> mole conversion cannot be executed when ' + 'neither unit is mole-based. No conversion was attempted.');
|
|
298
259
|
}
|
|
299
|
-
|
|
300
260
|
if (!fromUnit.isMoleMassCommensurable(toUnit)) {
|
|
301
261
|
throw new Error(`Sorry. ${fromUnitCode} cannot be ` + `converted to ${toUnitCode}.`);
|
|
302
|
-
}
|
|
303
|
-
// request
|
|
304
|
-
|
|
262
|
+
}
|
|
305
263
|
|
|
264
|
+
// if the "from" unit is a mole-based unit, assume a mole to mass
|
|
265
|
+
// request
|
|
306
266
|
if (fromUnit.moleExp_ !== 0) {
|
|
307
267
|
returnObj['toVal'] = fromUnit.convertMolToMass(fromVal, toUnit, molecularWeight);
|
|
308
|
-
}
|
|
268
|
+
}
|
|
269
|
+
// else the "to" unit must be the mole-based unit, so assume a
|
|
309
270
|
// mass to mole request
|
|
310
271
|
else {
|
|
311
|
-
|
|
312
|
-
|
|
272
|
+
returnObj['toVal'] = fromUnit.convertMassToMol(fromVal, toUnit, molecularWeight);
|
|
273
|
+
}
|
|
313
274
|
} // end if a molecular weight was specified
|
|
275
|
+
|
|
314
276
|
// if an error hasn't been thrown - either from convertFrom or here,
|
|
315
277
|
// set the return object to show success
|
|
316
|
-
|
|
317
|
-
|
|
318
278
|
returnObj['status'] = 'succeeded';
|
|
319
279
|
returnObj['fromUnit'] = fromUnit;
|
|
320
280
|
returnObj['toUnit'] = toUnit;
|
|
@@ -323,13 +283,11 @@ class UcumLhcUtils {
|
|
|
323
283
|
returnObj['msg'].push(err.message);
|
|
324
284
|
}
|
|
325
285
|
} // end if we have the from and to units
|
|
326
|
-
|
|
327
286
|
} catch (err) {
|
|
328
287
|
if (err.message == Ucum.needMoleWeightMsg_) returnObj['status'] = 'failed';else returnObj['status'] = 'error';
|
|
329
288
|
returnObj['msg'].push(err.message);
|
|
330
289
|
}
|
|
331
290
|
}
|
|
332
|
-
|
|
333
291
|
return returnObj;
|
|
334
292
|
} // end convertUnitTo
|
|
335
293
|
|
|
@@ -339,70 +297,97 @@ class UcumLhcUtils {
|
|
|
339
297
|
* @param fromUnit the unit string to be converted to base units information
|
|
340
298
|
* @param fromVal the number of "from" units to be converted
|
|
341
299
|
* @returns an object with the properties:
|
|
342
|
-
* '
|
|
300
|
+
* 'status' indicates whether the result succeeded. The value will be one of:
|
|
301
|
+
* 'succeeded': the conversion was successfully calculated (which can be
|
|
302
|
+
* true even if it was already in base units);
|
|
303
|
+
* 'invalid': fromUnit is not a valid UCUM code;
|
|
304
|
+
* 'failed': the conversion could not be made (e.g., if it is an "arbitrary" unit);
|
|
305
|
+
* 'error': if an error occurred (an input or programming error)
|
|
306
|
+
* 'msg': an array of messages (possibly empty) if the string is invalid or
|
|
343
307
|
* an error occurred, indicating the problem, or a suggestion of a
|
|
344
308
|
* substitution such as the substitution of 'G' for 'Gauss', or
|
|
345
|
-
* an empty array if no messages were generated.
|
|
346
|
-
*
|
|
309
|
+
* an empty array if no messages were generated. There can also be a
|
|
310
|
+
* message that is just informational or warning.
|
|
347
311
|
* 'magnitude': the new value when fromVal units of fromUnits is expressed in the base units.
|
|
348
312
|
* 'fromUnitIsSpecial': whether the input unit fromUnit is a "special unit"
|
|
349
313
|
* as defined in UCUM. This means there is some function applied to convert
|
|
350
314
|
* between fromUnit and the base units, so the returned magnitude is likely not
|
|
351
315
|
* useful as a scale factor for other conversions (i.e., it only has validity
|
|
352
316
|
* and usefulness for the input values that produced it).
|
|
353
|
-
* 'unitToExp': a map of base units in
|
|
317
|
+
* 'unitToExp': a map of base units in fromUnit to their exponent
|
|
354
318
|
*/
|
|
355
|
-
|
|
356
|
-
|
|
357
319
|
convertToBaseUnits(fromUnit, fromVal) {
|
|
358
|
-
let inputUnitLookup = this.getSpecifiedUnit(fromUnit, 'validate');
|
|
359
320
|
let retObj = {};
|
|
360
|
-
|
|
361
|
-
retObj.
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
321
|
+
this._checkFromVal(fromVal, retObj);
|
|
322
|
+
if (!retObj.status) {
|
|
323
|
+
// could be set to 'error' by _checkFromVal
|
|
324
|
+
let inputUnitLookup = this.getSpecifiedUnit(fromUnit, 'validate');
|
|
325
|
+
retObj = {
|
|
326
|
+
status: inputUnitLookup.status == 'valid' ? 'succeeded' : inputUnitLookup.status
|
|
327
|
+
};
|
|
328
|
+
let unit = inputUnitLookup.unit;
|
|
329
|
+
retObj.msg = inputUnitLookup.retMsg || [];
|
|
330
|
+
if (!unit) {
|
|
331
|
+
if (inputUnitLookup.retMsg?.length == 0) retObj.msg.push('Could not find unit information for ' + fromUnit);
|
|
332
|
+
} else if (unit.isArbitrary_) {
|
|
333
|
+
retObj.msg.push('Arbitrary units cannot be converted to base units or other units.');
|
|
334
|
+
retObj.status = 'failed';
|
|
335
|
+
} else if (retObj.status == 'succeeded') {
|
|
336
|
+
let unitToExp = {};
|
|
337
|
+
let dimVec = unit.dim_?.dimVec_;
|
|
338
|
+
let baseUnitString = '1';
|
|
339
|
+
if (dimVec) {
|
|
340
|
+
let dimVecIndexToBaseUnit = UnitTables.getInstance().dimVecIndexToBaseUnit_;
|
|
341
|
+
for (let i = 0, len = dimVec.length; i < len; ++i) {
|
|
342
|
+
let exp = dimVec[i];
|
|
343
|
+
if (exp) {
|
|
344
|
+
unitToExp[dimVecIndexToBaseUnit[i]] = exp;
|
|
345
|
+
baseUnitString += '.' + dimVecIndexToBaseUnit[i] + exp;
|
|
346
|
+
}
|
|
381
347
|
}
|
|
382
348
|
}
|
|
383
|
-
} // The unit might have a conversion function, which has to be applied; we
|
|
384
|
-
// cannot just assume unit_.magnitude_ is the magnitude in base units.
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
let retUnitLookup = this.getSpecifiedUnit(baseUnitString, 'validate'); // There should not be any error in retUnitLookup, unless there is a bug.
|
|
388
|
-
|
|
389
|
-
let retUnit = retUnitLookup.unit;
|
|
390
|
-
if (!retUnit && retUnitLookup.retMsg?.length == 0) retObj.msg.push('Unable construct base unit string; tried ' + baseUnitString);else {
|
|
391
|
-
try {
|
|
392
|
-
retObj.magnitude = retUnit.convertFrom(fromVal, unit);
|
|
393
|
-
} catch (e) {
|
|
394
|
-
retObj.msg.push(e.toString());
|
|
395
|
-
}
|
|
396
349
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
350
|
+
// The unit might have a conversion function, which has to be applied; we
|
|
351
|
+
// cannot just assume unit_.magnitude_ is the magnitude in base units.
|
|
352
|
+
let retUnitLookup = this.getSpecifiedUnit(baseUnitString, 'validate');
|
|
353
|
+
// There should not be any error in retUnitLookup, unless there is a bug.
|
|
354
|
+
let retUnit = retUnitLookup.unit;
|
|
355
|
+
if (retUnitLookup.status !== 'valid') {
|
|
356
|
+
retObj.msg.push('Unable construct base unit string; tried ' + baseUnitString);
|
|
357
|
+
retObj.status = 'error';
|
|
358
|
+
} else {
|
|
359
|
+
try {
|
|
360
|
+
retObj.magnitude = retUnit.convertFrom(fromVal, unit);
|
|
361
|
+
} catch (e) {
|
|
362
|
+
retObj.msg.push(e.toString());
|
|
363
|
+
retObj.status = 'error';
|
|
364
|
+
}
|
|
365
|
+
if (retObj.status == 'succeeded') {
|
|
366
|
+
retObj.unitToExp = unitToExp;
|
|
367
|
+
retObj.fromUnitIsSpecial = unit.isSpecial_;
|
|
368
|
+
}
|
|
400
369
|
}
|
|
401
370
|
}
|
|
402
371
|
}
|
|
403
|
-
|
|
404
372
|
return retObj;
|
|
405
373
|
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Checks the given value as to whether it is suitable as a "from" value in a
|
|
377
|
+
* unit conversion. If it is not, the responseObj will have its status set
|
|
378
|
+
* to 'error' and a message added.
|
|
379
|
+
* @param fromVal The value to check
|
|
380
|
+
* @param responseObj the object that will be updated if the value is not
|
|
381
|
+
* usable.
|
|
382
|
+
*/
|
|
383
|
+
_checkFromVal(fromVal, responseObj) {
|
|
384
|
+
if (fromVal === null || isNaN(fromVal) || typeof fromVal !== 'number' && !intUtils_.isNumericString(fromVal)) {
|
|
385
|
+
responseObj.status = 'error';
|
|
386
|
+
if (!responseObj.msg) responseObj.msg = [];
|
|
387
|
+
responseObj.msg.push('No "from" value, or an invalid "from" value, ' + 'was specified.');
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
406
391
|
/**
|
|
407
392
|
* This method accepts a term and looks for units that include it as
|
|
408
393
|
* a synonym - or that include the term in its name.
|
|
@@ -418,11 +403,8 @@ class UcumLhcUtils {
|
|
|
418
403
|
* 'guidance' is the unit's guidance_
|
|
419
404
|
*
|
|
420
405
|
*/
|
|
421
|
-
|
|
422
|
-
|
|
423
406
|
checkSynonyms(theSyn) {
|
|
424
407
|
let retObj = {};
|
|
425
|
-
|
|
426
408
|
if (theSyn === undefined || theSyn === null) {
|
|
427
409
|
retObj['status'] = 'error';
|
|
428
410
|
retObj['msg'] = 'No term specified for synonym search.';
|
|
@@ -430,7 +412,6 @@ class UcumLhcUtils {
|
|
|
430
412
|
retObj = intUtils_.getSynonyms(theSyn);
|
|
431
413
|
} // end if a search synonym was supplied
|
|
432
414
|
|
|
433
|
-
|
|
434
415
|
return retObj;
|
|
435
416
|
} // end checkSynonyms
|
|
436
417
|
|
|
@@ -447,13 +428,18 @@ class UcumLhcUtils {
|
|
|
447
428
|
* true indicates suggestions are wanted; false indicates they are not,
|
|
448
429
|
* and is the default if the parameter is not specified;
|
|
449
430
|
* @returns a hash containing:
|
|
431
|
+
* 'status' will be 'valid' (uName is a valid UCUM code), 'invalid'
|
|
432
|
+
* (the uStr is not a valid UCUM code, and substitutions or
|
|
433
|
+
* suggestions may or may not be returned, depending on what was
|
|
434
|
+
* requested and found); or 'error' (an input or programming error
|
|
435
|
+
* occurred);
|
|
450
436
|
* 'unit' the unit object (or null if there were problems creating the
|
|
451
437
|
* unit);
|
|
452
438
|
* 'origString' the possibly updated unit string passed in;
|
|
453
439
|
* 'retMsg' an array of user messages (informational, error or warning) if
|
|
454
440
|
* any were generated (IF any were generated, otherwise will be an
|
|
455
441
|
* empty array); and
|
|
456
|
-
*
|
|
442
|
+
* 'suggestions' is an array of 1 or more hash objects. Each hash
|
|
457
443
|
* contains three elements:
|
|
458
444
|
* 'msg' which is a message indicating what unit expression the
|
|
459
445
|
* suggestions are for;
|
|
@@ -465,23 +451,22 @@ class UcumLhcUtils {
|
|
|
465
451
|
* The return hash will not contain a suggestions array if a valid unit
|
|
466
452
|
* was found or if suggestions were not requested and found.
|
|
467
453
|
*/
|
|
468
|
-
|
|
469
|
-
|
|
470
454
|
getSpecifiedUnit(uName, valConv, suggest) {
|
|
471
455
|
if (suggest === undefined) suggest = false;
|
|
472
456
|
let retObj = {};
|
|
473
457
|
retObj['retMsg'] = [];
|
|
474
|
-
|
|
475
458
|
if (!uName) {
|
|
476
459
|
retObj['retMsg'].push('No unit string specified.');
|
|
477
460
|
} else {
|
|
478
461
|
let utab = UnitTables.getInstance();
|
|
479
|
-
uName = uName.trim();
|
|
462
|
+
uName = uName.trim();
|
|
463
|
+
|
|
464
|
+
// go ahead and just try using the name as the code. This may or may not
|
|
480
465
|
// work, but if it does, it cuts out a lot of parsing.
|
|
466
|
+
let theUnit = utab.getUnitByCode(uName);
|
|
481
467
|
|
|
482
|
-
|
|
468
|
+
// If we found it, set the returned unit string to what was passed in;
|
|
483
469
|
// otherwise try parsing as a unit string
|
|
484
|
-
|
|
485
470
|
if (theUnit) {
|
|
486
471
|
retObj['unit'] = theUnit;
|
|
487
472
|
retObj['origString'] = uName;
|
|
@@ -497,10 +482,17 @@ class UcumLhcUtils {
|
|
|
497
482
|
retObj['retMsg'].unshift(`${uName} is not a valid unit. ` + `${err.message}`);
|
|
498
483
|
}
|
|
499
484
|
} // end if the unit was not found as a unit name
|
|
500
|
-
|
|
501
485
|
} // end if a unit expression was specified
|
|
502
486
|
|
|
503
|
-
|
|
487
|
+
// Set the status field
|
|
488
|
+
if (!retObj.unit) {
|
|
489
|
+
// No unit was found; check whether origString has a value
|
|
490
|
+
retObj.status = !retObj.origString ? 'error' : 'invalid';
|
|
491
|
+
} else {
|
|
492
|
+
// Check whether substitutions were made to the unit string in order to
|
|
493
|
+
// find the unit
|
|
494
|
+
retObj.status = retObj.origString === uName ? 'valid' : 'invalid';
|
|
495
|
+
}
|
|
504
496
|
return retObj;
|
|
505
497
|
} // end getSpecifiedUnit
|
|
506
498
|
|
|
@@ -514,21 +506,17 @@ class UcumLhcUtils {
|
|
|
514
506
|
* first element is the list of commensurable units if any were found
|
|
515
507
|
* second element is an error message if the "from" unit is not found
|
|
516
508
|
*/
|
|
517
|
-
|
|
518
|
-
|
|
519
509
|
commensurablesList(fromName) {
|
|
520
510
|
let retMsg = [];
|
|
521
511
|
let commUnits = null;
|
|
522
512
|
let parseResp = this.getSpecifiedUnit(fromName, 'validate', false);
|
|
523
513
|
let fromUnit = parseResp['unit'];
|
|
524
514
|
if (parseResp['retMsg'].length > 0) retMsg = parseResp['retMsg'];
|
|
525
|
-
|
|
526
515
|
if (!fromUnit) {
|
|
527
516
|
retMsg.push(`Could not find unit ${fromName}.`);
|
|
528
517
|
} else {
|
|
529
518
|
let dimVec = null;
|
|
530
519
|
let fromDim = fromUnit.getProperty('dim_');
|
|
531
|
-
|
|
532
520
|
if (!fromDim) {
|
|
533
521
|
retMsg.push('No commensurable units were found for ' + fromName);
|
|
534
522
|
} else {
|
|
@@ -538,20 +526,14 @@ class UcumLhcUtils {
|
|
|
538
526
|
retMsg.push(err.message);
|
|
539
527
|
if (err.message === "Dimension does not have requested property(dimVec_)") dimVec = null;
|
|
540
528
|
}
|
|
541
|
-
|
|
542
529
|
if (dimVec) {
|
|
543
530
|
let utab = UnitTables.getInstance();
|
|
544
531
|
commUnits = utab.getUnitsByDimension(dimVec);
|
|
545
532
|
}
|
|
546
533
|
} // end if the from unit has a dimension vector
|
|
547
|
-
|
|
548
534
|
} // end if we found a "from" unit
|
|
549
|
-
|
|
550
|
-
|
|
551
535
|
return [commUnits, retMsg];
|
|
552
536
|
} // end commensurablesList
|
|
553
|
-
|
|
554
|
-
|
|
555
537
|
} // end UcumLhcUtils class
|
|
556
538
|
|
|
557
539
|
/**
|
|
@@ -568,10 +550,7 @@ class UcumLhcUtils {
|
|
|
568
550
|
*
|
|
569
551
|
* @return the (formerly singleton) UcumLhcUtils object.
|
|
570
552
|
*/
|
|
571
|
-
|
|
572
|
-
|
|
573
553
|
exports.UcumLhcUtils = UcumLhcUtils;
|
|
574
|
-
|
|
575
554
|
UcumLhcUtils.getInstance = function () {
|
|
576
555
|
return new UcumLhcUtils();
|
|
577
556
|
};
|