Skip to content

Commit

Permalink
adding getData method
Browse files Browse the repository at this point in the history
  • Loading branch information
pat310 committed Feb 1, 2017
1 parent c5d7134 commit af4eaec
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 42 deletions.
79 changes: 59 additions & 20 deletions lib/quick-pivot.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/quick-pivot.js.map

Large diffs are not rendered by default.

27 changes: 23 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,39 @@ export default class Pivot {
}

this.currData = this.originalData;
this.collapsedRows = [];
this.collapsedRows = {};
}

update(data, rows, cols, agg, type, header) {
this.originalData = tableCreator(data, rows, cols, agg, type, header);
this.currData = this.originalData;
}

collapse(rowNum) {
this.collapsedRows.push(rowNum);
this.currData = collapse(rowNum, this.currData);
let returnedData = collapse(rowNum, this.currData);

this.collapsedRows[this.currData.table[rowNum].row] =
returnedData.collapsed;
this.currData = returnedData.dataToReturn;
return this;
}

expand(rowNum) {
this.currData = expand(rowNum, this.currData);
this.currData = expand(
rowNum,
this.currData,
this.collapsedRows[this.currData.table[rowNum].row],
);
delete this.collapsedRows[this.currData.table[rowNum].row];
return this;
}

getData(rowNum) {
if (this.collapsedRows[this.currData.table[rowNum].row]) {
return this.collapsedRows[this.currData.table[rowNum].row].rawData;
}

return this.originalData.rawData[this.currData.table[rowNum].row];
}

}
7 changes: 5 additions & 2 deletions src/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export default function tableCreator(
value,
depth,
type: 'colHeader',
row: depth,
};
});

Expand All @@ -179,7 +180,7 @@ export default function tableCreator(

(function recurseThroughMap(dataPos, map) {
if (Array.isArray(dataPos)) {
console.log('data Depth', dataPos, depth);
// console.log('data Depth', dataPos, depth);
if (key === prevKey) {
let datum = dataRows[dataRows.length - 1].value;

Expand Down Expand Up @@ -210,6 +211,7 @@ export default function tableCreator(
value: datum,
type: 'data',
depth,
row: dataRows.length + formattedColumnHeaders.length,
});
}
} else {
Expand All @@ -220,13 +222,14 @@ export default function tableCreator(
})(recursedData, mapToHeader || 1);

} else {
console.log('header depth', key, depth)
// console.log('header depth', key, depth)
const value = [key].concat(Array(headerLength - 1).fill(''));

dataRows.push({
value,
depth,
type: 'rowHeader',
row: dataRows.length + formattedColumnHeaders.length,
});
rawData.push({
value,
Expand Down
38 changes: 26 additions & 12 deletions src/progressiveDiscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,42 @@ export function collapse(rowNum, data) {
const selectedRow = data.table[rowNum];
const {type, depth} = selectedRow;

if (type !== 'rowHeader' || type !== 'colHeader') {
return data;
if (type !== 'rowHeader' && type !== 'colHeader') {
return {
dataToReturn: data,
};
}

let count = rowNum + 1;
let currDepth = depth;
let currDepth = data.table[count].depth;
let dataToReturn = {
table: data.slice(0, count),
rawData: data.slice(0, count),
table: data.table.slice(0, count),
rawData: data.rawData.slice(0, count),
};

while (count < data.table.length || currDepth <= depth) {
let collapsed = {
table: [],
rawData: [],
};

while (count < data.table.length - 1 && currDepth > depth) {
collapsed.rawData.push(data.rawData[count]);
collapsed.table.push(data.table[count]);
count += 1;
currDepth = data.table[count].depth;
}
dataToReturn.table = dataToReturn.table.concat(data.table.slice(count));
dataToReturn.rawData = dataToReturn.rawData.concat(data.rawData.slice(count));

dataToReturn.table.concat(data.slice(count));
dataToReturn.rawData.concat(data.slice(count));

return dataToReturn;
return {
dataToReturn,
collapsed,
};
}

export function expand(rowNum, data) {

export function expand(rowNum, currData, collapsedRows) {
if (!collapsedRows) return currData;
currData.table.splice(rowNum + 1, 0, ...collapsedRows.table);
currData.rawData.splice(rowNum + 1, 0, ...collapsedRows.rawData);
return currData;
}
12 changes: 9 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var dataArray = [
['Joffrey', 'm', 'Baratheon', 18],
['Bran', 'm', 'Stark', 8],
['Jaime', 'm', 'Lannister', 32],
['Sansa', 'f', 'Stark', 12]
['Sansa', 'f', 'Stark', 12],
];

var rowsToPivot = ['house', 'gender', 'name'];
var rowsToPivot = ['house', 'gender'];
var colsToPivot = ['house'];
var aggregationCategory = 'age';
var aggregationType = 'sum';
Expand All @@ -33,6 +33,12 @@ var pivot = new Pivot(

describe('this is a test', function() {
it('is this a pivot?', function() {
console.log('testing', pivot.originalData.table);
// console.log('testing', pivot.originalData.table);
// pivot.collapse(1).collapse(2);
// pivot.collapse(1).expand(1)
pivot.collapse(1);
// console.log('table', pivot.originalData.table);
// console.log('data', pivot.originalData.rawData)
console.log('this is data', pivot.getData(1))
});
});

0 comments on commit af4eaec

Please sign in to comment.