abstract generic logic from blueprinters to blueprinters.js

The logic in the files in the 'blueprinters' folder is now _only_ the data transformation logic. Instead of taking in arguments like the sheetId, the tabName, and the sheetName, the function now takes a single argument: the list of lists that represents the raw data from the sheet.

This setup gives datasheet-server greater value, as it allows developers to only specify the transformation logic, and not worry about the other stuff that datasheet server is doing.
This commit is contained in:
Lachlan Kermode
2018-12-13 15:56:54 +00:00
parent 7636db4f41
commit 5431b2be3f
9 changed files with 84 additions and 161 deletions

View File

@@ -1,41 +1,22 @@
import R from 'ramda'
import { fmtObj } from '../lib/util'
import { defaultBlueprint, defaultResource } from '../lib/blueprinters'
/**
* rows - generate a Blueprint from a data sheet 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.
* Each resource item is an object with values labelled according
* to column names specified in the sheet's first row.
*
* @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
* @param {type} data list of lists representing sheet data.
* @return {type} Array the structured data.
*/
export default function rows (
tabName,
sheetName,
sheetId,
data,
label = 'rows'
) {
// Define Blueprint
const bp = R.clone(defaultBlueprint)
bp.sheet = {
name: sheetName,
id: sheetId
}
bp.name = tabName
// Column names define resources
export default (data) => {
const itemLabels = data[0]
const fmt = fmtObj(itemLabels)
bp.resources[label] = R.clone(defaultResource)
bp.resources[label].data = []
const output = []
data.forEach((row, idx) => {
if (idx === 0) return
bp.resources[label].data.push(fmt(row))
output.push(fmt(row))
})
return bp
return output
}