Skip to content

Instantly share code, notes, and snippets.

@shanewholloway
Created February 16, 2018 19:06
Show Gist options
  • Save shanewholloway/b6656951e8ea8aabc64d7b6192d99dea to your computer and use it in GitHub Desktop.
Save shanewholloway/b6656951e8ea8aabc64d7b6192d99dea to your computer and use it in GitHub Desktop.
ES3 Inspect tool
// to depth-first inspect all attributes of an object
inspect(obj)
// or, to see all attributes on the entire prototype chain
inspect.chain(obj)
var inspect = function () {
"use strict";
var _getPrototypeOf = Object.getPrototypeOf || _fallback_getPrototypeOf;
var _hasOwnProperty = {}.hasOwnProperty;
var _isArray = Array.isArray || _fallback_isArray;
inspect.chain = function (obj, cb) {
return inspect(prototypeChain(obj), cb);
};
return inspect;
function _fallback_getPrototypeOf(obj) {
console.log('fallback getPrototypeOf');
return obj.__proto__;
}
function _fallback_isArray(obj) {
return 'number' == typeof obj.length && 'function' == typeof obj.map && 'function' == typeof obj.forEach;
}
function prototypeChain(obj) {
var res = [];
while (null !== obj && undefined !== obj) {
res.push(obj);
obj = _getPrototypeOf(obj);
}
return res;
}
function _indent_path(path, indent) {
var i = 0,
len = path.length - 1,
res = '';
for (; i < len; i++) {
res += indent || ' ';
}
return res + (path[len] || '');
}
function _cb_debug(kind, path, v, type) {
var sz_path = _indent_path(path, ' ');
if ('value' === kind) {
console.log(sz_path + '(' + type + '):', [v]);
} else if ('array' === kind) {
console.log(sz_path + '(' + type + '): @[]');
} else if ('hash' === kind) {
console.log(sz_path + '(' + type + '): @{}');
}
}
function inspect(obj, cb) {
if (null == cb) {
cb = _cb_debug;
}
var seen = [];
var q = [[obj, []]];
while (q.length) {
visit_tip(q.shift());
}
function visit_tip(tip) {
var v = tip[0],
path = tip[1],
type = typeof v;
var k = path.join('.');
if (-1 !== seen.indexOf(v)) {
return;
}
seen.push(v);
if (null === v || undefined === v) {
cb('value', path, v, type);
} else if ('object' == type) {
if (_isArray(v)) {
cb('array', path, v, type);
visit_array(v, path);
} else {
cb('hash', path, v, type);
visit_hash(v, path);
}
} else cb('value', path, v, type);
}
function visit_hash(obj, path) {
var ql = [],
k;
for (k in obj) {
if (_hasOwnProperty.call(obj, k)) {
ql.push([obj[k], path.concat([k])]);
}
}
q = ql.concat(q);
}
function visit_array(arr, path) {
var ql = [],
i = 0,
len = arr.length;
for (; i < len; i++) {
ql.push([arr[i], path.concat(['[' + i + ']'])]);
}
q = ql.concat(q);
}
}
}();
var inspect = (function() {
"use strict"
var _getPrototypeOf = Object.getPrototypeOf || _fallback_getPrototypeOf
var _hasOwnProperty = {}.hasOwnProperty
var _isArray = Array.isArray || _fallback_isArray
inspect.chain = function(obj, cb) { return inspect(prototypeChain(obj), cb) }
return inspect
function _fallback_getPrototypeOf(obj) ::
console.log @ 'fallback getPrototypeOf'
return obj.__proto__
function _fallback_isArray(obj) ::
return 'number' == typeof obj.length &&
'function' == typeof obj.map &&
'function' == typeof obj.forEach
function prototypeChain(obj) ::
var res = []
while null !== obj && undefined !== obj ::
res.push @ obj
obj = _getPrototypeOf(obj)
return res
function _indent_path(path, indent) ::
var i=0, len=path.length-1, res=''
for ; i<len; i++ :: res += (indent || ' ')
return res + (path[len] || '')
function _cb_debug(kind, path, v, type) ::
var sz_path = _indent_path(path, ' ')
if 'value' === kind ::
console.log @ sz_path+'('+type+'):', [v]
else if 'array' === kind ::
console.log @ sz_path+'('+type+'): @[]'
else if 'hash' === kind ::
console.log @ sz_path+'('+type+'): @{}'
function inspect(obj, cb) ::
if null == cb :: cb = _cb_debug
var seen = []
var q = [[obj, []]]
while q.length ::
visit_tip @ q.shift()
function visit_tip(tip) ::
var v = tip[0], path=tip[1], type = typeof v
var k = path.join('.')
if -1 !== seen.indexOf(v) :: return
seen.push(v)
if null === v || undefined === v ::
cb @ 'value', path, v, type
else if 'object' == type ::
if _isArray(v) ::
cb @ 'array', path, v, type
visit_array(v, path)
else ::
cb @ 'hash', path, v, type
visit_hash(v, path)
else cb @ 'value', path, v, type
function visit_hash(obj, path) ::
var ql=[], k
for k in obj ::
if _hasOwnProperty.call(obj, k) ::
ql.push @# obj[k], path.concat([k])
q = ql.concat @ q
function visit_array(arr, path) ::
var ql=[], i=0, len = arr.length
for ; i<len; i++ ::
ql.push @# arr[i], path.concat(['['+i+']'])
q = ql.concat @ q
})()
@HollisGist
Copy link

chrome bookmark version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment