unitsdb 0.0.0-stage → 0.1.1
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 +121 -0
- package/dist/index.js +214 -0
- package/package.json +30 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
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 interface CurrencySymbol {
|
|
57
|
+
id?: string;
|
|
58
|
+
ascii?: string;
|
|
59
|
+
unicode?: string;
|
|
60
|
+
}
|
|
61
|
+
export interface CurrencyRecord {
|
|
62
|
+
identifiers?: Identifier[];
|
|
63
|
+
names?: LocalizedString[];
|
|
64
|
+
symbols?: CurrencySymbol[];
|
|
65
|
+
/** ISO 4217 "N.A." minor units are absent — the key is omitted, never 0. */
|
|
66
|
+
minor_unit_exponent?: number;
|
|
67
|
+
status?: string;
|
|
68
|
+
unit_system_reference?: Identifier[];
|
|
69
|
+
quantity_references?: Identifier[];
|
|
70
|
+
notes?: string | null;
|
|
71
|
+
}
|
|
72
|
+
export declare const Unitsdb: {
|
|
73
|
+
units: UnitRecord[];
|
|
74
|
+
/** unitsml-ruby `Unitsdb.units.find_by_symbol_id` */
|
|
75
|
+
findUnitBySymbolId(symbolId: string): UnitRecord | undefined;
|
|
76
|
+
/** unitsml-ruby `Unitsdb.units.find_by_name` (via symbols) */
|
|
77
|
+
findUnitByName(name: string): UnitRecord | undefined;
|
|
78
|
+
/** unitsml-ruby `Unitsdb.units.find_by_id` (via identifiers) */
|
|
79
|
+
findUnitById(id: string): UnitRecord | undefined;
|
|
80
|
+
prefixes: PrefixRecord[];
|
|
81
|
+
/** unitsml-ruby `Unitsdb.prefixes.find_by_symbol_name` */
|
|
82
|
+
findPrefixBySymbolName(ascii: string): PrefixRecord | undefined;
|
|
83
|
+
/** unitsml-ruby `Unitsdb.prefixes.find_by_id` */
|
|
84
|
+
findPrefixById(id: string): PrefixRecord | undefined;
|
|
85
|
+
/** unitsml-ruby `Unitsdb.prefixes_array` (ascii symbols, length-sorted) */
|
|
86
|
+
prefixSymbolNames(): string[];
|
|
87
|
+
dimensions: DimensionRecord[];
|
|
88
|
+
findDimensionById(id: string): DimensionRecord | undefined;
|
|
89
|
+
/** unitsml-ruby `Unitsdb.dimensions.find_by_vector` (exponent vector) */
|
|
90
|
+
findDimensionByVector(vector: string): DimensionRecord | undefined;
|
|
91
|
+
quantities: QuantityRecord[];
|
|
92
|
+
findQuantityById(id: string): QuantityRecord | undefined;
|
|
93
|
+
currencies: CurrencyRecord[];
|
|
94
|
+
/** unitsml-ruby `Unitsdb.currencies.find_by_code` (case-insensitive alpha) */
|
|
95
|
+
findCurrencyByCode(code: string): CurrencyRecord | undefined;
|
|
96
|
+
/** unitsml-ruby `Unitsdb.currencies.find_by_numeric` (3-digit, base-10) */
|
|
97
|
+
findCurrencyByNumeric(numeric: string | number): CurrencyRecord | undefined;
|
|
98
|
+
/** unitsml-ruby `Unitsdb.find_by_symbol(_, :currencies)` — unicode sign
|
|
99
|
+
* (€, A$) or ascii alpha code (EUR, AUD), case-insensitive on ascii. */
|
|
100
|
+
findCurrenciesBySymbol(symbol: string): CurrencyRecord[];
|
|
101
|
+
/** unitsml-ruby `Unitsdb.get_by_id` across collections (REQUIRED first,
|
|
102
|
+
* OPTIONAL currencies last; first record wins on collisions). */
|
|
103
|
+
findById(id: string, type?: string): UnitRecord | PrefixRecord | DimensionRecord | QuantityRecord | CurrencyRecord | undefined;
|
|
104
|
+
/** unitsml-ruby `Unitsdb.search` — substring over identifiers and names. */
|
|
105
|
+
search(params: {
|
|
106
|
+
text?: string;
|
|
107
|
+
type?: string;
|
|
108
|
+
}): Array<UnitRecord | PrefixRecord | DimensionRecord | QuantityRecord | CurrencyRecord>;
|
|
109
|
+
};
|
|
110
|
+
/** The DIMS_VECTOR-ordered exponent string unitsml-ruby uses for lookups. */
|
|
111
|
+
export declare function dimensionVector(dim: DimensionRecord): string;
|
|
112
|
+
/** Components in the RECORD's own key order (the unitsdb YAML order,
|
|
113
|
+
* which is what unitsml-ruby's processed_keys preserves). */
|
|
114
|
+
export declare function dimensionComponents(dim: DimensionRecord): Array<[string, {
|
|
115
|
+
symbol?: string;
|
|
116
|
+
power?: unknown;
|
|
117
|
+
}]>;
|
|
118
|
+
export declare function unitNistId(unit: UnitRecord): string | undefined;
|
|
119
|
+
export declare function unitEnName(unit: UnitRecord): string | undefined;
|
|
120
|
+
export declare function prefixNistId(prefix: PrefixRecord): string | undefined;
|
|
121
|
+
export declare function prefixEnName(prefix: PrefixRecord): string | undefined;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
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 currencies = dataset.currencies;
|
|
10
|
+
const unitsBySymbolId = new Map();
|
|
11
|
+
for (const unit of units) {
|
|
12
|
+
for (const symbol of unit.symbols ?? []) {
|
|
13
|
+
if (symbol.id !== undefined && symbol.id !== null) {
|
|
14
|
+
unitsBySymbolId.set(symbol.id, unit);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// Ruby find_first_through — FIRST record wins on identifier collisions.
|
|
19
|
+
const unitsByIdentifier = new Map();
|
|
20
|
+
for (const unit of units) {
|
|
21
|
+
for (const identifier of unit.identifiers ?? []) {
|
|
22
|
+
if (!unitsByIdentifier.has(identifier.id)) {
|
|
23
|
+
unitsByIdentifier.set(identifier.id, unit);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const prefixesBySymbolName = new Map();
|
|
28
|
+
for (const prefix of prefixes) {
|
|
29
|
+
for (const symbol of prefix.symbols ?? []) {
|
|
30
|
+
if (symbol.ascii)
|
|
31
|
+
prefixesBySymbolName.set(symbol.ascii, prefix);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const prefixesByIdentifier = new Map();
|
|
35
|
+
for (const prefix of prefixes) {
|
|
36
|
+
for (const identifier of prefix.identifiers ?? []) {
|
|
37
|
+
if (!prefixesByIdentifier.has(identifier.id)) {
|
|
38
|
+
prefixesByIdentifier.set(identifier.id, prefix);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const prefixSymbolNames = [...prefixesBySymbolName.keys()].sort((a, b) => a.length - b.length);
|
|
43
|
+
const dimensionsById = new Map();
|
|
44
|
+
for (const dim of dimensions) {
|
|
45
|
+
if (dim.id)
|
|
46
|
+
dimensionsById.set(dim.id, dim);
|
|
47
|
+
}
|
|
48
|
+
const quantitiesById = new Map();
|
|
49
|
+
for (const quantity of quantities) {
|
|
50
|
+
if (quantity.id)
|
|
51
|
+
quantitiesById.set(quantity.id, quantity);
|
|
52
|
+
}
|
|
53
|
+
const quantitiesByIdentifier = new Map();
|
|
54
|
+
for (const quantity of quantities) {
|
|
55
|
+
for (const identifier of quantity.identifiers ?? []) {
|
|
56
|
+
if (!quantitiesByIdentifier.has(identifier.id)) {
|
|
57
|
+
quantitiesByIdentifier.set(identifier.id, quantity);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Ruby find_first_through — FIRST record wins on identifier collisions.
|
|
62
|
+
const currenciesByCode = new Map();
|
|
63
|
+
for (const currency of currencies) {
|
|
64
|
+
for (const identifier of currency.identifiers ?? []) {
|
|
65
|
+
if (identifier.type === "iso4217" && !currenciesByCode.has(identifier.id)) {
|
|
66
|
+
currenciesByCode.set(identifier.id, currency);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const currenciesByNumeric = new Map();
|
|
71
|
+
for (const currency of currencies) {
|
|
72
|
+
for (const identifier of currency.identifiers ?? []) {
|
|
73
|
+
if (identifier.type === "iso4217_numeric" &&
|
|
74
|
+
!currenciesByNumeric.has(identifier.id)) {
|
|
75
|
+
currenciesByNumeric.set(identifier.id, currency);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function currencySymbolMatch(currency, symbol) {
|
|
80
|
+
const needle = symbol.toLowerCase();
|
|
81
|
+
return (currency.symbols ?? []).some((sym) => sym.unicode === symbol || sym.ascii?.toLowerCase() === needle);
|
|
82
|
+
}
|
|
83
|
+
export const Unitsdb = {
|
|
84
|
+
units,
|
|
85
|
+
/** unitsml-ruby `Unitsdb.units.find_by_symbol_id` */
|
|
86
|
+
findUnitBySymbolId(symbolId) {
|
|
87
|
+
return unitsBySymbolId.get(symbolId);
|
|
88
|
+
},
|
|
89
|
+
/** unitsml-ruby `Unitsdb.units.find_by_name` (via symbols) */
|
|
90
|
+
findUnitByName(name) {
|
|
91
|
+
return unitsBySymbolId.get(name);
|
|
92
|
+
},
|
|
93
|
+
/** unitsml-ruby `Unitsdb.units.find_by_id` (via identifiers) */
|
|
94
|
+
findUnitById(id) {
|
|
95
|
+
return unitsByIdentifier.get(id);
|
|
96
|
+
},
|
|
97
|
+
prefixes,
|
|
98
|
+
/** unitsml-ruby `Unitsdb.prefixes.find_by_symbol_name` */
|
|
99
|
+
findPrefixBySymbolName(ascii) {
|
|
100
|
+
return prefixesBySymbolName.get(ascii);
|
|
101
|
+
},
|
|
102
|
+
/** unitsml-ruby `Unitsdb.prefixes.find_by_id` */
|
|
103
|
+
findPrefixById(id) {
|
|
104
|
+
return prefixesByIdentifier.get(id);
|
|
105
|
+
},
|
|
106
|
+
/** unitsml-ruby `Unitsdb.prefixes_array` (ascii symbols, length-sorted) */
|
|
107
|
+
prefixSymbolNames() {
|
|
108
|
+
return prefixSymbolNames;
|
|
109
|
+
},
|
|
110
|
+
dimensions,
|
|
111
|
+
findDimensionById(id) {
|
|
112
|
+
return dimensionsById.get(id);
|
|
113
|
+
},
|
|
114
|
+
/** unitsml-ruby `Unitsdb.dimensions.find_by_vector` (exponent vector) */
|
|
115
|
+
findDimensionByVector(vector) {
|
|
116
|
+
return dimensions.find((dim) => dimensionVector(dim) === vector);
|
|
117
|
+
},
|
|
118
|
+
quantities,
|
|
119
|
+
findQuantityById(id) {
|
|
120
|
+
return quantitiesById.get(id) ?? quantitiesByIdentifier.get(id);
|
|
121
|
+
},
|
|
122
|
+
currencies,
|
|
123
|
+
/** unitsml-ruby `Unitsdb.currencies.find_by_code` (case-insensitive alpha) */
|
|
124
|
+
findCurrencyByCode(code) {
|
|
125
|
+
return currenciesByCode.get(code.toUpperCase());
|
|
126
|
+
},
|
|
127
|
+
/** unitsml-ruby `Unitsdb.currencies.find_by_numeric` (3-digit, base-10) */
|
|
128
|
+
findCurrencyByNumeric(numeric) {
|
|
129
|
+
const parsed = Number.parseInt(String(numeric), 10);
|
|
130
|
+
if (Number.isNaN(parsed))
|
|
131
|
+
return undefined;
|
|
132
|
+
return currenciesByNumeric.get(String(parsed).padStart(3, "0"));
|
|
133
|
+
},
|
|
134
|
+
/** unitsml-ruby `Unitsdb.find_by_symbol(_, :currencies)` — unicode sign
|
|
135
|
+
* (€, A$) or ascii alpha code (EUR, AUD), case-insensitive on ascii. */
|
|
136
|
+
findCurrenciesBySymbol(symbol) {
|
|
137
|
+
return currencies.filter((currency) => currencySymbolMatch(currency, symbol));
|
|
138
|
+
},
|
|
139
|
+
/** unitsml-ruby `Unitsdb.get_by_id` across collections (REQUIRED first,
|
|
140
|
+
* OPTIONAL currencies last; first record wins on collisions). */
|
|
141
|
+
findById(id, type) {
|
|
142
|
+
const matchIdentifier = (record) => (record.identifiers ?? []).some((identifier) => identifier.id === id && (type === undefined || identifier.type === type));
|
|
143
|
+
return (units.find(matchIdentifier) ??
|
|
144
|
+
prefixes.find(matchIdentifier) ??
|
|
145
|
+
dimensions.find((dim) => type === undefined && dim.id === id) ??
|
|
146
|
+
quantities.find(matchIdentifier) ??
|
|
147
|
+
currencies.find(matchIdentifier));
|
|
148
|
+
},
|
|
149
|
+
/** unitsml-ruby `Unitsdb.search` — substring over identifiers and names. */
|
|
150
|
+
search(params) {
|
|
151
|
+
const text = params.text;
|
|
152
|
+
if (!text)
|
|
153
|
+
return [];
|
|
154
|
+
const needle = text.toLowerCase();
|
|
155
|
+
const scope = params.type ?? "all";
|
|
156
|
+
const matchesText = (record) => {
|
|
157
|
+
const identifiers = record.identifiers ?? [];
|
|
158
|
+
return (identifiers.some((i) => i.id.toLowerCase().includes(needle)) ||
|
|
159
|
+
(record.names ?? []).some((n) => n.value.toLowerCase().includes(needle)));
|
|
160
|
+
};
|
|
161
|
+
const results = [];
|
|
162
|
+
const push = (scopeName, records, match) => {
|
|
163
|
+
if (scope !== "all" && scope !== scopeName)
|
|
164
|
+
return;
|
|
165
|
+
for (const record of records)
|
|
166
|
+
if (match(record))
|
|
167
|
+
results.push(record);
|
|
168
|
+
};
|
|
169
|
+
push("units", units, matchesText);
|
|
170
|
+
push("prefixes", prefixes, matchesText);
|
|
171
|
+
push("dimensions", dimensions, (dim) => typeof dim.id === "string" && dim.id.toLowerCase().includes(needle));
|
|
172
|
+
push("quantities", quantities, matchesText);
|
|
173
|
+
push("currencies", currencies, matchesText);
|
|
174
|
+
return results;
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
// DIMS_VECTOR order (unitsml-ruby Utility::DIMS_VECTOR) — the vector
|
|
178
|
+
// string fed to unitsdb dimension lookups depends on it.
|
|
179
|
+
const DIM_COMPONENT_KEYS = [
|
|
180
|
+
"thermodynamic_temperature",
|
|
181
|
+
"amount_of_substance",
|
|
182
|
+
"luminous_intensity",
|
|
183
|
+
"electric_current",
|
|
184
|
+
"plane_angle",
|
|
185
|
+
"length",
|
|
186
|
+
"mass",
|
|
187
|
+
"time",
|
|
188
|
+
];
|
|
189
|
+
/** The DIMS_VECTOR-ordered exponent string unitsml-ruby uses for lookups. */
|
|
190
|
+
export function dimensionVector(dim) {
|
|
191
|
+
return DIM_COMPONENT_KEYS.map((key) => {
|
|
192
|
+
const component = dim[key];
|
|
193
|
+
return component?.power ?? "";
|
|
194
|
+
}).join(":");
|
|
195
|
+
}
|
|
196
|
+
/** Components in the RECORD's own key order (the unitsdb YAML order,
|
|
197
|
+
* which is what unitsml-ruby's processed_keys preserves). */
|
|
198
|
+
export function dimensionComponents(dim) {
|
|
199
|
+
return Object.keys(dim)
|
|
200
|
+
.filter((key) => key !== "id" && dim[key] !== undefined)
|
|
201
|
+
.map((key) => [key, dim[key]]);
|
|
202
|
+
}
|
|
203
|
+
export function unitNistId(unit) {
|
|
204
|
+
return unit.identifiers?.find((i) => i.type === "nist")?.id;
|
|
205
|
+
}
|
|
206
|
+
export function unitEnName(unit) {
|
|
207
|
+
return unit.names?.find((n) => n.lang === "en")?.value;
|
|
208
|
+
}
|
|
209
|
+
export function prefixNistId(prefix) {
|
|
210
|
+
return prefix.identifiers?.find((i) => i.type === "nist")?.id;
|
|
211
|
+
}
|
|
212
|
+
export function prefixEnName(prefix) {
|
|
213
|
+
return prefix.names?.find((n) => n.lang === "en")?.value;
|
|
214
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unitsdb",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
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
|
+
}
|