Last active
August 29, 2015 14:06
-
-
Save a-laughlin/834bc6bd2e132b832490 to your computer and use it in GitHub Desktop.
Search scope keys and values, including nested objects/arrays, for instances of string or regexp
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
javascript:(function(){ | |
window.angular.debug = { | |
searchScope : function(varName,options) { | |
var registeredScopes = {}; | |
var opts = angular.extend({ | |
includeFns:false, | |
includeHidden:false, | |
recurseDepth:20 | |
}, options); | |
var testFn = function(test,testStr){ | |
if(test instanceof RegExp) return test.test(testStr); | |
return testStr.indexOf(test) > -1 | |
}; | |
function getParentTree(scope){ | |
return (function appendParent(s){ | |
return s.$parent ? appendParent(s.$parent) + '>' + s.$id : '$root'; | |
})(scope); | |
} | |
angular.element('[ng-app],.ng-scope,.ng-isolate-scope').filter(opts.includeHidden?'*':':visible').each(function(i,elem){ | |
var scope = angular.element(elem).scope(); | |
if(registeredScopes[scope.$id]) return; | |
registeredScopes[scope.$id] = { | |
scope:scope, | |
tree:getParentTree(scope) | |
}; | |
recurse(scope,'',0); | |
function recurse(obj,key,currentDepth){ | |
if (currentDepth > opts.recurseDepth){return;} | |
angular.forEach(obj,function(item,prop){ | |
if(obj.hasOwnProperty(prop) === false || /^(__|\$\$|this$|\$parent|\$root)/.test(prop)) {return;} | |
if(typeof item === 'object' && item !== null){ | |
if(item instanceof Array === false){ | |
recurse(item,(key ? key + '.':'') + prop,currentDepth + 1); | |
} else { | |
item.forEach(function(val,idx){ | |
recurse(val,key + '.' + prop + '['+idx+']',currentDepth + 1); | |
}); | |
} | |
} | |
var toLog = ['\t',registeredScopes[scope.$id].tree + '.' + key + '.' + prop]; | |
var origLength = toLog.length | |
if(testFn(varName,prop)){ | |
if(typeof item === 'function'){ | |
if (opts.includeFns) { | |
toLog.push('function'); | |
}; | |
} else { | |
toLog.push(item); | |
} | |
} else if (/number|string|boolean/.test(typeof item) && testFn(varName,item)){ | |
toLog.push(item+''); | |
} | |
if(origLength !== toLog.length){ | |
if(registeredScopes[scope.$id].elem === undefined){ | |
registeredScopes[scope.$id].elem = elem; | |
console.log(registeredScopes[scope.$id].tree, elem); | |
} | |
console.log.apply(console,toLog); | |
} | |
}); | |
} | |
}); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment