Created
September 7, 2015 13:41
-
-
Save richard512/92ba28edb0993fe70d75 to your computer and use it in GitHub Desktop.
Waldo: Search global JavaScript variables for strings, functions, etc.
This file contains hidden or 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
| // author: https://javascriptweblog.wordpress.com/2011/07/11/waldo-search-the-javascript-runtime-in-under-1-kb/ | |
| (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