Skip to content

Instantly share code, notes, and snippets.

@igroff
Last active August 29, 2015 14:11
Show Gist options
  • Save igroff/7e4d39d13d68c25f155e to your computer and use it in GitHub Desktop.
Save igroff/7e4d39d13d68c25f155e to your computer and use it in GitHub Desktop.
Turns a list of consistently delimited strings into something treeish
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