mirror of
https://github.com/bellingcat/datasheet-server.git
synced 2026-06-11 04:48:32 +03:00
Clean master commit
This commit is contained in:
34
src/blueprinters/byColumn.js
Normal file
34
src/blueprinters/byColumn.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import {defaultBlueprint, defaultRoute} from "../lib/blueprinters";
|
||||
|
||||
/**
|
||||
* byColumn - generate a Blueprint from a data source by column. Each column
|
||||
* name is a resource, and all values in that column are the resource items.
|
||||
*
|
||||
* @param {type} data - list of lists representing sheet data.
|
||||
* @return {type} Blueprint
|
||||
* generated.
|
||||
*/
|
||||
export default function byColumn(tabName, sourceName, sourceId, data) {
|
||||
// Define Blueprint props
|
||||
const bp = R.clone(defaultBlueprint);
|
||||
bp.source = {
|
||||
name: sourceName,
|
||||
id: sourceId
|
||||
};
|
||||
bp.name = tabName;
|
||||
|
||||
// column names define routes
|
||||
const labels = data[0];
|
||||
labels.forEach(label => {
|
||||
bp.routes[label] = R.clone(defaultRoute);
|
||||
});
|
||||
|
||||
// remaining rows as data
|
||||
data.forEach((row, idx) => {
|
||||
if (idx == 0) return;
|
||||
labels.forEach((label, idx) => {
|
||||
bp.routes[label].data.push(row[idx]);
|
||||
});
|
||||
});
|
||||
return bp;
|
||||
}
|
||||
56
src/blueprinters/byGroup.js
Normal file
56
src/blueprinters/byGroup.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import R from "ramda";
|
||||
import {fmtObj, idxSearcher} from "../lib/util";
|
||||
import {defaultBlueprint, defaultRoute} from "../lib/blueprinters";
|
||||
|
||||
/**
|
||||
* byGroup - generate a Blueprint from a data source 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 byGroup(
|
||||
tabName,
|
||||
sourceName,
|
||||
sourceId,
|
||||
data,
|
||||
label = "groups"
|
||||
) {
|
||||
// Define Blueprint
|
||||
const bp = R.clone(defaultBlueprint);
|
||||
bp.source = {
|
||||
name: sourceName,
|
||||
id: sourceId
|
||||
};
|
||||
bp.name = tabName;
|
||||
|
||||
// Column names define routes
|
||||
const itemLabels = data[0];
|
||||
const fmt = fmtObj(itemLabels);
|
||||
bp.routes[label] = R.clone(defaultRoute);
|
||||
bp.routes[label].data = [];
|
||||
|
||||
const dataGroups = {};
|
||||
|
||||
data.forEach((row, idx) => {
|
||||
if (idx == 0) return;
|
||||
const group = fmt(row).group;
|
||||
if (!dataGroups[group]) {
|
||||
dataGroups[group] = [fmt(row)];
|
||||
} else {
|
||||
dataGroups[group].push(fmt(row));
|
||||
}
|
||||
});
|
||||
Object.keys(dataGroups).forEach(groupKey => {
|
||||
bp.routes[label].data.push({
|
||||
group: groupKey,
|
||||
group_label: dataGroups[groupKey][0].group_label,
|
||||
data: dataGroups[groupKey]
|
||||
});
|
||||
});
|
||||
return bp;
|
||||
}
|
||||
42
src/blueprinters/byId.js
Normal file
42
src/blueprinters/byId.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import R from "ramda";
|
||||
import {fmtObj, idxSearcher} from "../lib/util";
|
||||
import {defaultBlueprint, defaultRoute} from "../lib/blueprinters";
|
||||
|
||||
/**
|
||||
* byId - generate a Blueprint from a data source by id, which is an integer.
|
||||
* The resource name defaults to 'ids', 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="ids" name of resource in blueprint.
|
||||
* @param {type} name="" name of blueprint.
|
||||
* @return {type} Blueprint
|
||||
*/
|
||||
export default function byId(
|
||||
tabName,
|
||||
sourceName,
|
||||
sourceId,
|
||||
data,
|
||||
label = "ids"
|
||||
) {
|
||||
// Define Blueprint
|
||||
const bp = R.clone(defaultBlueprint);
|
||||
bp.source = {
|
||||
name: sourceName,
|
||||
id: sourceId
|
||||
};
|
||||
bp.name = tabName;
|
||||
|
||||
// Column names define routes
|
||||
const itemLabels = data[0];
|
||||
const fmt = fmtObj(itemLabels);
|
||||
bp.routes[label] = R.clone(defaultRoute);
|
||||
bp.routes[label].data = [];
|
||||
|
||||
data.forEach((row, idx) => {
|
||||
if (idx == 0) return;
|
||||
bp.routes[label].data[fmt(row).id] = fmt(row);
|
||||
});
|
||||
return bp;
|
||||
}
|
||||
41
src/blueprinters/byRow.js
Normal file
41
src/blueprinters/byRow.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import R from "ramda";
|
||||
import {fmtObj, idxSearcher} from "../lib/util";
|
||||
import {defaultBlueprint, defaultRoute} from "../lib/blueprinters";
|
||||
|
||||
/**
|
||||
* byRow - generate a Blueprint from a data source by row. The resource name
|
||||
* defaults to 'rows', or a custom resource name can be passed. Each resource
|
||||
* item is an object with values labelled according to column names.
|
||||
*
|
||||
* @param {type} data list of lists representing sheet data.
|
||||
* @param {type} label="rows" name of resource in blueprint.
|
||||
* @param {type} name="" name of blueprint.
|
||||
* @return {type} Blueprint
|
||||
*/
|
||||
export default function byRow(
|
||||
tabName,
|
||||
sourceName,
|
||||
sourceId,
|
||||
data,
|
||||
label = "rows"
|
||||
) {
|
||||
// Define Blueprint
|
||||
const bp = R.clone(defaultBlueprint);
|
||||
bp.source = {
|
||||
name: sourceName,
|
||||
id: sourceId
|
||||
};
|
||||
bp.name = tabName;
|
||||
|
||||
// Column names define routes
|
||||
const itemLabels = data[0];
|
||||
const fmt = fmtObj(itemLabels);
|
||||
bp.routes[label] = R.clone(defaultRoute);
|
||||
bp.routes[label].data = [];
|
||||
|
||||
data.forEach((row, idx) => {
|
||||
if (idx == 0) return;
|
||||
bp.routes[label].data.push(fmt(row));
|
||||
});
|
||||
return bp;
|
||||
}
|
||||
67
src/blueprinters/byTree.js
Normal file
67
src/blueprinters/byTree.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import R from "ramda";
|
||||
import {fmtObj, idxSearcher} from "../lib/util";
|
||||
import {defaultBlueprint, defaultRoute} from "../lib/blueprinters";
|
||||
|
||||
/**
|
||||
* byTree - generate a Blueprint from a data source 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 byTree(
|
||||
tabName,
|
||||
sourceName,
|
||||
sourceId,
|
||||
data,
|
||||
label = "tree"
|
||||
) {
|
||||
// Define Blueprint
|
||||
const bp = R.clone(defaultBlueprint);
|
||||
bp.source = {
|
||||
name: sourceName,
|
||||
id: sourceId
|
||||
};
|
||||
bp.name = tabName;
|
||||
|
||||
// Column names define routes
|
||||
bp.routes[label] = R.clone(defaultRoute);
|
||||
bp.routes[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.routes[label].data = tree;
|
||||
return bp;
|
||||
}
|
||||
Reference in New Issue
Block a user