BC-DCBOR TypeScript Library - v1.0.0-alpha.13
    Preparing search index...

    Class CborSet

    CBOR Set type with tag(258) encoding.

    Internally uses a CborMap to ensure unique elements with deterministic ordering. Elements are ordered by their CBOR encoding (lexicographic byte order).

    // Create set
    const set = CborSet.fromArray([1, 2, 3]);
    const set2 = CborSet.fromSet(new Set([1, 2, 3]));

    // Add elements
    set.insert(4);
    set.insert(2); // Duplicate, no effect

    // Check membership
    console.log(set.contains(2)); // true
    console.log(set.contains(99)); // false

    // Encode to CBOR
    const tagged = set.taggedCbor();

    Implements

    Index

    Constructors

    Accessors

    • get size(): number

      Get the number of elements in the set.

      Returns number

      Number of elements

    • get diagnostic(): string

      Get diagnostic notation for the set.

      Returns string

      String representation

      const set = CborSet.fromArray([1, 2, 3]);
      console.log(set.diagnostic); // "[1, 2, 3]"

    Methods

    • Create CborSet from array.

      Duplicates are automatically removed.

      Type Parameters

      Parameters

      • items: T[]

        Array of items to add to the set

      Returns CborSet

      New CborSet instance

      const set = CborSet.fromArray([1, 2, 3, 2, 1]);
      console.log(set.size); // 3
    • Create CborSet from JavaScript Set.

      Type Parameters

      Parameters

      • items: Set<T>

        JavaScript Set of items

      Returns CborSet

      New CborSet instance

      const jsSet = new Set([1, 2, 3]);
      const cborSet = CborSet.fromSet(jsSet);
    • Create CborSet from iterable.

      Type Parameters

      Parameters

      • items: Iterable<T>

        Iterable of items

      Returns CborSet

      New CborSet instance

    • Insert an element into the set.

      If the element already exists, has no effect.

      Parameters

      Returns void

      const set = new CborSet();
      set.insert(1);
      set.insert(2);
      set.insert(1); // No effect, already exists
    • Check if set contains an element.

      Parameters

      Returns boolean

      true if element is in the set

      const set = CborSet.fromArray([1, 2, 3]);
      console.log(set.contains(2)); // true
      console.log(set.contains(99)); // false
    • Remove an element from the set.

      Parameters

      Returns boolean

      true if element was removed, false if not found

      const set = CborSet.fromArray([1, 2, 3]);
      set.delete(2); // Returns true
      set.delete(99); // Returns false
    • Remove all elements from the set.

      Returns void

    • Check if the set is empty.

      Returns boolean

      true if set has no elements

    • Create a new set containing elements in this set or the other set.

      Parameters

      Returns CborSet

      New set with union of elements

      const set1 = CborSet.fromArray([1, 2, 3]);
      const set2 = CborSet.fromArray([3, 4, 5]);
      const union = set1.union(set2);
      // union contains [1, 2, 3, 4, 5]
    • Create a new set containing elements in both this set and the other set.

      Parameters

      Returns CborSet

      New set with intersection of elements

      const set1 = CborSet.fromArray([1, 2, 3]);
      const set2 = CborSet.fromArray([2, 3, 4]);
      const intersection = set1.intersection(set2);
      // intersection contains [2, 3]
    • Create a new set containing elements in this set but not in the other set.

      Parameters

      Returns CborSet

      New set with difference of elements

      const set1 = CborSet.fromArray([1, 2, 3]);
      const set2 = CborSet.fromArray([2, 3, 4]);
      const diff = set1.difference(set2);
      // diff contains [1]
    • Check if this set is a subset of another set.

      Parameters

      Returns boolean

      true if all elements of this set are in the other set

    • Check if this set is a superset of another set.

      Parameters

      Returns boolean

      true if all elements of the other set are in this set

    • Iterate over elements in the set.

      Elements are returned in deterministic order (by CBOR encoding).

      Returns Iterator<Cbor>

      const set = CborSet.fromArray([3, 1, 2]);
      for (const value of set) {
      console.log(extractCbor(value));
      }
    • Get all values as an array.

      Returns Cbor[]

      Array of CBOR values in deterministic order

      const set = CborSet.fromArray([3, 1, 2]);
      const values = set.values();
      // Values in deterministic order
    • Execute a function for each element.

      Parameters

      • callback: (value: Cbor) => void

        Function to call for each element

      Returns void

      set.forEach(value => {
      console.log(extractCbor(value));
      });
    • Returns the CBOR tags associated with this type.

      This method should return an array of tags in order of preference:

      • The first tag in the array is the "preferred" tag and will be used when encoding values of this type via CborTaggedEncodable.taggedCbor().

      • All tags in the array are considered equivalent for decoding. When CborTaggedDecodable.fromTaggedCbor() is called, any tag in this array will be accepted as valid for this type.

      This design enables backward compatibility: you can introduce a new tag (placed first in the array) while still supporting older tags for decoding.

      For standard CBOR tags, you can use predefined tag constants from the tags module, or create custom tags with createTag().

      Returns Tag[]

    • Returns the untagged CBOR encoding of this instance.

      This method defines how the value itself (without its tag) should be represented in CBOR format.

      Returns Cbor

      Untagged CBOR representation

    • Returns the tagged CBOR encoding of this instance.

      This method wraps the result of untaggedCbor() with the first tag from cborTags(), which is considered the "preferred" tag for the type.

      Even if a type supports multiple tags for backward-compatible decoding via cborTags(), only the first (preferred) tag is used for encoding. This ensures consistency in newly created data while maintaining the ability to read older formats.

      In most cases, you don't need to override this method.

      Returns Cbor

      Tagged CBOR representation

    • Creates an instance of this type by decoding it from untagged CBOR.

      This method defines how to interpret the CBOR content (without considering the tag) and convert it to the implementing type.

      Parameters

      Returns CborSet

      Decoded instance

      Error if the CBOR value cannot be decoded

    • Creates an instance of this type by decoding it from tagged CBOR.

      This method first verifies that the CBOR value has one of the expected tags (as defined by cborTags()), then delegates to fromUntaggedCbor() to decode the content.

      For backward compatibility, this method accepts any tag from the cborTags() array, not just the first one. This allows new versions of types to still accept data tagged with older/alternative tag values.

      In most cases, you don't need to override this method.

      Parameters

      Returns CborSet

      Decoded instance

      Error if the CBOR value has the wrong tag or cannot be decoded

    • Decode a CborSet from tagged CBOR (static method).

      Parameters

      • cbor: Cbor

        Tagged CBOR value with tag(258)

      Returns CborSet

      Decoded CborSet instance

    • Convert to CBOR array (untagged).

      Returns Cbor

      CBOR array

    • Convert to CBOR bytes (tagged).

      Returns Uint8Array

      Encoded CBOR bytes

    • Convert to JavaScript Set.

      Returns Set<unknown>

      JavaScript Set with extracted values

      const cborSet = CborSet.fromArray([1, 2, 3]);
      const jsSet = cborSet.toSet();
      console.log(jsSet.has(1)); // true
    • Convert to JavaScript Array.

      Returns unknown[]

      Array with extracted values

    • Convert to string (same as diagnostic).

      Returns string

      String representation

    • Convert to JSON (returns array of values).

      Returns unknown[]

      Array for JSON serialization