@lhncbc/ucum-lhc 7.1.6 → 7.1.9

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 CHANGED
@@ -1,75 +1,66 @@
1
1
  # ucum-lhc
2
- This is the LHC implementation of validation and conversion services based on
2
+ This package provides APIs for validation and conversion of unit strings in
3
3
  the [Unified Code for Units of Measure](https://ucum.org) (UCUM) code
4
- system created by the Regenstrief Institute, Inc.
4
+ system created by the Regenstrief Institute, Inc. ucum-lhc was created by the
5
+ Lister Hill Center (LHC) at the U.S. National Library of Medicine.
5
6
 
6
7
  See our [overview page](https://ucum.nlm.nih.gov/ucum-lhc) for
7
8
  general information.
8
9
 
9
- This is a work in progress so more capabilities will probably be introduced.
10
+ In addition to using this package from a JavaScript program, there is also a [web
11
+ API service](https://ucum.nlm.nih.gov/ucum-service.html) which uses this library, so
12
+ you can send HTTPS requests to validate and convert units if that is more
13
+ convenient.
10
14
 
11
15
  ## Check out the Demo page
12
16
 
13
- We have a [demo page](https://ucum.nlm.nih.gov/ucum-lhc/demo.html) that
17
+ We have a [demo page](https://ucum.nlm.nih.gov/ucum-lhc/demo.html) that
14
18
  shows various capabilities. That includes the validation and conversion
15
19
  functions described below. You might want to try that out first.
16
20
 
17
21
  ## Get the code
18
22
 
19
- The ucum-lhc code is written as ES6 modules, but the npm package (see below)
20
- also contains CommonJS modules, as well as a "browser-dist" directory with files
21
- ready to use in a web browser.
22
-
23
- Currently we have code to serve multiple purposes. The core code supports
24
- the validation and conversion of UCUM unit expressions as well as a function
25
- to search for commensurable units for a specified unit expression. Other code is
26
- concerned with importing and exporting the UCUM data, and in supporting the
27
- demo page (noted above). If you are looking to include the ucum-lhc core code
28
- in your application, download the code as an [npm](https://www.npmjs.com) package.
29
-
30
- ### Getting the code as an npm package
31
-
32
- You can use the [npm](https://www.npmjs.com) package manager
33
- to install the ucum-lhc npm package. (npm is
34
- [automatically installed](https://www.npmjs.com/get-npm) with Node.js.)
23
+ You can use the [npm](https://www.npmjs.com) package manager
24
+ to install the ucum-lhc npm package. (npm is automatically installed with Node.js.)
35
25
 
36
26
  npm install @lhncbc/ucum-lhc --save
37
27
 
38
- This will install the @lhncbc/ucum-lhc directory in your node_modules diretory.
39
- The browser-dist subdirectory will contain ucum-lhc.min.js for use directly in a
40
- browser.
28
+ This will install the @lhncbc/ucum-lhc directory in your node_modules directory.
29
+ If you are not building the code along with an app, you can use the
30
+ browser-dist/ucum-lhc.min.js file which is already built for use directly in a
31
+ browser without building.
41
32
 
42
- ## Using the code
33
+ The ucum-lhc code is written as ES6 modules, but the npm package
34
+ also contains CommonJS modules, which are used by default if you "require" or
35
+ "import" the package.
43
36
 
44
- The ucum-lhc.min.js file includes the source code you need for the validation,
45
- conversion and commensurable units functions as well as the ucum code
46
- definitions file. We assume that your main motivation for including the
47
- ucum-lhc code is to have those capabilities for units of measure on your system.
37
+
38
+ ## Using the code
48
39
 
49
40
  ### Server side
50
- To access those capabilities from your server side code (or from client side
51
- code that goes through a build system), require the npm package
52
- and create a UcumLhcUtils object that contains those functions.
41
+ Require (or import) the npm package and create a UcumLhcUtils object that
42
+ contains those functions.
53
43
 
54
44
  const ucum = require('@lhncbc/ucum-lhc');
55
45
  const utils = ucum.UcumLhcUtils.getInstance();
56
46
 
57
-
47
+ See the API reference section below for the APIs on the utils object.
48
+
58
49
  ### Client side
59
50
 
60
- To access those capabilities from your client side code, include the
61
- ucum-lhc.min.js package in your html file.
51
+ To access those capabilities from your client side code without building, include the
52
+ pre-built ucum-lhc.min.js file in your HTML file.
62
53
 
63
54
  <script src="path-to-installed-package/browser-dist/ucum-lhc.min.js"></script>
64
55
 
65
- The validation, conversion and commensurable units functions are available from
66
- the _ucumPkg.UcumLhcUtils_ class. In your client side javascript code access
67
- those functions via the ucumPkg object. For example,
56
+ The validation, conversion and commensurable units functions are available from
57
+ the _ucumPkg.UcumLhcUtils_ class. In your client side JavaScript code access
58
+ those functions via the ucumPkg object. For example,
68
59
 
69
60
  var parseResp = ucumPkg.UcumLhcUtils.getInstance().validateUnitString(uStr, true);
70
-
71
- ### Function descriptions
72
-
61
+
62
+ ### API reference
63
+
73
64
  Below is documentation for the public functions on the UcumLhcUtils instance.
74
65
  * [validateUnitString](#validateunitstringustr-suggest)
75
66
  * [convertUnitTo](#convertunittofromunitcode-fromval-tounitcode-options)
@@ -79,8 +70,8 @@ Below is documentation for the public functions on the UcumLhcUtils instance.
79
70
 
80
71
  #### validateUnitString(uStr, suggest)
81
72
 
82
- This method validates a unit string. It first checks to see if the string passed
83
- in is a unit code that is found in the unit codes table. If it is not found it
73
+ This method validates a unit string. It first checks to see if the string passed
74
+ in is a unit code that is found in the unit codes table. If it is not found it
84
75
  parses the string to see if it resolves to a valid unit string.
85
76
 
86
77
  If a valid unit cannot be found, the string is tested for some common errors,
@@ -107,17 +98,18 @@ unit names and synonyms.
107
98
  occurred);
108
99
  * 'ucumCode' the valid ucum code, which may differ from what was passed
109
100
  in (e.g., if 'Gauss' is passed in, this will contain 'G') OR null if
110
- the string was flagged as invalid or an error occurred;
101
+ the string was flagged as invalid or an error occurred;
111
102
  * 'msg' is an array of messages, if the string is invalid or an
112
103
  error occurred, indicating the problem, or an explanation of a
113
104
  substitution such as the substitution of 'G' for 'Gauss', or
114
- an empty array if no messages were generated ;
115
- * 'unit' which is null if no unit is found, or a hash for a unit found:
105
+ an empty array if no messages were generated;
106
+ * 'unit' which is null if no unit is found, or an object representing the found unit:
116
107
  * 'code' is the unit's ucum code (G in the above example);
117
108
  * 'name' is the unit's name (Gauss in the above example); and
118
109
  * 'guidance' is the unit's guidance/description data.
119
110
  * 'suggestions' if suggestions were requested and found, this is an array
120
- of one or more hash objects. Each hash contains three elements:
111
+ of one or more objects. (Otherwise, it will not be present.)
112
+ Each object contains three elements:
121
113
  * 'msg' which is a message indicating what part of the uStr input
122
114
  parameter the suggestions are for;
123
115
  * 'invalidUnit' which is the unit expression the suggestions are
@@ -125,27 +117,25 @@ unit names and synonyms.
125
117
  * 'units' which is an array of data for each suggested unit found.
126
118
  Each array will contain the unit code, the unit name and the
127
119
  unit guidance (if any).
128
- If no suggestions were requested and found, this property is not
129
- returned.
130
120
 
131
121
  For example, to validate a unit string of m2/g4 (assuming you have created a
132
122
  utils object as described above):
133
123
 
134
124
  var returnObj = utils.validateUnitString('m2/g4');
135
- if (returnObj['status'] === 'valid')
136
- /* the string is valid; returnObj['ucumCode'] will contain the valid
137
- ucum code (may differ from what was entered), returnObj['msg'] may
125
+ if (returnObj.status === 'valid')
126
+ /* the string is valid; returnObj.ucumCode will contain the valid
127
+ ucum code (may differ from what was entered), returnObj.msg may
138
128
  contain a message or messages describing substitution(s) for the
139
- code entered, and retObj['unit'] will contain 3 pieces of data for the
140
- unit - code, name and guidance (provides information about the unit,
129
+ code entered, and returnObj.unit will contain 3 pieces of data for the
130
+ unit - code, name and guidance (provides information about the unit,
141
131
  such as how the unit is used, etc.)*/
142
132
  else
143
- /* returnObj['status'] will be 'invalid' and */
144
- /* returnOb['msg'] will have a message describing the problem */
145
-
146
- For information on unit string formatting, look at the _Ucum Unit Expression
147
- Validation_ section on the [demo page](https://ucum.nlm.nih.gov/ucum-lhc/demo.html).
148
- There is a button labeled "Show entry hints". That will give you a short description
133
+ /* returnObj.status will be 'invalid' and */
134
+ /* returnObj.msg will have a message describing the problem */
135
+
136
+ For information on unit string formatting, look at the _Ucum Unit Expression
137
+ Validation_ section on the [demo page](https://ucum.nlm.nih.gov/ucum-lhc/demo.html).
138
+ There is a button labeled "Show entry hints". That will give you a short description
149
139
  of unit strings, and includes a link to the [UCUM Specification](https://ucum.org/ucum).
150
140
 
151
141
  #### convertUnitTo(fromUnitCode, fromVal, toUnitCode, options)
@@ -160,9 +150,9 @@ using them in actual clinical settings.
160
150
  **Parameters**:
161
151
  1) fromUnitCode: the unit code/expression/string of the unit to be converted;
162
152
  2) fromVal: the number of "from" units to be converted to "to" units;
163
- 3) toUnitCode: the unit code/expression/string of the unit that the from
153
+ 3) toUnitCode: the unit code/expression/string of the unit that the from
164
154
  field is to be converted to;
165
- 4) options: an optional hash of options that can be passed in:
155
+ 4) options: an optional object of options that can be passed in:
166
156
  * 'suggestions' a boolean to indicate whether or not suggestions are wanted
167
157
  for a string that cannot be resolved to a valid unit; true indicates
168
158
  suggestions are wanted; false indicates they are not, and is the default
@@ -172,13 +162,13 @@ using them in actual clinical settings.
172
162
  ignored if neither unit includes a measurement in moles. In such cases
173
163
  the mole-based unit must have a single mole unit in the numerator and the
174
164
  mass-based unit must have a single mass unit in the numerator.
175
- * 'charge' the absolute value of the charge of the substance in question when a conversion
176
- is being requested from mass/moles to equivalents and vice versa. It is required
177
- when one of the units represents a value in equivalents and the other in mass or moles.
165
+ * 'charge' the absolute value of the charge of the substance in question when a conversion
166
+ is being requested from mass/moles to equivalents and vice versa. It is required
167
+ when one of the units represents a value in equivalents and the other in mass or moles.
178
168
  It is ignored if neither unit includes an equivalent unit
179
169
 
180
- **Returns**: a hash with six elements:
181
- * 'status' the will be: 'succeeded' if the conversion was successfully
170
+ **Returns**: an object with six properties:
171
+ * 'status' will be: 'succeeded' if the conversion was successfully
182
172
  calculated; 'failed' if the conversion could not be made, e.g., if
183
173
  the units are not commensurable; or 'error' if an error occurred;
184
174
  * 'toVal' the numeric value indicating the conversion amount (without any
@@ -188,11 +178,13 @@ using them in actual clinical settings.
188
178
  error occurred, indicating the problem, or an explanation of a
189
179
  substitution such as the substitution of 'G' for 'Gauss', or
190
180
  an empty array if no messages were generated;
191
- * 'suggestions' if suggestions were requested and found, this is a hash
192
- that contains at most two elements:
181
+ * 'suggestions' if suggestions were requested and found, this is an object
182
+ that contains at most the following two properties. (If suggestions
183
+ were not requsted or found, the 'suggestions' property will not be
184
+ set.):
193
185
  * 'from' which, if the fromUnitCode input parameter or one or more of
194
- its components could not be found, is an array one or more hash
195
- objects. Each hash contains three elements:
186
+ its components could not be found, is an array of one or more
187
+ objects. Each object contains three elements:
196
188
  * 'msg' which is a message indicating what unit expression the
197
189
  suggestions are for;
198
190
  * 'invalidUnit' which is the unit expression the suggestions are
@@ -203,8 +195,8 @@ using them in actual clinical settings.
203
195
  If no suggestions were found for the fromUnitCode this element
204
196
  will not be included.
205
197
  * 'to' which, if the "to" unit expression or one or more of its
206
- components could not be found, is an array one or more hash
207
- objects. Each hash contains three elements:
198
+ components could not be found, is an array of one or more
199
+ objects. Each object contains three elements:
208
200
  * 'msg' which is a message indicating what part of the toUnitCode
209
201
  input parameter the suggestions are for;
210
202
  * 'invalidUnit' which is the unit expression the suggestions
@@ -214,61 +206,59 @@ using them in actual clinical settings.
214
206
  unit guidance (if any).
215
207
  If no suggestions were found for the toUnitCode this element will
216
208
  not be included.
217
- No 'suggestions' element will be included in the returned hash
218
- object if none were found, whether or not they were requested.
219
209
  * 'fromUnit' the unit object for the fromUnitCode passed in; returned
220
210
  in case it's needed for additional data from the object; and
221
211
  * 'toUnit' the unit object for the toUnitCode passed in; returned
222
212
  in case it's needed for additional data from the object.
223
213
 
224
- For example, to convert 27 U.S. fathoms to U.S. inches (assuming you have
225
- created a utils object as described above):
226
-
214
+ For example, to convert 27 U.S. fathoms to U.S. inches (assuming you have
215
+ created a utils object as described above):
216
+
227
217
  var returnObj = utils.convertUnitTo('[fth_us]', 27, '[in_us]');
228
- if (returnObj['status'] === 'succeeded')
218
+ if (returnObj.status === 'succeeded')
229
219
  /* the conversion was successful.
230
- returnObj['toVal'] will contain the conversion result
220
+ returnObj.toVal will contain the conversion result
231
221
  (~1943.9999999999998 - number, not formatted string)
232
- returnObj['msg'] will be null
233
- returnObj['fromUnit'] will contain the unit object for [fth_us]
234
- returnObj['toUnit'] will contain the unit object for [in_us]
222
+ returnObj.msg will be null
223
+ returnObj.fromUnit will contain the unit object for [fth_us]
224
+ returnObj.toUnit will contain the unit object for [in_us]
235
225
  */
236
- else if (returnObj['status'] === 'failed')
226
+ else if (returnObj.status === 'failed')
237
227
  /* the conversion could not be made.
238
- returnObj['toVal'] will be null
239
- returnObj['msg'] will contain a message describing the failure
240
- returnObj['fromUnit'] will be null
241
- returnObj['toUnit'] will be null
228
+ returnObj.toVal will be null
229
+ returnObj.msg will contain a message describing the failure
230
+ returnObj.fromUnit will be null
231
+ returnObj.toUnit will be null
242
232
  */
243
- else (returnObj['status'] === 'error)
233
+ else if (returnObj.status === 'error')
244
234
  /* the conversion encountered an error
245
- returnObj['toVal'] will be null
246
- returnObj['msg'] will contain a message describing the error
247
- returnObj['fromUnit'] will be null
248
- returnObj['toUnit'] will be null
235
+ returnObj.toVal will be null
236
+ returnObj.msg will contain a message describing the error
237
+ returnObj.fromUnit will be null
238
+ returnObj.toUnit will be null
249
239
  */
250
-
251
- If you want to know what unit types a particular unit can be converted to, the
240
+
241
+ If you want to know what unit types a particular unit can be converted to, the
252
242
  checkSynonyms function will provide a list of commensurable units for a specified
253
243
  unit expression.
254
244
 
255
245
  #### checkSynonyms(theSyn)
256
246
 
257
247
  This method searches for units that include a single search term (theSyn) in the
258
- unit's synonyms data and/or the unit name. It returns all units found with a
248
+ unit's synonyms data and/or the unit name. It returns all units found with a
259
249
  match. This is useful when an exact match for a term is not found. For example,
260
- submitting the term "pound" to the _validUnitString_ method will result in a
261
- "not found" response. Submitting it to this method will return with a list
250
+ submitting the term "pound" to the _validateUnitString_ method will result in a
251
+ "not found" response. Submitting it to this method will return a list
262
252
  of possible pound units.
263
253
 
264
254
  **Parameters**:
265
255
  1) theSyn: the term to search for
266
256
 
267
- **Returns**: a hash with three elements:
257
+ **Returns**: an object with three properties:
268
258
  * 'status' contains the status of the request, which can be 'error',
269
- 'failed' or 'succeeded';
270
- * 'msg' contains a message for an error or if no units were found; and
271
- * 'units' which is an array that contains one hash for each unit found:
259
+ 'failed' or 'succeeded';
260
+ * 'msg' contains a message for an error or if no units were found; and
261
+ * 'units' which is an array that contains one object for each unit found:
272
262
  * 'code' is the unit's code;
273
263
  * 'name' is the unit's name; and
274
264
  * 'guidance' is the guidance, or description, for the unit.
@@ -283,14 +273,14 @@ of possible pound units.
283
273
  (assuming you have created a utils object as described above):
284
274
 
285
275
  var returnObj = utils.checkSynonyms('pound');
286
- if (returnObj['status'] === 'succeeded')
287
- /* one or more units was found. returnObj['msg'] will be null and the
288
- returnObj['units'] array will contain the data listed above */
289
- else if (returnObj['status'] === 'failed')
290
- /* no units were found and the returnObj['msg'] string will indicate that
276
+ if (returnObj.status === 'succeeded')
277
+ /* one or more units were found. returnObj.msg will be null and the
278
+ returnObj.units array will contain the data listed above */
279
+ else if (returnObj.status === 'failed')
280
+ /* no units were found and the returnObj.msg string will indicate that
291
281
  */
292
282
  else
293
- /* returnObj['status'] will be 'error' and returnObj['msg'] will indicate
283
+ /* returnObj.status will be 'error' and returnObj.msg will indicate
294
284
  what the error was. */
295
285
 
296
286
  #### convertToBaseUnits(fromUnit, fromVal)
@@ -314,7 +304,7 @@ a magnitude, and returns that data.
314
304
  substitution such as the substitution of 'G' for 'Gauss', or
315
305
  an empty array if no messages were generated. There can also be a
316
306
  message that is just informational or warning.
317
- * magnitude: the new value when fromVal units of fromUnits is expressed in the base units.
307
+ * magnitude: the new value when fromVal units of fromUnit is expressed in the base units.
318
308
  * fromUnitIsSpecial: whether the input unit fromUnit is a "special unit"
319
309
  as defined in UCUM. This means there is some function applied to convert
320
310
  between fromUnit and the base units, so the returned magnitude is likely not
@@ -324,10 +314,10 @@ a magnitude, and returns that data.
324
314
 
325
315
  #### commensurablesList(fromUnit[, categoryList])
326
316
 
327
- Retrieves a list of units commensurable, i.e., that can be converted from and
328
- to, a specified unit. Returns an error if the "from" unit cannot be found.
329
- If necessary, you can filter the list of units by specifying a list of unit
330
- categories that should be in the resulting list.
317
+ Retrieves a list of units that are commensurable with, i.e., that can be
318
+ converted from and to, a specified unit. Returns an error if the "from" unit
319
+ cannot be found. If necessary, you can filter the list of units by specifying a
320
+ list of unit categories that should be in the resulting list.
331
321
 
332
322
  **Parameters**:
333
323
  1) fromUnit: the name/unit string
@@ -347,11 +337,10 @@ categories that should be in the resulting list.
347
337
  ### Download the GitHub repository
348
338
 
349
339
  The code available here on GitHub includes functions and scripts to perform
350
- additional functions, mainly to convert ucum data from various formats to
340
+ additional functions, mainly to convert ucum data from various formats to
351
341
  the data used by our code as well as the code that supports the demo page.
352
- Click on the green "Code" button above to download the repository.
353
342
 
354
-
343
+
355
344
  ### Building the code and data
356
345
  If you wish to modify the code, the build process is simply:
357
346
 
@@ -364,11 +353,10 @@ Tests can be run with:
364
353
 
365
354
  npm run test
366
355
 
367
- Although package does not directly use the UCUM "Functional Tests"
356
+ This will include tests from the UCUM "Functional Tests"
368
357
  (https://ucum.org/docs/functional-tests and
369
358
  https://github.com/FHIR/Ucum-java/blob/master/src/test/resources/UcumFunctionalTests.xml),
370
- we have run those tests using ucum-lhc, and we found that all but two tests pass.
371
- The two tests that do not pass are checking for rounding of values at the right
372
- number of signifant digits. The package does not round to significant digits,
373
- since if you round too early in a calculation, you can lose accuracy.
374
-
359
+ which pass with the exception of rounding differences. That is, ucum-lhc does
360
+ not round, while the UCUM Functional Tests assume rounding, which is not a part
361
+ of the UCUM specification. The reason this package does not round is that if
362
+ you round too early in a calculation, you can lose accuracy.