Skip to content

Instantly share code, notes, and snippets.

@anvaka
Created November 20, 2012 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anvaka/4121424 to your computer and use it in GitHub Desktop.
Save anvaka/4121424 to your computer and use it in GitHub Desktop.
Graph for internal JS objects call
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