Skip to content

Instantly share code, notes, and snippets.

@servel333
Created September 29, 2015 15:30
Show Gist options
  • Save servel333/e1be910742ae36e2bc99 to your computer and use it in GitHub Desktop.
Save servel333/e1be910742ae36e2bc99 to your computer and use it in GitHub Desktop.
Shows keys recursively from a root object.
var lodash = require('lodash');
var _seenObjects = [];
var recurseKeys = function(name, obj){
var duplicate;
if (( duplicate = lodash.find(_seenObjects, function(seen){
return seen.obj === obj;
}) )) {
console.log(name, '=> (DUPLICATE of',duplicate.name+')');
return;
}
_seenObjects.push({ name: name, obj: obj });
console.log(name, '=> {Object}');
var keys = lodash.keys(obj);
lodash.each(keys, function(key){
var val = obj[key];
if (lodash.isNull (val)) { console.log(name+'.'+key, '=> NULL' ); }
else if (lodash.isUndefined (val)) { console.log(name+'.'+key, '=> UNDEFINED' ); }
else if (lodash.isNaN (val)) { console.log(name+'.'+key, '=> NaN' ); }
else if (lodash.isNumber (val)) { console.log(name+'.'+key, '=> {Number}' , val ); }
else if (lodash.isBoolean (val)) { console.log(name+'.'+key, '=> {Bool}' , val ); }
else if (lodash.isTypedArray (val)) { console.log(name+'.'+key, '=> {Array(Type)}' ); }
else if (lodash.isArray (val)) { console.log(name+'.'+key, '=> {Array}' ); }
else if (lodash.isArguments (val)) { console.log(name+'.'+key, '=> {Arguments}' ); }
else if (lodash.isElement (val)) { console.log(name+'.'+key, '=> {Element}' ); }
else if (lodash.isDate (val)) { console.log(name+'.'+key, '=> {Date}' , val ); }
else if (lodash.isError (val)) { console.log(name+'.'+key, '=> {Error}' ); }
else if (lodash.isFunction (val)) { console.log(name+'.'+key, '=> {Function}' ); }
else if (lodash.isRegExp (val)) { console.log(name+'.'+key, '=> {RegExp}' ); }
else if (lodash.isString (val)) { console.log(name+'.'+key, '=> {String}' ); }
else if (lodash.isPlainObject (val)) {
// console.log(name+'.'+key, '=> {Object}');
recurseKeys(name+'.'+key, val);
}
else if (lodash.isObject (val)) {
// console.log(name+'.'+key, '=> {Object}');
recurseKeys(name+'.'+key, val);
}
else {
console.log(name+'.'+key, '=> ?');
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment