Created
August 4, 2014 08:28
-
-
Save flipjs/04a87d6e28d1fd25eb89 to your computer and use it in GitHub Desktop.
Waldo by Angus Croll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var traverse = function(util, searchTerm, options) { | |
var options = options || {}; | |
var obj = options.obj || window; | |
var path = options.path || ((obj==window) ? "window" : ""); | |
var props = Object.keys(obj); | |
props.forEach(function(prop) { | |
if ((tests[util] || util)(searchTerm, obj, prop)){ | |
console.log([path, ".", prop].join(""), "->",["(", typeof obj[prop], ")"].join(""), obj[prop]); | |
} | |
if(Object.prototype.toString.call(obj[prop])=="[object Object]" && | |
(obj[prop] != obj) && path.split(".").indexOf(prop) == -1) { | |
traverse(util, searchTerm, {obj: obj[prop], path: [path,prop].join(".")}); | |
} | |
}); | |
} | |
var dealWithIt = function(util, expected, searchTerm, options) { | |
(!expected || typeof searchTerm == expected) ? | |
traverse(util, searchTerm, options) : | |
console.error([searchTerm, 'must be', expected].join(' ')); | |
} | |
var tests = { | |
'name': function(searchTerm, obj, prop) {return searchTerm == prop}, | |
'nameContains': function(searchTerm, obj, prop) {return prop.indexOf(searchTerm)>-1}, | |
'type': function(searchTerm, obj, prop) {return obj[prop] instanceof searchTerm}, | |
'value': function(searchTerm, obj, prop) {return obj[prop] === searchTerm}, | |
'valueCoerced': function(searchTerm, obj, prop) {return obj[prop] == searchTerm} | |
} | |
window.find={ | |
byName: function(searchTerm, options) {dealWithIt('name', 'string', searchTerm, options);}, | |
byNameContains: function(searchTerm, options) {dealWithIt('nameContains', 'string', searchTerm, options);}, | |
byType: function(searchTerm, options) {dealWithIt('type', 'function', searchTerm, options);}, | |
byValue: function(searchTerm, options) {dealWithIt('value', null, searchTerm, options);}, | |
byValueCoerced: function(searchTerm, options) {dealWithIt('valueCoerced', null, searchTerm, options);}, | |
custom: function(fn, options) {traverse(fn, null, options);} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment