Class: TreeTraversal

TreeTraversal

A tree traversal.

Constructor

new TreeTraversal()

Author:
  • Ryan Sandor Richards
Source:

Members

run

Alias for `walk`.
Source:
See:
  • walk

traverse

Alias for `walk`.
Source:
See:
  • walk

Methods

addPropertyHelper(Property) → {TreeTraversal}

Adds a node property name helper to the traversal. Note that property names that correspond to methods on the traversal will be ignored.
Parameters:
Name Type Description
Property string name helper to add.
Source:
See:
  • traverse for usage via the factory method.
Returns:
This tree traversal (for chaining).
Type
TreeTraversal
Example
// Create a couple property helpers for the traversal
var myTraversal = traversal()
 .addPropertyHelper('name')
 .addPropertyHelper('coolness')
// Use it to quickly handle special cases
myTraversal
 .name('ryan', function() { console.log('Ryan found'); })
 .name('ryan', function() { console.log('Airiel found'); })
 .name('nallely', function() { console.log('Nallely found'); })
 .coolness('totally', function() { console.log('Totally cool'); })

postorder(…propertyName) → {TreeTraversal}

Adds node property names to the traversal that should be recursively traversed *before* the node has been visited.
Parameters:
Name Type Attributes Description
propertyName string | Array.<string> <repeatable>
Node property names that, if exist, should be automatically traversed.
Source:
Returns:
This tree traversal (for chaining).
Type
TreeTraversal

preorder(…propertyName) → {TreeTraversal}

Adds node property names to the traversal that should be recursively traversed *after* the node has been visited.
Parameters:
Name Type Attributes Description
propertyName string | Array.<string> <repeatable>
Node property names that, if exist, should be automatically traversed.
Source:
Returns:
This tree traversal (for chaining).
Type
TreeTraversal

property(key, value, visitor) → {TreeTraversal}

Defines a new visitor that only applies to nodes that have a given property set to the given value. If `node` is the node currently being visited in the traversal, then this will only apply if `node[property] === value`.
Parameters:
Name Type Description
key string Key the node must have.
value string Value the node must have at the given key.
visitor TreeTraversal~visitorCallback Visitor callback to apply when `node[key] === value`.
Source:
Returns:
This tree traversal (for chaining).
Type
TreeTraversal
Example
// Add a visitor for all `node.type === 'number'`
traversal().property('type', 'number', function(node, recur) {
 // Do something with the `node` and possibly `recur` on its
 // children.
});

visit(visitor) → {TreeTraversal}

Sets the default visitor for the traversal.
Parameters:
Name Type Description
visitor TreeTraversal~visitorCallback Default visitor to set.
Source:
Returns:
This tree traversal (for chaining).
Type
TreeTraversal

walk(node, depthopt)

Perform the traversal on a given node.
Parameters:
Name Type Attributes Description
node Object Root node to traverse.
depth Number <optional>
Current depth of the traversal.
Source:

Type Definitions

visitorCallback(node, recur, depth)

Performs operations on a given node during a tree traversal.
Parameters:
Name Type Description
node Object Node currently being visited during the traversal.
recur TreeTraversal~recur Continues the traversal on a given node.
depth Number Current depth of the traversal.
Source: