Skip to content

Methods & Callbacks

Alex edited this page Jun 14, 2015 · 2 revisions

Methods

next()

Returns the state described of iteration protocol.

isNode(any)

Returns true if any is node.

var iterator = new RecursiveIterator(root);
var item = iterator.next();

iterator.isNode(item.node);

You can override this function for custom behaviour:

var iterator = new RecursiveIterator(root);

iterator.isNode = function(any) {
    return {Boolean};
};

By default node is {Object}.

isLeaf(node)

Returns true if node is leaf. Leaf is all primitive types.

var iterator = new RecursiveIterator(root);
var item = iterator.next();

iterator.isLeaf(item.node);

isCircular(object)

Returns true if object is circular reference.

var iterator = new RecursiveIterator(root);
var item = iterator.next();

iterator.isCircular(item.node);

destroy()

Clears cache and queue. It calls automatically at the end of the loop.

getState(parent, node, key, path = [], deep = 0)

Returns descriptor of state.

getStatesOfChildNodes(node, path, deep)

Returns array of states of child nodes.

Callbacks

onStepInto(state)

It calls for each node. If returns false node will be skipped:

var root = {
    object: {
        number: 1
    },
    string: 'foo'
};

var iterator = new RecursiveIterator(root);

iterator.onStepInto = function(state) {
    // prevent step into node
    return false;
};

for(let {node, path} of iterator) {
    console.log(path.join('.'), node);
}

// object    Object {number: 1}
// string    foo