diff --git a/src/blueprinters/deeprows.js b/src/blueprinters/deeprows.js index 7e00e69..a27c7f2 100644 --- a/src/blueprinters/deeprows.js +++ b/src/blueprinters/deeprows.js @@ -7,19 +7,65 @@ import { fmtObj } from '../lib/util' * column names are the same except for a different integer at the end * (e.g. 'tag1', and 'tag2'), then the values of those two columns are * aggregated into a list, which is the value of the prefix's key ('tag'). + * Any values in those columns that are empty will NOT be added to the list. * * @param {type} data list of lists representing sheet data. * @return {type} Array the structured data. */ export default (data) => { - // TODO: make these deep rows. const itemLabels = data[0] - const fmt = fmtObj(itemLabels) + const baseFmt = fmtObj(itemLabels) const output = [] + // create a structure to indicate which columns needs to be aggregated + const endsWithNumber = new RegExp('(.*)[0-9]+$') + const structure = { + __flat: [] + } + + itemLabels.forEach(label => { + const matches = label.match(endsWithNumber) + if (!matches) { + structure.__flat.push(label) + } else { + const labelPrefix = `${matches[1]}s` + if (labelPrefix in structure) { + structure[labelPrefix].push(label) + } else { + structure[labelPrefix] = [ label ] + } + } + }) + + // generate the value for deep labels using the structure created data.forEach((row, idx) => { if (idx === 0) return - output.push(fmt(row)) + const baseRow = baseFmt(row) + const deepRow = {} + + // generate deep row labels using structure + Object.keys(structure) + .forEach(newLabel => { + if (newLabel != '__flat') { + const oldLabels = structure[newLabel] + // only add new value if not '' + const labelValues = [] + oldLabels.forEach(l => { + const vl = baseRow[l] + if (vl !== '') { + labelValues.push(vl) + } + }) + deepRow[newLabel] = labelValues + } + }) + + // move values for flat labels over from base + structure.__flat.forEach(label => { + deepRow[label] = baseRow[label] + }) + + output.push(deepRow) }) return output