@lifefrom/units 0.0.0-stage → 1.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/LICENSE +36 -0
- package/README.md +23 -2
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +70 -0
- package/dist/src/data.d.ts +2 -0
- package/dist/src/data.js +1 -0
- package/dist/src/index.d.ts +110 -0
- package/dist/src/index.js +480 -0
- package/dist/src/types.d.ts +104 -0
- package/dist/src/types.js +2 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
lifefrom-units is released under two licences.
|
|
2
|
+
|
|
3
|
+
Code (python/, ts/, scripts/, tests/): MIT License
|
|
4
|
+
Data (data/, web/, tests/vectors.json, the units.json copies inside the packages): Creative Commons
|
|
5
|
+
Attribution 4.0 International (CC BY 4.0), https://creativecommons.org/licenses/by/4.0/
|
|
6
|
+
|
|
7
|
+
Third-party material:
|
|
8
|
+
- Molar masses are facts taken from PubChem (NCBI) and CIAAW; each is cited in data/units.json.
|
|
9
|
+
- UCUM code strings in data/units.json are expressions in the Unified Code for Units of Measure,
|
|
10
|
+
Copyright 1999-2024 Regenstrief Institute, Inc., used under the UCUM License, Version 1.1
|
|
11
|
+
(https://ucum.org/license). This repository does not include or redistribute the UCUM tables
|
|
12
|
+
(ucum-essence.xml). The mapping from lab spellings to UCUM codes is LifeFrom's work, is not part of
|
|
13
|
+
UCUM, and is not a units standard.
|
|
14
|
+
|
|
15
|
+
-------------------------------------------------------------------------------
|
|
16
|
+
MIT License
|
|
17
|
+
|
|
18
|
+
Copyright (c) 2026 LifeFrom (https://lifefrom.ai) and Julian Caraulani
|
|
19
|
+
|
|
20
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
21
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
22
|
+
in the Software without restriction, including without limitation the rights
|
|
23
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
24
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
25
|
+
furnished to do so, subject to the following conditions:
|
|
26
|
+
|
|
27
|
+
The above copyright notice and this permission notice shall be included in all
|
|
28
|
+
copies or substantial portions of the Software.
|
|
29
|
+
|
|
30
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
31
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
32
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
33
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
34
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
35
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
36
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lifefrom/units
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Normalise clinical lab unit spellings and convert lab values with sourced, analyte-specific factors. ESM, strict TypeScript types, zero runtime dependencies. It uses the same data and the same test vectors as the Python package `lifefrom-units`.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { convert, convertResult, normalizeUnit, sameUnit, explain, LifeFromUnitsError } from '@lifefrom/units';
|
|
7
|
+
|
|
8
|
+
normalizeUnit('mcg/l'); // 'µg/L'
|
|
9
|
+
sameUnit('µg/L', 'ng/mL'); // true
|
|
10
|
+
convert('5,4', 'mmol/L', 'mg/dL', 'glucose'); // 97.2864
|
|
11
|
+
convertResult('5,4 H', 'mmol/L', 'mg/dL', 'glucose').text; // '97.2864 H'
|
|
12
|
+
convert(10, 'µIU/mL', 'pmol/L', 'insulin', 6); // 60, your factor (explain() records it)
|
|
13
|
+
convert(50, 'mg/dL', 'nmol/L', 'lipoprotein-a'); // throws LifeFromUnitsError, code 'forbidden'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Error codes:
|
|
17
|
+
|
|
18
|
+
- `unknown_unit`, `unknown_analyte`
|
|
19
|
+
- `incompatible_units`
|
|
20
|
+
- `forbidden`
|
|
21
|
+
- `no_sourced_conversion`
|
|
22
|
+
- `invalid_value`, `qualified_value`, `ambiguous_value`
|
|
23
|
+
|
|
24
|
+
Sources, checks and limitations are described in the repository README and METHODS: https://github.com/caraulani/lifefrom-units
|
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** lifefrom-units CLI (TypeScript build): convert | normalize | same | explain */
|
|
3
|
+
import { convertResult, explain, LifeFromUnitsError, normalizeUnit, sameUnit } from './index.js';
|
|
4
|
+
function flag(args, name) {
|
|
5
|
+
const i = args.findIndex((a) => a === `--${name}` || a === `-${name[0]}`);
|
|
6
|
+
if (i < 0)
|
|
7
|
+
return undefined;
|
|
8
|
+
const v = args[i + 1];
|
|
9
|
+
args.splice(i, 2);
|
|
10
|
+
return v;
|
|
11
|
+
}
|
|
12
|
+
function main(argv) {
|
|
13
|
+
const args = [...argv];
|
|
14
|
+
const cmd = args.shift();
|
|
15
|
+
const json = args.includes('--json');
|
|
16
|
+
if (json)
|
|
17
|
+
args.splice(args.indexOf('--json'), 1);
|
|
18
|
+
const analyte = flag(args, 'analyte');
|
|
19
|
+
const factor = flag(args, 'factor');
|
|
20
|
+
try {
|
|
21
|
+
if (cmd === 'convert' && args.length === 3) {
|
|
22
|
+
const r = convertResult(args[0], args[1], args[2], analyte, factor);
|
|
23
|
+
if (json)
|
|
24
|
+
console.log(JSON.stringify({ result: r, explanation: explain(args[1], args[2], analyte, undefined, factor) }, null, 2));
|
|
25
|
+
else {
|
|
26
|
+
console.log(r.text);
|
|
27
|
+
if (factor !== undefined)
|
|
28
|
+
console.error('note: factor supplied by you; not checked by lifefrom-units');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (cmd === 'normalize' && args.length >= 1) {
|
|
32
|
+
for (const u of args)
|
|
33
|
+
console.log(normalizeUnit(u));
|
|
34
|
+
}
|
|
35
|
+
else if (cmd === 'same' && args.length === 2) {
|
|
36
|
+
const ok = sameUnit(args[0], args[1], analyte);
|
|
37
|
+
console.log(ok ? 'same' : 'different');
|
|
38
|
+
return ok ? 0 : 1;
|
|
39
|
+
}
|
|
40
|
+
else if (cmd === 'explain' && args.length === 2) {
|
|
41
|
+
const e = explain(args[0], args[1], analyte, undefined, factor);
|
|
42
|
+
if (json)
|
|
43
|
+
console.log(JSON.stringify(e, null, 2));
|
|
44
|
+
else {
|
|
45
|
+
console.log(e.text);
|
|
46
|
+
for (const s of e.sources)
|
|
47
|
+
console.log(` source: ${s.label} <${s.url}>`);
|
|
48
|
+
for (const w of e.warnings)
|
|
49
|
+
console.log(` warning: ${w}`);
|
|
50
|
+
if (e.derived)
|
|
51
|
+
console.log(' derived: unit arithmetic');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
console.error('usage: lifefrom-units convert <value> <from> <to> [--analyte X] [--factor F] [--json]\n' +
|
|
56
|
+
' lifefrom-units normalize <unit>...\n lifefrom-units same <a> <b> [--analyte X]\n' +
|
|
57
|
+
' lifefrom-units explain <from> <to> [--analyte X] [--factor F] [--json]');
|
|
58
|
+
return 64;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
if (e instanceof LifeFromUnitsError) {
|
|
63
|
+
console.error(`lifefrom-units: ${e.code}: ${e.message}`);
|
|
64
|
+
return 2;
|
|
65
|
+
}
|
|
66
|
+
throw e;
|
|
67
|
+
}
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
process.exitCode = main(process.argv.slice(2));
|