@bcts/known-values 1.0.0-alpha.5

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.
@@ -0,0 +1,317 @@
1
+ import { KnownValue } from "./known-value";
2
+ import { KnownValuesStore } from "./known-values-store";
3
+
4
+ // For definitions see: https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2023-002-known-value.md#appendix-a-registry
5
+
6
+ //
7
+ // General
8
+ //
9
+
10
+ export const UNIT = new KnownValue(0, "");
11
+ export const IS_A = new KnownValue(1, "isA");
12
+ export const ID = new KnownValue(2, "id");
13
+ export const SIGNED = new KnownValue(3, "signed");
14
+ export const NOTE = new KnownValue(4, "note");
15
+ export const HAS_RECIPIENT = new KnownValue(5, "hasRecipient");
16
+ export const SSKR_SHARE = new KnownValue(6, "sskrShare");
17
+ export const CONTROLLER = new KnownValue(7, "controller");
18
+ export const KEY = new KnownValue(8, "key");
19
+ export const DEREFERENCE_VIA = new KnownValue(9, "dereferenceVia");
20
+ export const ENTITY = new KnownValue(10, "entity");
21
+ export const NAME = new KnownValue(11, "name");
22
+ export const LANGUAGE = new KnownValue(12, "language");
23
+ export const ISSUER = new KnownValue(13, "issuer");
24
+ export const HOLDER = new KnownValue(14, "holder");
25
+ export const SALT = new KnownValue(15, "salt");
26
+ export const DATE = new KnownValue(16, "date");
27
+ export const UNKNOWN_VALUE = new KnownValue(17, "Unknown");
28
+ export const VERSION_VALUE = new KnownValue(18, "version");
29
+ export const HAS_SECRET = new KnownValue(19, "hasSecret");
30
+ export const DIFF_EDITS = new KnownValue(20, "edits");
31
+ export const VALID_FROM = new KnownValue(21, "validFrom");
32
+ export const VALID_UNTIL = new KnownValue(22, "validUntil");
33
+ export const POSITION = new KnownValue(23, "position");
34
+ export const NICKNAME = new KnownValue(24, "nickname");
35
+ // 25-49 *unassigned*
36
+
37
+ //
38
+ // Attachments
39
+ //
40
+
41
+ export const ATTACHMENT = new KnownValue(50, "attachment");
42
+ export const VENDOR = new KnownValue(51, "vendor");
43
+ export const CONFORMS_TO = new KnownValue(52, "conformsTo");
44
+ // 53-59 *unassigned*
45
+
46
+ //
47
+ // XID Documents
48
+ //
49
+
50
+ export const ALLOW = new KnownValue(60, "allow");
51
+ export const DENY = new KnownValue(61, "deny");
52
+ export const ENDPOINT = new KnownValue(62, "endpoint");
53
+ export const DELEGATE = new KnownValue(63, "delegate");
54
+ export const PROVENANCE = new KnownValue(64, "provenance");
55
+ export const PRIVATE_KEY = new KnownValue(65, "privateKey");
56
+ export const SERVICE = new KnownValue(66, "service");
57
+ export const CAPABILITY = new KnownValue(67, "capability");
58
+ export const PROVENANCE_GENERATOR = new KnownValue(68, "provenanceGenerator");
59
+ // 68-69 *unassigned*
60
+
61
+ //
62
+ // XID Privileges
63
+ //
64
+
65
+ export const PRIVILEGE_ALL = new KnownValue(70, "All");
66
+ export const PRIVILEGE_AUTH = new KnownValue(71, "Auth");
67
+ export const PRIVILEGE_SIGN = new KnownValue(72, "Sign");
68
+ export const PRIVILEGE_ENCRYPT = new KnownValue(73, "Encrypt");
69
+ export const PRIVILEGE_ELIDE = new KnownValue(74, "Elide");
70
+ export const PRIVILEGE_ISSUE = new KnownValue(75, "Issue");
71
+ export const PRIVILEGE_ACCESS = new KnownValue(76, "Access");
72
+ // 77-79 *unassigned*
73
+ export const PRIVILEGE_DELEGATE = new KnownValue(80, "Delegate");
74
+ export const PRIVILEGE_VERIFY = new KnownValue(81, "Verify");
75
+ export const PRIVILEGE_UPDATE = new KnownValue(82, "Update");
76
+ export const PRIVILEGE_TRANSFER = new KnownValue(83, "Transfer");
77
+ export const PRIVILEGE_ELECT = new KnownValue(84, "Elect");
78
+ export const PRIVILEGE_BURN = new KnownValue(85, "Burn");
79
+ export const PRIVILEGE_REVOKE = new KnownValue(86, "Revoke");
80
+ // 87-99 *unassigned*
81
+
82
+ //
83
+ // Expression and Function Calls
84
+ //
85
+
86
+ export const BODY = new KnownValue(100, "body");
87
+ export const RESULT = new KnownValue(101, "result");
88
+ export const ERROR = new KnownValue(102, "error");
89
+ export const OK_VALUE = new KnownValue(103, "OK");
90
+ export const PROCESSING_VALUE = new KnownValue(104, "Processing");
91
+ export const SENDER = new KnownValue(105, "sender");
92
+ export const SENDER_CONTINUATION = new KnownValue(106, "senderContinuation");
93
+ export const RECIPIENT_CONTINUATION = new KnownValue(107, "recipientContinuation");
94
+ export const CONTENT = new KnownValue(108, "content");
95
+ // 109-199 *unassigned*
96
+
97
+ //
98
+ // Cryptography
99
+ //
100
+
101
+ export const SEED_TYPE = new KnownValue(200, "Seed");
102
+ export const PRIVATE_KEY_TYPE = new KnownValue(201, "PrivateKey");
103
+ export const PUBLIC_KEY_TYPE = new KnownValue(202, "PublicKey");
104
+ export const MASTER_KEY_TYPE = new KnownValue(203, "MasterKey");
105
+ // 204-299 *unassigned*
106
+
107
+ //
108
+ // Cryptocurrency Assets
109
+ //
110
+
111
+ export const ASSET = new KnownValue(300, "asset");
112
+ export const BITCOIN_VALUE = new KnownValue(301, "BTC");
113
+ export const ETHEREUM_VALUE = new KnownValue(302, "ETH");
114
+ export const TEZOS_VALUE = new KnownValue(303, "XTZ");
115
+ // 304-399 *unassigned*
116
+
117
+ //
118
+ // Cryptocurrency Networks
119
+ //
120
+
121
+ export const NETWORK = new KnownValue(400, "network");
122
+ export const MAIN_NET_VALUE = new KnownValue(401, "MainNet");
123
+ export const TEST_NET_VALUE = new KnownValue(402, "TestNet");
124
+ // 403-499 *unassigned*
125
+
126
+ //
127
+ // Bitcoin
128
+ //
129
+
130
+ export const BIP32_KEY_TYPE = new KnownValue(500, "BIP32Key");
131
+ export const CHAIN_CODE = new KnownValue(501, "chainCode");
132
+ export const DERIVATION_PATH_TYPE = new KnownValue(502, "DerivationPath");
133
+ export const PARENT_PATH = new KnownValue(503, "parent");
134
+ export const CHILDREN_PATH = new KnownValue(504, "children");
135
+ export const PARENT_FINGERPRINT = new KnownValue(505, "parentFingerprint");
136
+ export const PSBT_TYPE = new KnownValue(506, "PSBT");
137
+ export const OUTPUT_DESCRIPTOR_TYPE = new KnownValue(507, "OutputDescriptor");
138
+ export const OUTPUT_DESCRIPTOR = new KnownValue(508, "outputDescriptor");
139
+ // 509-599 *unassigned*
140
+
141
+ //
142
+ // Graphs
143
+ //
144
+
145
+ export const GRAPH = new KnownValue(600, "graph");
146
+ export const SOURCE_TARGET_GRAPH = new KnownValue(601, "SourceTargetGraph");
147
+ export const PARENT_CHILD_GRAPH = new KnownValue(602, "ParentChildGraph");
148
+ export const DIGRAPH = new KnownValue(603, "Digraph");
149
+ export const ACYCLIC_GRAPH = new KnownValue(604, "AcyclicGraph");
150
+ export const MULTIGRAPH = new KnownValue(605, "Multigraph");
151
+ export const PSEUDOGRAPH = new KnownValue(606, "Pseudograph");
152
+ export const GRAPH_FRAGMENT = new KnownValue(607, "GraphFragment");
153
+ export const DAG = new KnownValue(608, "DAG");
154
+ export const TREE = new KnownValue(609, "Tree");
155
+ export const FOREST = new KnownValue(610, "Forest");
156
+ export const COMPOUND_GRAPH = new KnownValue(611, "CompoundGraph");
157
+ export const HYPERGRAPH = new KnownValue(612, "Hypergraph");
158
+ export const DIHYPERGRAPH = new KnownValue(613, "Dihypergraph");
159
+ // 614-699 *unassigned*
160
+ export const NODE = new KnownValue(700, "node");
161
+ export const EDGE = new KnownValue(701, "edge");
162
+ export const SOURCE = new KnownValue(702, "source");
163
+ export const TARGET = new KnownValue(703, "target");
164
+ export const PARENT = new KnownValue(704, "parent");
165
+ export const CHILD = new KnownValue(705, "child");
166
+ // 706-... *unassigned*
167
+
168
+ /**
169
+ * A lazily initialized singleton that holds the global registry of known
170
+ * values.
171
+ *
172
+ * This class provides thread-safe, lazy initialization of the global
173
+ * KnownValuesStore that contains all the predefined Known Values in the
174
+ * registry. The store is created only when first accessed, and subsequent
175
+ * accesses reuse the same instance.
176
+ *
177
+ * This is used internally by the crate and should not typically be needed by
178
+ * users of the API, who should access Known Values through the constants
179
+ * exposed in the `known_values` module.
180
+ */
181
+ class LazyKnownValues {
182
+ private _data: KnownValuesStore | undefined;
183
+
184
+ /**
185
+ * Gets the global KnownValuesStore, initializing it if necessary.
186
+ *
187
+ * This method guarantees that initialization occurs exactly once.
188
+ */
189
+ get(): KnownValuesStore {
190
+ this._data ??= new KnownValuesStore([
191
+ UNIT,
192
+ IS_A,
193
+ ID,
194
+ SIGNED,
195
+ NOTE,
196
+ HAS_RECIPIENT,
197
+ SSKR_SHARE,
198
+ CONTROLLER,
199
+ KEY,
200
+ DEREFERENCE_VIA,
201
+ ENTITY,
202
+ NAME,
203
+ LANGUAGE,
204
+ ISSUER,
205
+ HOLDER,
206
+ SALT,
207
+ DATE,
208
+ UNKNOWN_VALUE,
209
+ VERSION_VALUE,
210
+ HAS_SECRET,
211
+ DIFF_EDITS,
212
+ VALID_FROM,
213
+ VALID_UNTIL,
214
+ POSITION,
215
+ NICKNAME,
216
+ ATTACHMENT,
217
+ VENDOR,
218
+ CONFORMS_TO,
219
+ ALLOW,
220
+ DENY,
221
+ ENDPOINT,
222
+ DELEGATE,
223
+ PROVENANCE,
224
+ PRIVATE_KEY,
225
+ SERVICE,
226
+ CAPABILITY,
227
+ PROVENANCE_GENERATOR,
228
+ PRIVILEGE_ALL,
229
+ PRIVILEGE_AUTH,
230
+ PRIVILEGE_SIGN,
231
+ PRIVILEGE_ENCRYPT,
232
+ PRIVILEGE_ELIDE,
233
+ PRIVILEGE_ISSUE,
234
+ PRIVILEGE_ACCESS,
235
+ PRIVILEGE_DELEGATE,
236
+ PRIVILEGE_VERIFY,
237
+ PRIVILEGE_UPDATE,
238
+ PRIVILEGE_TRANSFER,
239
+ PRIVILEGE_ELECT,
240
+ PRIVILEGE_BURN,
241
+ PRIVILEGE_REVOKE,
242
+ BODY,
243
+ RESULT,
244
+ ERROR,
245
+ OK_VALUE,
246
+ PROCESSING_VALUE,
247
+ SENDER,
248
+ SENDER_CONTINUATION,
249
+ RECIPIENT_CONTINUATION,
250
+ CONTENT,
251
+ SEED_TYPE,
252
+ PRIVATE_KEY_TYPE,
253
+ PUBLIC_KEY_TYPE,
254
+ MASTER_KEY_TYPE,
255
+ ASSET,
256
+ BITCOIN_VALUE,
257
+ ETHEREUM_VALUE,
258
+ TEZOS_VALUE,
259
+ NETWORK,
260
+ MAIN_NET_VALUE,
261
+ TEST_NET_VALUE,
262
+ BIP32_KEY_TYPE,
263
+ CHAIN_CODE,
264
+ DERIVATION_PATH_TYPE,
265
+ PARENT_PATH,
266
+ CHILDREN_PATH,
267
+ PARENT_FINGERPRINT,
268
+ PSBT_TYPE,
269
+ OUTPUT_DESCRIPTOR_TYPE,
270
+ OUTPUT_DESCRIPTOR,
271
+ GRAPH,
272
+ SOURCE_TARGET_GRAPH,
273
+ PARENT_CHILD_GRAPH,
274
+ DIGRAPH,
275
+ ACYCLIC_GRAPH,
276
+ MULTIGRAPH,
277
+ PSEUDOGRAPH,
278
+ GRAPH_FRAGMENT,
279
+ DAG,
280
+ TREE,
281
+ FOREST,
282
+ COMPOUND_GRAPH,
283
+ HYPERGRAPH,
284
+ DIHYPERGRAPH,
285
+ NODE,
286
+ EDGE,
287
+ SOURCE,
288
+ TARGET,
289
+ PARENT,
290
+ CHILD,
291
+ ]);
292
+ return this._data;
293
+ }
294
+ }
295
+
296
+ /**
297
+ * The global registry of Known Values.
298
+ *
299
+ * This static instance provides access to all standard Known Values defined in
300
+ * the registry specification. It is lazily initialized on first access.
301
+ *
302
+ * Most users should not need to interact with this directly, as the predefined
303
+ * Known Values are exposed as constants in the `known_values` module.
304
+ *
305
+ * @example
306
+ * ```typescript
307
+ * import { KNOWN_VALUES } from '@bcts/known-values';
308
+ *
309
+ * // Access the global store
310
+ * const knownValues = KNOWN_VALUES.get();
311
+ *
312
+ * // Look up a Known Value by name
313
+ * const isA = knownValues.knownValueNamed('isA');
314
+ * console.log(isA?.value()); // 1
315
+ * ```
316
+ */
317
+ export const KNOWN_VALUES = new LazyKnownValues();
@@ -0,0 +1,327 @@
1
+ import { KnownValue, type KnownValueInput } from "./known-value";
2
+
3
+ /**
4
+ * A store that maps between Known Values and their assigned names.
5
+ *
6
+ * The `KnownValuesStore` provides a bidirectional mapping between:
7
+ * - Numeric values (bigint) and their corresponding KnownValue instances
8
+ * - String names and their corresponding KnownValue instances
9
+ *
10
+ * This enables efficient lookup in both directions, making it possible to:
11
+ * - Find the name for a given numeric value
12
+ * - Find the numeric value for a given name
13
+ * - Retrieve complete KnownValue instances by either name or value
14
+ *
15
+ * The store is typically populated with predefined Known Values from the
16
+ * registry, but can also be extended with custom values.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * import { KnownValuesStore, IS_A, NOTE, SIGNED } from '@bcts/known-values';
21
+ *
22
+ * // Create a store with predefined Known Values
23
+ * const store = new KnownValuesStore([IS_A, NOTE, SIGNED]);
24
+ *
25
+ * // Look up a Known Value by name
26
+ * const isA = store.knownValueNamed('isA');
27
+ * console.log(isA?.value()); // 1
28
+ *
29
+ * // Look up a name for a raw value
30
+ * const name = store.name(new KnownValue(3));
31
+ * console.log(name); // "signed"
32
+ *
33
+ * // Insert a custom Known Value
34
+ * const customStore = store.clone();
35
+ * customStore.insert(new KnownValue(100, 'customValue'));
36
+ * console.log(customStore.knownValueNamed('customValue')?.value()); // 100
37
+ * ```
38
+ */
39
+ export class KnownValuesStore {
40
+ // Use bigint for map keys to support full 64-bit range
41
+ private knownValuesByRawValue: Map<bigint, KnownValue>;
42
+ private knownValuesByAssignedName: Map<string, KnownValue>;
43
+
44
+ /**
45
+ * Creates a new KnownValuesStore with the provided Known Values.
46
+ *
47
+ * This constructor takes an iterable of KnownValue instances and
48
+ * populates the store with them, creating mappings from both raw
49
+ * values and names to the corresponding KnownValue instances.
50
+ *
51
+ * @param knownValues - Iterable of KnownValue instances to populate the store
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * import { KnownValuesStore, IS_A, NOTE, SIGNED } from '@bcts/known-values';
56
+ *
57
+ * // Create a store with predefined Known Values
58
+ * const store = new KnownValuesStore([IS_A, NOTE, SIGNED]);
59
+ *
60
+ * // Look up Known Values
61
+ * console.log(store.knownValueNamed('isA')?.value()); // 1
62
+ * console.log(store.knownValueNamed('note')?.value()); // 4
63
+ * ```
64
+ */
65
+ constructor(knownValues: Iterable<KnownValue> = []) {
66
+ this.knownValuesByRawValue = new Map();
67
+ this.knownValuesByAssignedName = new Map();
68
+
69
+ for (const knownValue of knownValues) {
70
+ this._insert(knownValue);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Inserts a KnownValue into the store.
76
+ *
77
+ * If the KnownValue has an assigned name, it will be indexed by both its
78
+ * raw value and its name. If a KnownValue with the same raw value or name
79
+ * already exists in the store, it will be replaced.
80
+ *
81
+ * @param knownValue - The KnownValue to insert
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * import { KnownValuesStore, KnownValue } from '@bcts/known-values';
86
+ *
87
+ * const store = new KnownValuesStore();
88
+ * store.insert(new KnownValue(100, 'customValue'));
89
+ * console.log(store.knownValueNamed('customValue')?.value()); // 100
90
+ * ```
91
+ */
92
+ insert(knownValue: KnownValue): void {
93
+ this._insert(knownValue);
94
+ }
95
+
96
+ /**
97
+ * Returns the assigned name for a KnownValue, if present in the store.
98
+ *
99
+ * @param knownValue - The KnownValue to look up
100
+ * @returns The assigned name, or undefined if not found
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * import { KnownValuesStore, IS_A } from '@bcts/known-values';
105
+ *
106
+ * const store = new KnownValuesStore([IS_A]);
107
+ * console.log(store.assignedName(IS_A)); // "isA"
108
+ * console.log(store.assignedName(new KnownValue(999))); // undefined
109
+ * ```
110
+ */
111
+ assignedName(knownValue: KnownValue): string | undefined {
112
+ return this.knownValuesByRawValue.get(knownValue.valueBigInt())?.assignedName();
113
+ }
114
+
115
+ /**
116
+ * Returns a human-readable name for a KnownValue.
117
+ *
118
+ * If the KnownValue has an assigned name in the store, that name is
119
+ * returned. Otherwise, the KnownValue's default name (which may be its
120
+ * numeric value as a string) is returned.
121
+ *
122
+ * @param knownValue - The KnownValue to get the name for
123
+ * @returns The name (assigned or numeric)
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * import { KnownValuesStore, IS_A } from '@bcts/known-values';
128
+ *
129
+ * const store = new KnownValuesStore([IS_A]);
130
+ * console.log(store.name(IS_A)); // "isA"
131
+ * console.log(store.name(new KnownValue(999))); // "999"
132
+ * ```
133
+ */
134
+ name(knownValue: KnownValue): string {
135
+ const assignedName = this.assignedName(knownValue);
136
+ return assignedName ?? knownValue.name();
137
+ }
138
+
139
+ /**
140
+ * Looks up a KnownValue by its assigned name.
141
+ *
142
+ * Returns the KnownValue if found, or undefined if no KnownValue
143
+ * with the given name exists in the store.
144
+ *
145
+ * @param assignedName - The name to look up
146
+ * @returns The KnownValue, or undefined if not found
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * import { KnownValuesStore, IS_A } from '@bcts/known-values';
151
+ *
152
+ * const store = new KnownValuesStore([IS_A]);
153
+ *
154
+ * const isA = store.knownValueNamed('isA');
155
+ * console.log(isA?.value()); // 1
156
+ *
157
+ * console.log(store.knownValueNamed('nonexistent')); // undefined
158
+ * ```
159
+ */
160
+ knownValueNamed(assignedName: string): KnownValue | undefined {
161
+ return this.knownValuesByAssignedName.get(assignedName);
162
+ }
163
+
164
+ /**
165
+ * Looks up a KnownValue by its raw numeric value.
166
+ *
167
+ * @param rawValue - The numeric value to look up (number or bigint)
168
+ * @returns The KnownValue, or undefined if not found
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * import { KnownValuesStore, IS_A } from '@bcts/known-values';
173
+ *
174
+ * const store = new KnownValuesStore([IS_A]);
175
+ * const isA = store.knownValueForValue(1);
176
+ * console.log(isA?.name()); // "isA"
177
+ * ```
178
+ */
179
+ knownValueForValue(rawValue: KnownValueInput): KnownValue | undefined {
180
+ const key = typeof rawValue === "bigint" ? rawValue : BigInt(rawValue);
181
+ return this.knownValuesByRawValue.get(key);
182
+ }
183
+
184
+ /**
185
+ * Retrieves a KnownValue for a raw value, using a store if provided.
186
+ *
187
+ * This static method allows looking up a KnownValue by its raw numeric
188
+ * value:
189
+ * - If a store is provided and contains a mapping for the raw value, that
190
+ * KnownValue is returned
191
+ * - Otherwise, a new KnownValue with no assigned name is created and
192
+ * returned
193
+ *
194
+ * @param rawValue - The numeric value to look up (number or bigint)
195
+ * @param knownValues - Optional store to search in
196
+ * @returns The KnownValue from the store or a new unnamed KnownValue
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * import { KnownValuesStore, IS_A } from '@bcts/known-values';
201
+ *
202
+ * const store = new KnownValuesStore([IS_A]);
203
+ *
204
+ * // Known value from store
205
+ * const isA = KnownValuesStore.knownValueForRawValue(1, store);
206
+ * console.log(isA.name()); // "isA"
207
+ *
208
+ * // Unknown value creates a new KnownValue
209
+ * const unknown = KnownValuesStore.knownValueForRawValue(999, store);
210
+ * console.log(unknown.name()); // "999"
211
+ *
212
+ * // No store provided also creates a new KnownValue
213
+ * const unknown2 = KnownValuesStore.knownValueForRawValue(1, undefined);
214
+ * console.log(unknown2.name()); // "1"
215
+ * ```
216
+ */
217
+ static knownValueForRawValue(
218
+ rawValue: KnownValueInput,
219
+ knownValues?: KnownValuesStore,
220
+ ): KnownValue {
221
+ if (knownValues !== undefined) {
222
+ const value = knownValues.knownValueForValue(rawValue);
223
+ if (value !== undefined) {
224
+ return value;
225
+ }
226
+ }
227
+ return new KnownValue(rawValue);
228
+ }
229
+
230
+ /**
231
+ * Attempts to find a KnownValue by its name, using a store if provided.
232
+ *
233
+ * This static method allows looking up a KnownValue by its name:
234
+ * - If a store is provided and contains a mapping for the name, that
235
+ * KnownValue is returned
236
+ * - Otherwise, undefined is returned
237
+ *
238
+ * @param name - The name to look up
239
+ * @param knownValues - Optional store to search in
240
+ * @returns The KnownValue if found, or undefined
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * import { KnownValuesStore, IS_A } from '@bcts/known-values';
245
+ *
246
+ * const store = new KnownValuesStore([IS_A]);
247
+ *
248
+ * // Known value from store
249
+ * const isA = KnownValuesStore.knownValueForName('isA', store);
250
+ * console.log(isA?.value()); // 1
251
+ *
252
+ * // Unknown name returns undefined
253
+ * console.log(KnownValuesStore.knownValueForName('unknown', store)); // undefined
254
+ *
255
+ * // No store provided also returns undefined
256
+ * console.log(KnownValuesStore.knownValueForName('isA', undefined)); // undefined
257
+ * ```
258
+ */
259
+ static knownValueForName(name: string, knownValues?: KnownValuesStore): KnownValue | undefined {
260
+ return knownValues?.knownValueNamed(name);
261
+ }
262
+
263
+ /**
264
+ * Returns a human-readable name for a KnownValue, using a store if provided.
265
+ *
266
+ * This static method allows getting a name for a KnownValue:
267
+ * - If a store is provided and contains a mapping for the KnownValue, its
268
+ * assigned name is returned
269
+ * - Otherwise, the KnownValue's default name (which may be its numeric
270
+ * value as a string) is returned
271
+ *
272
+ * @param knownValue - The KnownValue to get the name for
273
+ * @param knownValues - Optional store to use for lookup
274
+ * @returns The name (assigned or numeric)
275
+ *
276
+ * @example
277
+ * ```typescript
278
+ * import { KnownValuesStore, IS_A } from '@bcts/known-values';
279
+ *
280
+ * const store = new KnownValuesStore([IS_A]);
281
+ *
282
+ * // Known value from store
283
+ * let name = KnownValuesStore.nameForKnownValue(IS_A, store);
284
+ * console.log(name); // "isA"
285
+ *
286
+ * // Unknown value in store uses KnownValue's name method
287
+ * name = KnownValuesStore.nameForKnownValue(new KnownValue(999), store);
288
+ * console.log(name); // "999"
289
+ *
290
+ * // No store provided also uses KnownValue's name method
291
+ * name = KnownValuesStore.nameForKnownValue(IS_A, undefined);
292
+ * console.log(name); // "isA"
293
+ * ```
294
+ */
295
+ static nameForKnownValue(knownValue: KnownValue, knownValues?: KnownValuesStore): string {
296
+ if (knownValues !== undefined) {
297
+ const assignedName = knownValues.assignedName(knownValue);
298
+ if (assignedName !== undefined && assignedName !== "") {
299
+ return assignedName;
300
+ }
301
+ }
302
+ return knownValue.name();
303
+ }
304
+
305
+ /**
306
+ * Creates a shallow clone of this store.
307
+ *
308
+ * @returns A new KnownValuesStore with the same entries
309
+ */
310
+ clone(): KnownValuesStore {
311
+ const cloned = new KnownValuesStore();
312
+ cloned.knownValuesByRawValue = new Map(this.knownValuesByRawValue);
313
+ cloned.knownValuesByAssignedName = new Map(this.knownValuesByAssignedName);
314
+ return cloned;
315
+ }
316
+
317
+ /**
318
+ * Internal helper method to insert a KnownValue into the store's maps.
319
+ */
320
+ private _insert(knownValue: KnownValue): void {
321
+ this.knownValuesByRawValue.set(knownValue.valueBigInt(), knownValue);
322
+ const assignedName = knownValue.assignedName();
323
+ if (assignedName !== undefined && assignedName !== "") {
324
+ this.knownValuesByAssignedName.set(assignedName, knownValue);
325
+ }
326
+ }
327
+ }