Last active
January 26, 2016 14:37
-
-
Save LarsVonQualen/ae66be53e7ef8f419438 to your computer and use it in GitHub Desktop.
Quick typescript namespace iterator
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
var NameSpaceIterator; | |
(function (NameSpaceIterator) { | |
function graph(namespace, depth) { | |
if (depth === void 0) { depth = 0; } | |
var result = ""; | |
for (var key in namespace) { | |
if (namespace.hasOwnProperty(key)) { | |
var val = namespace[key]; | |
if (val !== null && typeof val === "object") { | |
result += "" + getSpaces(depth) + key + "\n"; | |
result += graph(val, depth + 1); | |
} | |
else if (typeof val === "function") { | |
result += getSpaces(depth) + "- " + key + "\n"; | |
} | |
} | |
} | |
return result; | |
} | |
NameSpaceIterator.graph = graph; | |
function getSpaces(num) { | |
var tabs = ""; | |
for (var i = 0; i < num; i++) { | |
tabs += " "; | |
} | |
return tabs; | |
} | |
})(NameSpaceIterator || (NameSpaceIterator = {})); |
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
namespace NameSpaceIterator { | |
export function graph(namespace: Object, depth: number = 0) { | |
let result = ""; | |
for (let key in namespace) { | |
if (namespace.hasOwnProperty(key)) { | |
const val = namespace[key]; | |
if (val !== null && typeof val === "object") { | |
result += `${getSpaces(depth)}${key}\n`; | |
result += graph(val, depth + 1); | |
} else if (typeof val === "function") { | |
result += `${getSpaces(depth)}- ${key}\n`; | |
} | |
} | |
} | |
return result; | |
} | |
function getSpaces(num: number) { | |
var tabs = ""; | |
for (var i = 0; i < num; i++) { | |
tabs += " "; | |
} | |
return tabs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment