mirror of
https://github.com/bellingcat/datasheet-server.git
synced 2026-06-10 12:28:34 +03:00
41 lines
805 B
JavaScript
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
|
|
}
|