Last active
August 29, 2015 14:11
-
-
Save igroff/7e4d39d13d68c25f155e to your computer and use it in GitHub Desktop.
Turns a list of consistently delimited strings into something treeish
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
listOfDelimitedStringsToTree = (lines, delimiter) -> | |
stringTreeToObjectTree = (root) -> | |
childNodes = [] | |
for name, value of root | |
# path is just metadata for this whole process | |
# so we skip it | |
if name isnt "path" | |
node = | |
name: name | |
children: stringTreeToObjectTree(value) | |
if root.path | |
node.path = root.path + delimiter + name | |
else | |
node.path | |
childNodes.push node | |
childNodes | |
tree = {} | |
for line in lines | |
tokens = line.split(delimiter) | |
workingTree = tree | |
curPath = "" | |
for token in tokens | |
curPath += delimiter if curPath | |
curPath += token | |
workingTree[token] = workingTree[token] || {path: curPath} | |
workingTree = workingTree[token] | |
ret = | |
name: "root" | |
children: stringTreeToObjectTree(tree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment