Creates a new KnownValuesStore with the provided Known Values.
This constructor takes an iterable of KnownValue instances and populates the store with them, creating mappings from both raw values and names to the corresponding KnownValue instances.
Iterable of KnownValue instances to populate the store
import { KnownValuesStore, IS_A, NOTE, SIGNED } from '@bcts/known-values';
// Create a store with predefined Known Values
const store = new KnownValuesStore([IS_A, NOTE, SIGNED]);
// Look up Known Values
console.log(store.knownValueNamed('isA')?.value()); // 1
console.log(store.knownValueNamed('note')?.value()); // 4
Inserts a KnownValue into the store.
If the KnownValue has an assigned name, it will be indexed by both its raw value and its name. If a KnownValue with the same raw value or name already exists in the store, it will be replaced.
The KnownValue to insert
Returns the assigned name for a KnownValue, if present in the store.
The KnownValue to look up
The assigned name, or undefined if not found
Returns a human-readable name for a KnownValue.
If the KnownValue has an assigned name in the store, that name is returned. Otherwise, the KnownValue's default name (which may be its numeric value as a string) is returned.
The KnownValue to get the name for
The name (assigned or numeric)
Looks up a KnownValue by its assigned name.
Returns the KnownValue if found, or undefined if no KnownValue with the given name exists in the store.
The name to look up
The KnownValue, or undefined if not found
Looks up a KnownValue by its raw numeric value.
The numeric value to look up (number or bigint)
The KnownValue, or undefined if not found
StaticknownRetrieves a KnownValue for a raw value, using a store if provided.
This static method allows looking up a KnownValue by its raw numeric value:
The numeric value to look up (number or bigint)
OptionalknownValues: KnownValuesStoreOptional store to search in
The KnownValue from the store or a new unnamed KnownValue
import { KnownValuesStore, IS_A } from '@bcts/known-values';
const store = new KnownValuesStore([IS_A]);
// Known value from store
const isA = KnownValuesStore.knownValueForRawValue(1, store);
console.log(isA.name()); // "isA"
// Unknown value creates a new KnownValue
const unknown = KnownValuesStore.knownValueForRawValue(999, store);
console.log(unknown.name()); // "999"
// No store provided also creates a new KnownValue
const unknown2 = KnownValuesStore.knownValueForRawValue(1, undefined);
console.log(unknown2.name()); // "1"
StaticknownAttempts to find a KnownValue by its name, using a store if provided.
This static method allows looking up a KnownValue by its name:
The name to look up
OptionalknownValues: KnownValuesStoreOptional store to search in
The KnownValue if found, or undefined
import { KnownValuesStore, IS_A } from '@bcts/known-values';
const store = new KnownValuesStore([IS_A]);
// Known value from store
const isA = KnownValuesStore.knownValueForName('isA', store);
console.log(isA?.value()); // 1
// Unknown name returns undefined
console.log(KnownValuesStore.knownValueForName('unknown', store)); // undefined
// No store provided also returns undefined
console.log(KnownValuesStore.knownValueForName('isA', undefined)); // undefined
StaticnameReturns a human-readable name for a KnownValue, using a store if provided.
This static method allows getting a name for a KnownValue:
The KnownValue to get the name for
OptionalknownValues: KnownValuesStoreOptional store to use for lookup
The name (assigned or numeric)
import { KnownValuesStore, IS_A } from '@bcts/known-values';
const store = new KnownValuesStore([IS_A]);
// Known value from store
let name = KnownValuesStore.nameForKnownValue(IS_A, store);
console.log(name); // "isA"
// Unknown value in store uses KnownValue's name method
name = KnownValuesStore.nameForKnownValue(new KnownValue(999), store);
console.log(name); // "999"
// No store provided also uses KnownValue's name method
name = KnownValuesStore.nameForKnownValue(IS_A, undefined);
console.log(name); // "isA"
Creates a shallow clone of this store.
A new KnownValuesStore with the same entries
A store that maps between Known Values and their assigned names.
The
KnownValuesStoreprovides a bidirectional mapping between:This enables efficient lookup in both directions, making it possible to:
The store is typically populated with predefined Known Values from the registry, but can also be extended with custom values.
Example