mirror of
https://github.com/bellingcat/datasheet-server.git
synced 2026-06-10 12:28:34 +03:00
68 lines
1.5 KiB
JavaScript
68 lines
1.5 KiB
JavaScript
import R from 'ramda'
|
|
import { defaultBlueprint, defaultResource } from '../lib/blueprinters'
|
|
|
|
/**
|
|
* tree - generate a Blueprint from a data sheet grouped by a column called 'group'
|
|
* The resource name defaults to 'groups', or a custom resource name can be passed.
|
|
* Each resource item is an object with values labelled according to column
|
|
* names. Items are inserted in the data list at idx = id.
|
|
*
|
|
* @param {type} data list of lists representing sheet data.
|
|
* @param {type} label="groups" name of resource in blueprint.
|
|
* @param {type} name="" name of blueprint.
|
|
* @return {type} Blueprint
|
|
*/
|
|
export default function tree (
|
|
tabName,
|
|
sheetName,
|
|
sheetId,
|
|
data,
|
|
label = 'tree'
|
|
) {
|
|
// Define Blueprint
|
|
const bp = R.clone(defaultBlueprint)
|
|
bp.sheet = {
|
|
name: sheetName,
|
|
id: sheetId
|
|
}
|
|
bp.name = tabName
|
|
|
|
// Column names define resources
|
|
bp.resources[label] = R.clone(defaultResource)
|
|
bp.resources[label].data = {}
|
|
|
|
const tree = {
|
|
key: 'tags',
|
|
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++
|
|
}
|
|
})
|
|
|
|
bp.resources[label].data = tree
|
|
return bp
|
|
}
|