Created
November 20, 2012 21:48
-
-
Save anvaka/4121424 to your computer and use it in GitHub Desktop.
Graph for internal JS objects call
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 buildObjectCallGraph(obj, timeOutId) { | |
if (typeof Viva === 'undefined') { | |
if (!timeOutId) { | |
var head = document.getElementsByTagName('head')[0], | |
script = document.createElement('script'); | |
script.setAttribute('src', 'https://raw.github.com/anvaka/VivaGraphJS/master/dist/vivagraph.min.js'); | |
script.setAttribute("type","text/javascript"); | |
head.appendChild(script); | |
} | |
var timeOutId = setTimeout(function() { | |
buildObjectCallGraph(obj, timeOutId); | |
}, 500); | |
return; | |
} | |
var g = new Viva.Graph.graph(), | |
properties = [], | |
key; | |
for (key in obj) { | |
g.addNode(key); | |
properties.push(key); | |
} | |
properties.forEach(function(propName) { | |
var content = '', | |
foundDependencies = {}; | |
if (typeof obj[propName] === 'function') { | |
content = obj[propName].toString(); | |
} | |
if (content) { | |
properties.forEach(function(otherPropName) { | |
// ignore self-declaration | |
if (otherPropName !== propName) { | |
if (content.match(new RegExp('\\b' + otherPropName + '\\b'))) { | |
// make sure we add it only once. | |
foundDependencies[otherPropName] = true; | |
} | |
} | |
}); | |
for(var dependency in foundDependencies) { | |
if (foundDependencies.hasOwnProperty(dependency)) { | |
g.addLink(propName, dependency); | |
} | |
} | |
} | |
}); | |
var result = Viva.Graph.serializer().storeToJSON(g); | |
console.log(result); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment