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

    Function walk

    • Walk a CBOR tree, visiting each element with a visitor function.

      The visitor function is called for each element in the tree, in depth-first order. State is threaded through the traversal, allowing accumulation of results.

      For maps, the visitor is called with:

      1. A 'keyvalue' element containing both key and value
      2. The key individually (if descent wasn't stopped)
      3. The value individually (if descent wasn't stopped)

      Type Parameters

      • State

        The type of state to thread through the traversal

      Parameters

      • cbor: Cbor

        The CBOR value to traverse

      • initialState: State

        Initial state value

      • visitor: Visitor<State>

        Function to call for each element

      Returns void

      // Count all text strings in a structure using RefCell-like pattern
      const count = { value: 0 };

      const structure = cbor({ name: 'Alice', tags: ['urgent', 'draft'] });
      walk(structure, null, (element, level, edge, state) => {
      if (element.type === 'single' && element.cbor.type === MajorType.Text) {
      count.value++;
      }
      return [state, false];
      });
      console.log(count.value); // 3 (name, urgent, draft)
      // Find first occurrence and stop
      const structure = cbor([1, 2, 3, 'found', 5, 6]);
      let found = false;

      walk(structure, null, (element, level, edge, state) => {
      if (element.type === 'single' &&
      element.cbor.type === MajorType.Text &&
      element.cbor.value === 'found') {
      found = true;
      return [null, true]; // Stop descending
      }
      return [null, false];
      });