Blockchain Commons Known Values TypeScript Library - v1.0.0-alpha.13
    Preparing search index...

    Class KnownValuesStore

    A store that maps between Known Values and their assigned names.

    The KnownValuesStore provides a bidirectional mapping between:

    • Numeric values (bigint) and their corresponding KnownValue instances
    • String names and their corresponding KnownValue instances

    This enables efficient lookup in both directions, making it possible to:

    • Find the name for a given numeric value
    • Find the numeric value for a given name
    • Retrieve complete KnownValue instances by either name or value

    The store is typically populated with predefined Known Values from the registry, but can also be extended with custom values.

    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 a Known Value by name
    const isA = store.knownValueNamed('isA');
    console.log(isA?.value()); // 1

    // Look up a name for a raw value
    const name = store.name(new KnownValue(3));
    console.log(name); // "signed"

    // Insert a custom Known Value
    const customStore = store.clone();
    customStore.insert(new KnownValue(100, 'customValue'));
    console.log(customStore.knownValueNamed('customValue')?.value()); // 100
    Index

    Constructors

    • 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.

      Parameters

      • knownValues: Iterable<KnownValue> = []

        Iterable of KnownValue instances to populate the store

      Returns KnownValuesStore

      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

    Methods

    • 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.

      Parameters

      Returns void

      import { KnownValuesStore, KnownValue } from '@bcts/known-values';

      const store = new KnownValuesStore();
      store.insert(new KnownValue(100, 'customValue'));
      console.log(store.knownValueNamed('customValue')?.value()); // 100
    • Returns the assigned name for a KnownValue, if present in the store.

      Parameters

      Returns string | undefined

      The assigned name, or undefined if not found

      import { KnownValuesStore, IS_A } from '@bcts/known-values';

      const store = new KnownValuesStore([IS_A]);
      console.log(store.assignedName(IS_A)); // "isA"
      console.log(store.assignedName(new KnownValue(999))); // undefined
    • 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.

      Parameters

      • knownValue: KnownValue

        The KnownValue to get the name for

      Returns string

      The name (assigned or numeric)

      import { KnownValuesStore, IS_A } from '@bcts/known-values';

      const store = new KnownValuesStore([IS_A]);
      console.log(store.name(IS_A)); // "isA"
      console.log(store.name(new KnownValue(999))); // "999"
    • 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.

      Parameters

      • assignedName: string

        The name to look up

      Returns KnownValue | undefined

      The KnownValue, or undefined if not found

      import { KnownValuesStore, IS_A } from '@bcts/known-values';

      const store = new KnownValuesStore([IS_A]);

      const isA = store.knownValueNamed('isA');
      console.log(isA?.value()); // 1

      console.log(store.knownValueNamed('nonexistent')); // undefined
    • Looks up a KnownValue by its raw numeric value.

      Parameters

      Returns KnownValue | undefined

      The KnownValue, or undefined if not found

      import { KnownValuesStore, IS_A } from '@bcts/known-values';

      const store = new KnownValuesStore([IS_A]);
      const isA = store.knownValueForValue(1);
      console.log(isA?.name()); // "isA"
    • Retrieves a KnownValue for a raw value, using a store if provided.

      This static method allows looking up a KnownValue by its raw numeric value:

      • If a store is provided and contains a mapping for the raw value, that KnownValue is returned
      • Otherwise, a new KnownValue with no assigned name is created and returned

      Parameters

      Returns KnownValue

      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"
    • Attempts to find a KnownValue by its name, using a store if provided.

      This static method allows looking up a KnownValue by its name:

      • If a store is provided and contains a mapping for the name, that KnownValue is returned
      • Otherwise, undefined is returned

      Parameters

      • name: string

        The name to look up

      • OptionalknownValues: KnownValuesStore

        Optional store to search in

      Returns KnownValue | undefined

      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
    • Returns a human-readable name for a KnownValue, using a store if provided.

      This static method allows getting a name for a KnownValue:

      • If a store is provided and contains a mapping for the KnownValue, its assigned name is returned
      • Otherwise, the KnownValue's default name (which may be its numeric value as a string) is returned

      Parameters

      Returns string

      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"