unitsdb 0.1.0 → 0.1.2

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/dist/index.d.ts CHANGED
@@ -23,6 +23,10 @@ export interface DerivedBase {
23
23
  } | null;
24
24
  power: string | number | null;
25
25
  }
26
+ export interface QuantityReferenceRef {
27
+ id: string;
28
+ primary?: boolean;
29
+ }
26
30
  export interface UnitRecord {
27
31
  identifiers?: Identifier[];
28
32
  names?: LocalizedString[];
@@ -32,7 +36,7 @@ export interface UnitRecord {
32
36
  dimension_reference?: {
33
37
  id: string;
34
38
  } | null;
35
- quantity_references?: Identifier[];
39
+ quantity_references?: QuantityReferenceRef[];
36
40
  }
37
41
  export interface PrefixRecord {
38
42
  identifiers?: Identifier[];
@@ -53,6 +57,22 @@ export interface QuantityRecord {
53
57
  } | null;
54
58
  quantityType?: string;
55
59
  }
60
+ export interface CurrencySymbol {
61
+ id?: string;
62
+ ascii?: string;
63
+ unicode?: string;
64
+ }
65
+ export interface CurrencyRecord {
66
+ identifiers?: Identifier[];
67
+ names?: LocalizedString[];
68
+ symbols?: CurrencySymbol[];
69
+ /** ISO 4217 "N.A." minor units are absent — the key is omitted, never 0. */
70
+ minor_unit_exponent?: number;
71
+ status?: string;
72
+ unit_system_reference?: Identifier[];
73
+ quantity_references?: QuantityReferenceRef[];
74
+ notes?: string | null;
75
+ }
56
76
  export declare const Unitsdb: {
57
77
  units: UnitRecord[];
58
78
  /** unitsml-ruby `Unitsdb.units.find_by_symbol_id` */
@@ -74,6 +94,22 @@ export declare const Unitsdb: {
74
94
  findDimensionByVector(vector: string): DimensionRecord | undefined;
75
95
  quantities: QuantityRecord[];
76
96
  findQuantityById(id: string): QuantityRecord | undefined;
97
+ currencies: CurrencyRecord[];
98
+ /** unitsml-ruby `Unitsdb.currencies.find_by_code` (case-insensitive alpha) */
99
+ findCurrencyByCode(code: string): CurrencyRecord | undefined;
100
+ /** unitsml-ruby `Unitsdb.currencies.find_by_numeric` (3-digit, base-10) */
101
+ findCurrencyByNumeric(numeric: string | number): CurrencyRecord | undefined;
102
+ /** unitsml-ruby `Unitsdb.find_by_symbol(_, :currencies)` — unicode sign
103
+ * (€, A$) or ascii alpha code (EUR, AUD), case-insensitive on ascii. */
104
+ findCurrenciesBySymbol(symbol: string): CurrencyRecord[];
105
+ /** unitsml-ruby `Unitsdb.get_by_id` across collections (REQUIRED first,
106
+ * OPTIONAL currencies last; first record wins on collisions). */
107
+ findById(id: string, type?: string): UnitRecord | PrefixRecord | DimensionRecord | QuantityRecord | CurrencyRecord | undefined;
108
+ /** unitsml-ruby `Unitsdb.search` — substring over identifiers and names. */
109
+ search(params: {
110
+ text?: string;
111
+ type?: string;
112
+ }): Array<UnitRecord | PrefixRecord | DimensionRecord | QuantityRecord | CurrencyRecord>;
77
113
  };
78
114
  /** The DIMS_VECTOR-ordered exponent string unitsml-ruby uses for lookups. */
79
115
  export declare function dimensionVector(dim: DimensionRecord): string;
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ const units = dataset.units;
6
6
  const prefixes = dataset.prefixes;
7
7
  const dimensions = dataset.dimensions;
8
8
  const quantities = dataset.quantities;
9
+ const currencies = dataset.currencies;
9
10
  const unitsBySymbolId = new Map();
10
11
  for (const unit of units) {
11
12
  for (const symbol of unit.symbols ?? []) {
@@ -57,6 +58,28 @@ for (const quantity of quantities) {
57
58
  }
58
59
  }
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
+ }
60
83
  export const Unitsdb = {
61
84
  units,
62
85
  /** unitsml-ruby `Unitsdb.units.find_by_symbol_id` */
@@ -96,6 +119,60 @@ export const Unitsdb = {
96
119
  findQuantityById(id) {
97
120
  return quantitiesById.get(id) ?? quantitiesByIdentifier.get(id);
98
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
+ },
99
176
  };
100
177
  // DIMS_VECTOR order (unitsml-ruby Utility::DIMS_VECTOR) — the vector
101
178
  // string fed to unitsdb dimension lookups depends on it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unitsdb",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
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",