unitsdb 0.1.2 → 0.1.3
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 +65 -6
- package/dist/data/unitsdb.json +1 -1
- package/dist/index.d.ts +98 -3
- package/dist/index.js +72 -2
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,34 @@ export interface DerivedBase {
|
|
|
23
23
|
} | null;
|
|
24
24
|
power: string | number | null;
|
|
25
25
|
}
|
|
26
|
+
/** A unit/prefix power pair (canonical_conversion.root_units, root_units). */
|
|
27
|
+
export interface RootUnitRef {
|
|
28
|
+
power: number | null;
|
|
29
|
+
unit_reference: {
|
|
30
|
+
id: string;
|
|
31
|
+
};
|
|
32
|
+
prefix_reference?: {
|
|
33
|
+
id: string;
|
|
34
|
+
} | null;
|
|
35
|
+
}
|
|
36
|
+
/** Schema 2.2.0 canonical conversion: value_root = value * factor + offset.
|
|
37
|
+
* factor/offset are exact decimal or rational strings ("1", "273.15",
|
|
38
|
+
* "5/9") — parse them exactly, never through binary floating point. */
|
|
39
|
+
export interface CanonicalConversion {
|
|
40
|
+
factor: string;
|
|
41
|
+
offset?: string;
|
|
42
|
+
root_units: RootUnitRef[];
|
|
43
|
+
}
|
|
44
|
+
/** An external reference (si-digital-framework, qudt, ucum, ...). */
|
|
45
|
+
export interface ExternalReference {
|
|
46
|
+
type: string;
|
|
47
|
+
authority: string;
|
|
48
|
+
uri: string;
|
|
49
|
+
}
|
|
50
|
+
export interface ScaleReference {
|
|
51
|
+
type: string;
|
|
52
|
+
id: string;
|
|
53
|
+
}
|
|
26
54
|
export interface QuantityReferenceRef {
|
|
27
55
|
id: string;
|
|
28
56
|
primary?: boolean;
|
|
@@ -37,6 +65,13 @@ export interface UnitRecord {
|
|
|
37
65
|
id: string;
|
|
38
66
|
} | null;
|
|
39
67
|
quantity_references?: QuantityReferenceRef[];
|
|
68
|
+
canonical_conversion?: CanonicalConversion;
|
|
69
|
+
short?: string | null;
|
|
70
|
+
root?: boolean | null;
|
|
71
|
+
prefixed?: boolean | null;
|
|
72
|
+
root_units?: RootUnitRef[] | null;
|
|
73
|
+
scale_reference?: ScaleReference | null;
|
|
74
|
+
references?: ExternalReference[] | null;
|
|
40
75
|
}
|
|
41
76
|
export interface PrefixRecord {
|
|
42
77
|
identifiers?: Identifier[];
|
|
@@ -44,6 +79,8 @@ export interface PrefixRecord {
|
|
|
44
79
|
symbols?: UnitSymbol[];
|
|
45
80
|
base?: number;
|
|
46
81
|
power?: number;
|
|
82
|
+
short?: string | null;
|
|
83
|
+
references?: ExternalReference[] | null;
|
|
47
84
|
}
|
|
48
85
|
export interface DimensionRecord {
|
|
49
86
|
id: string | null;
|
|
@@ -56,6 +93,8 @@ export interface QuantityRecord {
|
|
|
56
93
|
id: string;
|
|
57
94
|
} | null;
|
|
58
95
|
quantityType?: string;
|
|
96
|
+
short?: string | null;
|
|
97
|
+
references?: ExternalReference[] | null;
|
|
59
98
|
}
|
|
60
99
|
export interface CurrencySymbol {
|
|
61
100
|
id?: string;
|
|
@@ -73,7 +112,41 @@ export interface CurrencyRecord {
|
|
|
73
112
|
quantity_references?: QuantityReferenceRef[];
|
|
74
113
|
notes?: string | null;
|
|
75
114
|
}
|
|
76
|
-
export
|
|
115
|
+
export interface ScaleRecord {
|
|
116
|
+
identifiers: Identifier[];
|
|
117
|
+
names: LocalizedString[];
|
|
118
|
+
short?: string | null;
|
|
119
|
+
properties: {
|
|
120
|
+
continuous: boolean;
|
|
121
|
+
ordered: boolean;
|
|
122
|
+
logarithmic: boolean;
|
|
123
|
+
interval: boolean;
|
|
124
|
+
ratio: boolean;
|
|
125
|
+
};
|
|
126
|
+
/** Operations the scale permits (addition, subtraction, scaling, ...). */
|
|
127
|
+
operations?: string[] | null;
|
|
128
|
+
}
|
|
129
|
+
/** Which unitsdb release the bundled dataset was projected from, and by
|
|
130
|
+
* which generator — written by scripts/build-dataset.rb. Digests are sha256
|
|
131
|
+
* of the file text with CRLF normalized to LF. */
|
|
132
|
+
export interface DatasetProvenance {
|
|
133
|
+
data: {
|
|
134
|
+
repository: string;
|
|
135
|
+
commit: string;
|
|
136
|
+
tag: string | null;
|
|
137
|
+
schema_version: string;
|
|
138
|
+
files: Record<string, string>;
|
|
139
|
+
schemas: Record<string, string>;
|
|
140
|
+
};
|
|
141
|
+
generator: {
|
|
142
|
+
script: string;
|
|
143
|
+
script_digest: string;
|
|
144
|
+
unitsdb_ruby: string;
|
|
145
|
+
lutaml_model: string | null;
|
|
146
|
+
};
|
|
147
|
+
counts: Record<string, number>;
|
|
148
|
+
}
|
|
149
|
+
declare const database: {
|
|
77
150
|
units: UnitRecord[];
|
|
78
151
|
/** unitsml-ruby `Unitsdb.units.find_by_symbol_id` */
|
|
79
152
|
findUnitBySymbolId(symbolId: string): UnitRecord | undefined;
|
|
@@ -81,6 +154,17 @@ export declare const Unitsdb: {
|
|
|
81
154
|
findUnitByName(name: string): UnitRecord | undefined;
|
|
82
155
|
/** unitsml-ruby `Unitsdb.units.find_by_id` (via identifiers) */
|
|
83
156
|
findUnitById(id: string): UnitRecord | undefined;
|
|
157
|
+
/** unitsml-ruby `Unitsdb.units.find_by_reference_uri` — the unit whose
|
|
158
|
+
* external references include this URI (e.g. an SI digital framework
|
|
159
|
+
* unit URI). */
|
|
160
|
+
findUnitByReferenceUri(uri: string): UnitRecord | undefined;
|
|
161
|
+
scales: ScaleRecord[];
|
|
162
|
+
/** A scale by identifier ("continuous_ratio" or "s:continuous_ratio"). */
|
|
163
|
+
findScaleById(id: string): ScaleRecord | undefined;
|
|
164
|
+
/** The scale a unit's scale_reference names. */
|
|
165
|
+
unitScale(unit: UnitRecord): ScaleRecord | undefined;
|
|
166
|
+
/** Release/generator provenance of the bundled dataset. */
|
|
167
|
+
provenance: DatasetProvenance;
|
|
84
168
|
prefixes: PrefixRecord[];
|
|
85
169
|
/** unitsml-ruby `Unitsdb.prefixes.find_by_symbol_name` */
|
|
86
170
|
findPrefixBySymbolName(ascii: string): PrefixRecord | undefined;
|
|
@@ -102,15 +186,25 @@ export declare const Unitsdb: {
|
|
|
102
186
|
/** unitsml-ruby `Unitsdb.find_by_symbol(_, :currencies)` — unicode sign
|
|
103
187
|
* (€, A$) or ascii alpha code (EUR, AUD), case-insensitive on ascii. */
|
|
104
188
|
findCurrenciesBySymbol(symbol: string): CurrencyRecord[];
|
|
189
|
+
/** Ruby Database#find_by_symbol: units and prefixes match ASCII symbols;
|
|
190
|
+
* currencies also match their exact Unicode sign. Collection order is kept. */
|
|
191
|
+
findBySymbol(symbol: string | null | undefined, entityType?: string | null): Array<UnitRecord | PrefixRecord | CurrencyRecord>;
|
|
192
|
+
/** Compatibility name for the identifier lookup documented by unitsdb-js. */
|
|
193
|
+
getById(id: string, type?: string): UnitRecord | PrefixRecord | DimensionRecord | QuantityRecord | CurrencyRecord | undefined;
|
|
105
194
|
/** unitsml-ruby `Unitsdb.get_by_id` across collections (REQUIRED first,
|
|
106
195
|
* OPTIONAL currencies last; first record wins on collisions). */
|
|
107
196
|
findById(id: string, type?: string): UnitRecord | PrefixRecord | DimensionRecord | QuantityRecord | CurrencyRecord | undefined;
|
|
108
197
|
/** unitsml-ruby `Unitsdb.search` — substring over identifiers and names. */
|
|
109
|
-
search(
|
|
198
|
+
search(query: {
|
|
110
199
|
text?: string;
|
|
111
200
|
type?: string;
|
|
112
|
-
}): Array<UnitRecord | PrefixRecord | DimensionRecord | QuantityRecord | CurrencyRecord>;
|
|
201
|
+
} | string, type?: string): Array<UnitRecord | PrefixRecord | DimensionRecord | QuantityRecord | CurrencyRecord>;
|
|
202
|
+
};
|
|
203
|
+
export type BundledDatabase = typeof database & {
|
|
204
|
+
bundled(): BundledDatabase;
|
|
113
205
|
};
|
|
206
|
+
/** The bundled dataset is loaded once and shared by all callers. */
|
|
207
|
+
export declare const Unitsdb: BundledDatabase;
|
|
114
208
|
/** The DIMS_VECTOR-ordered exponent string unitsml-ruby uses for lookups. */
|
|
115
209
|
export declare function dimensionVector(dim: DimensionRecord): string;
|
|
116
210
|
/** Components in the RECORD's own key order (the unitsdb YAML order,
|
|
@@ -123,3 +217,4 @@ export declare function unitNistId(unit: UnitRecord): string | undefined;
|
|
|
123
217
|
export declare function unitEnName(unit: UnitRecord): string | undefined;
|
|
124
218
|
export declare function prefixNistId(prefix: PrefixRecord): string | undefined;
|
|
125
219
|
export declare function prefixEnName(prefix: PrefixRecord): string | undefined;
|
|
220
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,8 @@ const prefixes = dataset.prefixes;
|
|
|
7
7
|
const dimensions = dataset.dimensions;
|
|
8
8
|
const quantities = dataset.quantities;
|
|
9
9
|
const currencies = dataset.currencies;
|
|
10
|
+
const scales = dataset.scales;
|
|
11
|
+
const provenance = dataset.provenance;
|
|
10
12
|
const unitsBySymbolId = new Map();
|
|
11
13
|
for (const unit of units) {
|
|
12
14
|
for (const symbol of unit.symbols ?? []) {
|
|
@@ -24,6 +26,23 @@ for (const unit of units) {
|
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
}
|
|
29
|
+
// unitsml-ruby `find_by_reference_uri` — first unit carrying the URI.
|
|
30
|
+
const unitsByReferenceUri = new Map();
|
|
31
|
+
for (const unit of units) {
|
|
32
|
+
for (const reference of unit.references ?? []) {
|
|
33
|
+
if (!unitsByReferenceUri.has(reference.uri)) {
|
|
34
|
+
unitsByReferenceUri.set(reference.uri, unit);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const scalesByIdentifier = new Map();
|
|
39
|
+
for (const scale of scales) {
|
|
40
|
+
for (const identifier of scale.identifiers) {
|
|
41
|
+
if (!scalesByIdentifier.has(identifier.id)) {
|
|
42
|
+
scalesByIdentifier.set(identifier.id, scale);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
27
46
|
const prefixesBySymbolName = new Map();
|
|
28
47
|
for (const prefix of prefixes) {
|
|
29
48
|
for (const symbol of prefix.symbols ?? []) {
|
|
@@ -80,7 +99,7 @@ function currencySymbolMatch(currency, symbol) {
|
|
|
80
99
|
const needle = symbol.toLowerCase();
|
|
81
100
|
return (currency.symbols ?? []).some((sym) => sym.unicode === symbol || sym.ascii?.toLowerCase() === needle);
|
|
82
101
|
}
|
|
83
|
-
|
|
102
|
+
const database = {
|
|
84
103
|
units,
|
|
85
104
|
/** unitsml-ruby `Unitsdb.units.find_by_symbol_id` */
|
|
86
105
|
findUnitBySymbolId(symbolId) {
|
|
@@ -94,6 +113,24 @@ export const Unitsdb = {
|
|
|
94
113
|
findUnitById(id) {
|
|
95
114
|
return unitsByIdentifier.get(id);
|
|
96
115
|
},
|
|
116
|
+
/** unitsml-ruby `Unitsdb.units.find_by_reference_uri` — the unit whose
|
|
117
|
+
* external references include this URI (e.g. an SI digital framework
|
|
118
|
+
* unit URI). */
|
|
119
|
+
findUnitByReferenceUri(uri) {
|
|
120
|
+
return unitsByReferenceUri.get(uri);
|
|
121
|
+
},
|
|
122
|
+
scales,
|
|
123
|
+
/** A scale by identifier ("continuous_ratio" or "s:continuous_ratio"). */
|
|
124
|
+
findScaleById(id) {
|
|
125
|
+
return scalesByIdentifier.get(id);
|
|
126
|
+
},
|
|
127
|
+
/** The scale a unit's scale_reference names. */
|
|
128
|
+
unitScale(unit) {
|
|
129
|
+
const id = unit.scale_reference?.id;
|
|
130
|
+
return id === undefined ? undefined : scalesByIdentifier.get(id);
|
|
131
|
+
},
|
|
132
|
+
/** Release/generator provenance of the bundled dataset. */
|
|
133
|
+
provenance,
|
|
97
134
|
prefixes,
|
|
98
135
|
/** unitsml-ruby `Unitsdb.prefixes.find_by_symbol_name` */
|
|
99
136
|
findPrefixBySymbolName(ascii) {
|
|
@@ -136,6 +173,34 @@ export const Unitsdb = {
|
|
|
136
173
|
findCurrenciesBySymbol(symbol) {
|
|
137
174
|
return currencies.filter((currency) => currencySymbolMatch(currency, symbol));
|
|
138
175
|
},
|
|
176
|
+
/** Ruby Database#find_by_symbol: units and prefixes match ASCII symbols;
|
|
177
|
+
* currencies also match their exact Unicode sign. Collection order is kept. */
|
|
178
|
+
findBySymbol(symbol, entityType) {
|
|
179
|
+
if (symbol == null)
|
|
180
|
+
return [];
|
|
181
|
+
const collections = { units, prefixes, currencies };
|
|
182
|
+
const scope = entityType == null
|
|
183
|
+
? ["units", "prefixes", "currencies"]
|
|
184
|
+
: [entityType];
|
|
185
|
+
const needle = symbol.toLowerCase();
|
|
186
|
+
const matches = [];
|
|
187
|
+
for (const name of scope) {
|
|
188
|
+
if (name !== "units" && name !== "prefixes" && name !== "currencies") {
|
|
189
|
+
throw new RangeError(`unknown collection: ${name}`);
|
|
190
|
+
}
|
|
191
|
+
for (const record of collections[name]) {
|
|
192
|
+
if ((record.symbols ?? []).some((sym) => (sym.ascii ?? "").toLowerCase() === needle ||
|
|
193
|
+
(name === "currencies" && (sym.unicode ?? "") === symbol))) {
|
|
194
|
+
matches.push(record);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return matches;
|
|
199
|
+
},
|
|
200
|
+
/** Compatibility name for the identifier lookup documented by unitsdb-js. */
|
|
201
|
+
getById(id, type) {
|
|
202
|
+
return database.findById(id, type);
|
|
203
|
+
},
|
|
139
204
|
/** unitsml-ruby `Unitsdb.get_by_id` across collections (REQUIRED first,
|
|
140
205
|
* OPTIONAL currencies last; first record wins on collisions). */
|
|
141
206
|
findById(id, type) {
|
|
@@ -147,7 +212,8 @@ export const Unitsdb = {
|
|
|
147
212
|
currencies.find(matchIdentifier));
|
|
148
213
|
},
|
|
149
214
|
/** unitsml-ruby `Unitsdb.search` — substring over identifiers and names. */
|
|
150
|
-
search(
|
|
215
|
+
search(query, type) {
|
|
216
|
+
const params = typeof query === "string" ? { text: query, type } : query;
|
|
151
217
|
const text = params.text;
|
|
152
218
|
if (!text)
|
|
153
219
|
return [];
|
|
@@ -174,6 +240,10 @@ export const Unitsdb = {
|
|
|
174
240
|
return results;
|
|
175
241
|
},
|
|
176
242
|
};
|
|
243
|
+
/** The bundled dataset is loaded once and shared by all callers. */
|
|
244
|
+
export const Unitsdb = Object.assign(database, {
|
|
245
|
+
bundled: () => Unitsdb,
|
|
246
|
+
});
|
|
177
247
|
// DIMS_VECTOR order (unitsml-ruby Utility::DIMS_VECTOR) — the vector
|
|
178
248
|
// string fed to unitsdb dimension lookups depends on it.
|
|
179
249
|
const DIM_COMPONENT_KEYS = [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unitsdb",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "UnitsDB in TypeScript: typed access to the units database (units, prefixes, dimensions, quantities, currencies) rebuilt from unitsml/unitsdb releases",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"build": "tsc -p tsconfig.json",
|
|
19
19
|
"prepare": "npm run build",
|
|
20
20
|
"test": "node --test",
|
|
21
|
+
"test:package": "npm run build && node scripts/smoke-package.mjs",
|
|
21
22
|
"dataset": "ruby scripts/build-dataset.rb > /dev/null && cp /tmp/unitsdb-ts.json src/data/unitsdb.json"
|
|
22
23
|
},
|
|
23
24
|
"license": "BSD-2-Clause",
|