Sha256: 1911643e93fad3385abd54d201f1eff9904b2831107b7b460ffff993c34ae473

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

var _= require('underscore');

var nt= {'Array':true,'Object':true},
    isNode= function (val)
    {
       return (typeof val=='object')&&!!nt[val.constructor.name];
    };

module.exports= function traverse(orig,cb,skipDelete)
{
    var stack= [orig],
        push= _.bind(stack.push,stack),
        visited= [],
        wasVisited= function (obj)
        {
            if (obj.__visited!==undefined)
              return true;
            else
            {
              obj.__visited= visited.length;
              visited.push(obj);
              return false;
            }
        };

    while (stack.length)
    {
       var current= stack.pop();

       if (current&&isNode(current))
       {
           if (wasVisited(current)) continue;
     
           if (_.isArray(current))
           {
             if (cb(current)) continue;
             _(current).forEach(push);
           }
           else
           {
             var keys= _.without(_.keys(current),'__visited');
             if (cb(current,keys)) continue;
             _(keys).forEach(function (key)
             {
                push(current[key]);
             });
           }
       }
    }

    if (!skipDelete)
    _(visited).forEach(function (node)
    {
        delete node.__visited;
    });
};

module.exports.isNode= isNode;

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gson_parser-0.1.1 lib/nodejs/node_modules/gson/node_modules/circularjs/index.js
gson_parser-0.1.0 lib/nodejs/node_modules/gson/node_modules/circularjs/index.js