Files
datasheet-server/src/blueprinters/tree.js
Lachlan Kermode ff30d1be18 tree root -> _root
2019-02-14 16:11:38 +00:00

41 lines
805 B
JavaScript

/**
* Each resource item is inserted into a tree. TODO: describe layout.
*
* @param {type} data list of lists representing sheet data.
* @return {type} Array the structured data.
*/
export default (data) => {
const tree = {
key: '_root',
children: {}
}
data.forEach(path => {
const root = path[0]
if (!tree.children[root]) {
tree.children[root] = {
key: root,
children: {}
}
}
let depth = 1
let parentNode = tree.children[root]
while (depth < path.length) {
const node = path[depth]
if (!parentNode.children[node]) {
parentNode.children[node] = {
key: node,
children: {}
}
}
parentNode = parentNode.children[node]
depth++
}
})
return tree
}