The type of state to thread through the traversal
// 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];
});
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: