unitsdb 0.0.0-stage → 0.1.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.md +53 -0
- package/README.md +39 -2
- package/dist/data/unitsdb.json +1 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.js +137 -0
- package/package.json +30 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export interface Identifier {
|
|
2
|
+
type: string;
|
|
3
|
+
id: string;
|
|
4
|
+
}
|
|
5
|
+
export interface LocalizedString {
|
|
6
|
+
lang: string;
|
|
7
|
+
value: string;
|
|
8
|
+
}
|
|
9
|
+
export interface UnitSymbol {
|
|
10
|
+
id?: string;
|
|
11
|
+
ascii?: string;
|
|
12
|
+
unicode?: string;
|
|
13
|
+
latex?: string;
|
|
14
|
+
html?: string;
|
|
15
|
+
mathml?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface DerivedBase {
|
|
18
|
+
unit_reference: {
|
|
19
|
+
id: string;
|
|
20
|
+
};
|
|
21
|
+
prefix_reference: {
|
|
22
|
+
id: string;
|
|
23
|
+
} | null;
|
|
24
|
+
power: string | number | null;
|
|
25
|
+
}
|
|
26
|
+
export interface UnitRecord {
|
|
27
|
+
identifiers?: Identifier[];
|
|
28
|
+
names?: LocalizedString[];
|
|
29
|
+
symbols?: UnitSymbol[];
|
|
30
|
+
unit_system_reference?: Identifier[];
|
|
31
|
+
si_derived_bases?: DerivedBase[];
|
|
32
|
+
dimension_reference?: {
|
|
33
|
+
id: string;
|
|
34
|
+
} | null;
|
|
35
|
+
quantity_references?: Identifier[];
|
|
36
|
+
}
|
|
37
|
+
export interface PrefixRecord {
|
|
38
|
+
identifiers?: Identifier[];
|
|
39
|
+
names?: LocalizedString[];
|
|
40
|
+
symbols?: UnitSymbol[];
|
|
41
|
+
base?: number;
|
|
42
|
+
power?: number;
|
|
43
|
+
}
|
|
44
|
+
export interface DimensionRecord {
|
|
45
|
+
id: string | null;
|
|
46
|
+
[component: string]: unknown;
|
|
47
|
+
}
|
|
48
|
+
export interface QuantityRecord {
|
|
49
|
+
id: string | null;
|
|
50
|
+
names?: LocalizedString[];
|
|
51
|
+
dimension_reference?: {
|
|
52
|
+
id: string;
|
|
53
|
+
} | null;
|
|
54
|
+
quantityType?: string;
|
|
55
|
+
}
|
|
56
|
+
export declare const Unitsdb: {
|
|
57
|
+
units: UnitRecord[];
|
|
58
|
+
/** unitsml-ruby `Unitsdb.units.find_by_symbol_id` */
|
|
59
|
+
findUnitBySymbolId(symbolId: string): UnitRecord | undefined;
|
|
60
|
+
/** unitsml-ruby `Unitsdb.units.find_by_name` (via symbols) */
|
|
61
|
+
findUnitByName(name: string): UnitRecord | undefined;
|
|
62
|
+
/** unitsml-ruby `Unitsdb.units.find_by_id` (via identifiers) */
|
|
63
|
+
findUnitById(id: string): UnitRecord | undefined;
|
|
64
|
+
prefixes: PrefixRecord[];
|
|
65
|
+
/** unitsml-ruby `Unitsdb.prefixes.find_by_symbol_name` */
|
|
66
|
+
findPrefixBySymbolName(ascii: string): PrefixRecord | undefined;
|
|
67
|
+
/** unitsml-ruby `Unitsdb.prefixes.find_by_id` */
|
|
68
|
+
findPrefixById(id: string): PrefixRecord | undefined;
|
|
69
|
+
/** unitsml-ruby `Unitsdb.prefixes_array` (ascii symbols, length-sorted) */
|
|
70
|
+
prefixSymbolNames(): string[];
|
|
71
|
+
dimensions: DimensionRecord[];
|
|
72
|
+
findDimensionById(id: string): DimensionRecord | undefined;
|
|
73
|
+
/** unitsml-ruby `Unitsdb.dimensions.find_by_vector` (exponent vector) */
|
|
74
|
+
findDimensionByVector(vector: string): DimensionRecord | undefined;
|
|
75
|
+
quantities: QuantityRecord[];
|
|
76
|
+
findQuantityById(id: string): QuantityRecord | undefined;
|
|
77
|
+
};
|
|
78
|
+
/** The DIMS_VECTOR-ordered exponent string unitsml-ruby uses for lookups. */
|
|
79
|
+
export declare function dimensionVector(dim: DimensionRecord): string;
|
|
80
|
+
/** Components in the RECORD's own key order (the unitsdb YAML order,
|
|
81
|
+
* which is what unitsml-ruby's processed_keys preserves). */
|
|
82
|
+
export declare function dimensionComponents(dim: DimensionRecord): Array<[string, {
|
|
83
|
+
symbol?: string;
|
|
84
|
+
power?: unknown;
|
|
85
|
+
}]>;
|
|
86
|
+
export declare function unitNistId(unit: UnitRecord): string | undefined;
|
|
87
|
+
export declare function unitEnName(unit: UnitRecord): string | undefined;
|
|
88
|
+
export declare function prefixNistId(prefix: PrefixRecord): string | undefined;
|
|
89
|
+
export declare function prefixEnName(prefix: PrefixRecord): string | undefined;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// UnitsDB data access for the unitsml model: finders and record
|
|
2
|
+
// accessors over the vendored dataset (built from unitsml/unitsdb
|
|
3
|
+
// releases; see data/README in the unitsdb repo for the schema).
|
|
4
|
+
import dataset from "./data/unitsdb.json" with { type: "json" };
|
|
5
|
+
const units = dataset.units;
|
|
6
|
+
const prefixes = dataset.prefixes;
|
|
7
|
+
const dimensions = dataset.dimensions;
|
|
8
|
+
const quantities = dataset.quantities;
|
|
9
|
+
const unitsBySymbolId = new Map();
|
|
10
|
+
for (const unit of units) {
|
|
11
|
+
for (const symbol of unit.symbols ?? []) {
|
|
12
|
+
if (symbol.id !== undefined && symbol.id !== null) {
|
|
13
|
+
unitsBySymbolId.set(symbol.id, unit);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
// Ruby find_first_through — FIRST record wins on identifier collisions.
|
|
18
|
+
const unitsByIdentifier = new Map();
|
|
19
|
+
for (const unit of units) {
|
|
20
|
+
for (const identifier of unit.identifiers ?? []) {
|
|
21
|
+
if (!unitsByIdentifier.has(identifier.id)) {
|
|
22
|
+
unitsByIdentifier.set(identifier.id, unit);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const prefixesBySymbolName = new Map();
|
|
27
|
+
for (const prefix of prefixes) {
|
|
28
|
+
for (const symbol of prefix.symbols ?? []) {
|
|
29
|
+
if (symbol.ascii)
|
|
30
|
+
prefixesBySymbolName.set(symbol.ascii, prefix);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const prefixesByIdentifier = new Map();
|
|
34
|
+
for (const prefix of prefixes) {
|
|
35
|
+
for (const identifier of prefix.identifiers ?? []) {
|
|
36
|
+
if (!prefixesByIdentifier.has(identifier.id)) {
|
|
37
|
+
prefixesByIdentifier.set(identifier.id, prefix);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const prefixSymbolNames = [...prefixesBySymbolName.keys()].sort((a, b) => a.length - b.length);
|
|
42
|
+
const dimensionsById = new Map();
|
|
43
|
+
for (const dim of dimensions) {
|
|
44
|
+
if (dim.id)
|
|
45
|
+
dimensionsById.set(dim.id, dim);
|
|
46
|
+
}
|
|
47
|
+
const quantitiesById = new Map();
|
|
48
|
+
for (const quantity of quantities) {
|
|
49
|
+
if (quantity.id)
|
|
50
|
+
quantitiesById.set(quantity.id, quantity);
|
|
51
|
+
}
|
|
52
|
+
const quantitiesByIdentifier = new Map();
|
|
53
|
+
for (const quantity of quantities) {
|
|
54
|
+
for (const identifier of quantity.identifiers ?? []) {
|
|
55
|
+
if (!quantitiesByIdentifier.has(identifier.id)) {
|
|
56
|
+
quantitiesByIdentifier.set(identifier.id, quantity);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export const Unitsdb = {
|
|
61
|
+
units,
|
|
62
|
+
/** unitsml-ruby `Unitsdb.units.find_by_symbol_id` */
|
|
63
|
+
findUnitBySymbolId(symbolId) {
|
|
64
|
+
return unitsBySymbolId.get(symbolId);
|
|
65
|
+
},
|
|
66
|
+
/** unitsml-ruby `Unitsdb.units.find_by_name` (via symbols) */
|
|
67
|
+
findUnitByName(name) {
|
|
68
|
+
return unitsBySymbolId.get(name);
|
|
69
|
+
},
|
|
70
|
+
/** unitsml-ruby `Unitsdb.units.find_by_id` (via identifiers) */
|
|
71
|
+
findUnitById(id) {
|
|
72
|
+
return unitsByIdentifier.get(id);
|
|
73
|
+
},
|
|
74
|
+
prefixes,
|
|
75
|
+
/** unitsml-ruby `Unitsdb.prefixes.find_by_symbol_name` */
|
|
76
|
+
findPrefixBySymbolName(ascii) {
|
|
77
|
+
return prefixesBySymbolName.get(ascii);
|
|
78
|
+
},
|
|
79
|
+
/** unitsml-ruby `Unitsdb.prefixes.find_by_id` */
|
|
80
|
+
findPrefixById(id) {
|
|
81
|
+
return prefixesByIdentifier.get(id);
|
|
82
|
+
},
|
|
83
|
+
/** unitsml-ruby `Unitsdb.prefixes_array` (ascii symbols, length-sorted) */
|
|
84
|
+
prefixSymbolNames() {
|
|
85
|
+
return prefixSymbolNames;
|
|
86
|
+
},
|
|
87
|
+
dimensions,
|
|
88
|
+
findDimensionById(id) {
|
|
89
|
+
return dimensionsById.get(id);
|
|
90
|
+
},
|
|
91
|
+
/** unitsml-ruby `Unitsdb.dimensions.find_by_vector` (exponent vector) */
|
|
92
|
+
findDimensionByVector(vector) {
|
|
93
|
+
return dimensions.find((dim) => dimensionVector(dim) === vector);
|
|
94
|
+
},
|
|
95
|
+
quantities,
|
|
96
|
+
findQuantityById(id) {
|
|
97
|
+
return quantitiesById.get(id) ?? quantitiesByIdentifier.get(id);
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
// DIMS_VECTOR order (unitsml-ruby Utility::DIMS_VECTOR) — the vector
|
|
101
|
+
// string fed to unitsdb dimension lookups depends on it.
|
|
102
|
+
const DIM_COMPONENT_KEYS = [
|
|
103
|
+
"thermodynamic_temperature",
|
|
104
|
+
"amount_of_substance",
|
|
105
|
+
"luminous_intensity",
|
|
106
|
+
"electric_current",
|
|
107
|
+
"plane_angle",
|
|
108
|
+
"length",
|
|
109
|
+
"mass",
|
|
110
|
+
"time",
|
|
111
|
+
];
|
|
112
|
+
/** The DIMS_VECTOR-ordered exponent string unitsml-ruby uses for lookups. */
|
|
113
|
+
export function dimensionVector(dim) {
|
|
114
|
+
return DIM_COMPONENT_KEYS.map((key) => {
|
|
115
|
+
const component = dim[key];
|
|
116
|
+
return component?.power ?? "";
|
|
117
|
+
}).join(":");
|
|
118
|
+
}
|
|
119
|
+
/** Components in the RECORD's own key order (the unitsdb YAML order,
|
|
120
|
+
* which is what unitsml-ruby's processed_keys preserves). */
|
|
121
|
+
export function dimensionComponents(dim) {
|
|
122
|
+
return Object.keys(dim)
|
|
123
|
+
.filter((key) => key !== "id" && dim[key] !== undefined)
|
|
124
|
+
.map((key) => [key, dim[key]]);
|
|
125
|
+
}
|
|
126
|
+
export function unitNistId(unit) {
|
|
127
|
+
return unit.identifiers?.find((i) => i.type === "nist")?.id;
|
|
128
|
+
}
|
|
129
|
+
export function unitEnName(unit) {
|
|
130
|
+
return unit.names?.find((n) => n.lang === "en")?.value;
|
|
131
|
+
}
|
|
132
|
+
export function prefixNistId(prefix) {
|
|
133
|
+
return prefix.identifiers?.find((i) => i.type === "nist")?.id;
|
|
134
|
+
}
|
|
135
|
+
export function prefixEnName(prefix) {
|
|
136
|
+
return prefix.names?.find((n) => n.lang === "en")?.value;
|
|
137
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unitsdb",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "UnitsDB in TypeScript: typed access to the units database (units, prefixes, dimensions, quantities, currencies) rebuilt from unitsml/unitsdb releases",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"prepare": "npm run build",
|
|
20
|
+
"test": "node --test",
|
|
21
|
+
"dataset": "ruby scripts/build-dataset.rb > /dev/null && cp /tmp/unitsdb-ts.json src/data/unitsdb.json"
|
|
22
|
+
},
|
|
23
|
+
"license": "BSD-2-Clause",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/unitsml/unitsdb-ts"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^22.0.0",
|
|
30
|
+
"typescript": "^5.5.0"
|
|
31
|
+
}
|
|
32
|
+
}
|