Skip to content

Commit

Permalink
Merge branch 'master' into 63bumpVersionMinor
Browse files Browse the repository at this point in the history
  • Loading branch information
pat310 authored Nov 9, 2017
2 parents 6ccc4f2 + 65c8336 commit 06cb287
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 50 deletions.
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [v2.3.0]
> Nov 9, 2017
- Adding an aggregated row to the `data.table` object that aggregates all previous data rows using the selected aggregation function [PR #62](https://github.com/pat310/quick-pivot/pull/62)

## [v2.2.7]
> Aug 10, 2017
Expand Down Expand Up @@ -81,4 +86,4 @@
- `.expand` - expands a collapsed row
- `.getData` - returns the data within a collapsed row

[#18]: https://github.com/pat310/quick-pivot/pull/18
[#18]: https://github.com/pat310/quick-pivot/pull/18
122 changes: 76 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ pivot.data
{ value: [Object], type: 'data', depth: 0, row: 7 },
{ value: [Object], type: 'data', depth: 0, row: 8 },
{ value: [Object], type: 'data', depth: 0, row: 9 },
{ value: [Object], type: 'data', depth: 0, row: 10 } ],
{ value: [Object], type: 'data', depth: 0, row: 10 },
{ value: [Object], type: 'aggregated' } ],
rawData:
[ { value: [Object], depth: 0, type: 'colHeader', row: 0 },
{ value: [Object], depth: 1, type: 'colHeader', row: 1 },
Expand Down Expand Up @@ -130,12 +131,14 @@ pivot.data.table
{ value: [ 'Sansa', '', 12, '', '', '' ],
type: 'data',
depth: 0,
row: 10 } ]
row: 10 },
{ value: [ '', 22, 22, 38, 18, 133 ],
type: 'aggregated' } ]
```

## API
### Pivot `data` value
The `data` value returns an object with keys `table` and `rawData`. `table` is an array of objects with each object containing four keys:
The `data` value returns an object with keys `table` and `rawData`. `table` is an array of objects with each object containing four keys (except for the last object which is an aggregated row of all the previous data rows based on the selected aggregation function):
1. `value` - Array which contains the result of the pivot to be rendered
2. `type` - Enumerated string describing what this data row contains, [`data`, `rowHeader`, or `colHeader`]
3. `depth` - Number describing how deeply nested the row is within a parent row
Expand Down Expand Up @@ -219,8 +222,13 @@ console.log(pivot.data.table);
depth: 0,
type: 'colHeader',
row: 0 },
{ value: [ 'm', 11, 18, 44.333333333333336 ], type: 'data', depth: 0, row: 1 }, { value: [ 'f', 11, 38, '' ], type: 'data', depth: 0, row: 2 }
]
{ value: [ 'f', 11, 38, '' ], type: 'data', depth: 0, row: 1 },
{ value: [ 'm', 11, 18, 44.333333333333336 ],
type: 'data',
depth: 0,
row: 2 },
{ value: [ '', 11, 28, 44.333333333333336 ],
type: 'aggregated' } ]
*/

pivot.update(dataArray, ['gender', 'name'], ['house'], cbFunc, 0, 'average age')
Expand All @@ -231,35 +239,37 @@ console.log(pivot.data.table);
depth: 0,
type: 'colHeader',
row: 0 },
{ value: [ 'm', 11, 18, 44.333333333333336 ],
{ value: [ 'f', 11, 38, '' ],
depth: 0,
type: 'rowHeader',
row: 1 },
{ value: [ 'Jon', 14, '', '' ], type: 'data', depth: 1, row: 2 },
{ value: [ 'Tywin', '', '', 67 ], type: 'data', depth: 1, row: 3 },
{ value: [ 'Tyrion', '', '', 34 ],
type: 'data',
depth: 1,
row: 4 },
{ value: [ 'Joffrey', '', 18, '' ],
{ value: [ 'Arya', 10, '', '' ], type: 'data', depth: 1, row: 2 },
{ value: [ 'Cersei', '', 38, '' ],
type: 'data',
depth: 1,
row: 3 },
{ value: [ 'Sansa', 12, '', '' ], type: 'data', depth: 1, row: 4 },
{ value: [ 'm', 11, 18, 44.333333333333336 ],
depth: 0,
type: 'rowHeader',
row: 5 },
{ value: [ 'Bran', 8, '', '' ], type: 'data', depth: 1, row: 6 },
{ value: [ 'Jaime', '', '', 32 ], type: 'data', depth: 1, row: 7 },
{ value: [ 'f', 11, 38, '' ],
depth: 0,
type: 'rowHeader',
{ value: [ 'Joffrey', '', 18, '' ],
type: 'data',
depth: 1,
row: 8 },
{ value: [ 'Arya', 10, '', '' ], type: 'data', depth: 1, row: 9 },
{ value: [ 'Cersei', '', 38, '' ],
{ value: [ 'Jon', 14, '', '' ], type: 'data', depth: 1, row: 9 },
{ value: [ 'Tyrion', '', '', 34 ],
type: 'data',
depth: 1,
row: 10 },
{ value: [ 'Sansa', 12, '', '' ],
{ value: [ 'Tywin', '', '', 67 ],
type: 'data',
depth: 1,
row: 11 } ]
row: 11 },
{ value: [ '', 11, 28, 44.333333333333336 ],
type: 'aggregated' } ]
*/

pivot.collapse(1);
Expand Down Expand Up @@ -291,20 +301,41 @@ console.log(pivot.data.table);

console.log(pivot.getData(1));
/*
[ { value: [ 'Jon', [Object], '', '' ], type: 'data', depth: 1 },
{ value: [ 'Tywin', '', '', [Object] ], type: 'data', depth: 1 },
{ value: [ 'Tyrion', '', '', [Object] ], type: 'data', depth: 1 },
{ value: [ 'Joffrey', '', [Object], '' ],
[ { value: [ 'average age', 'Stark', 'Baratheon', 'Lannister' ],
depth: 0,
type: 'colHeader',
row: 0 },
{ value: [ 'f', 11, 38, '' ],
depth: 0,
type: 'rowHeader',
row: 1 },
{ value: [ 'm', 11, 18, 44.333333333333336 ],
depth: 0,
type: 'rowHeader',
row: 5 },
{ value: [ 'Bran', 8, '', '' ], type: 'data', depth: 1, row: 6 },
{ value: [ 'Jaime', '', '', 32 ], type: 'data', depth: 1, row: 7 },
{ value: [ 'Joffrey', '', 18, '' ],
type: 'data',
depth: 1,
row: 8 },
{ value: [ 'Jon', 14, '', '' ], type: 'data', depth: 1, row: 9 },
{ value: [ 'Tyrion', '', '', 34 ],
type: 'data',
depth: 1,
row: 10 },
{ value: [ 'Tywin', '', '', 67 ],
type: 'data',
depth: 1 },
{ value: [ 'Bran', [Object], '', '' ], type: 'data', depth: 1 },
{ value: [ 'Jaime', '', '', [Object] ], type: 'data', depth: 1 } ]
depth: 1,
row: 11 },
{ value: [ '', 11, 28, 44.333333333333336 ],
type: 'aggregated' } ]
*/

console.log(pivot.getData(1)[0].value)
/*
[ 'Jon',
[ { name: 'Jon', gender: 'm', house: 'Stark', age: 14 } ],
[ 'Arya',
[ { name: 'Arya', gender: 'f', house: 'Stark', age: 10 } ],
'',
'' ]
*/
Expand All @@ -317,44 +348,43 @@ console.log(pivot.data.table);
depth: 0,
type: 'colHeader',
row: 0 },
{ value: [ 'm', 11, 18, 44.333333333333336 ],
{ value: [ 'f', 11, 38, '' ],
depth: 0,
type: 'rowHeader',
row: 1 },
{ value: [ 'f', 11, 38, '' ],
{ value: [ 'm', 11, 18, 44.333333333333336 ],
depth: 0,
type: 'rowHeader',
row: 8 } ]
row: 5 },
{ value: [ '', 11, 28, 44.333333333333336 ],
type: 'aggregated' } ]
*/

pivot.expand(1);
console.log(pivot.data.table);
/*
[ { value: [ 'average age', 'Stark', 'Baratheon', 'Lannister' ],
depth: 0,
type: 'colHeader',
row: 0 },
{ value: [ 'm', 11, 18, 44.333333333333336 ],
{ value: [ 'f', 11, 38, '' ],
depth: 0,
type: 'rowHeader',
row: 1 },
{ value: [ 'Jon', 14, '', '' ], type: 'data', depth: 1, row: 2 },
{ value: [ 'Tywin', '', '', 67 ], type: 'data', depth: 1, row: 3 },
{ value: [ 'Tyrion', '', '', 34 ],
type: 'data',
depth: 1,
row: 4 },
{ value: [ 'Joffrey', '', 18, '' ],
{ value: [ 'Arya', 10, '', '' ], type: 'data', depth: 1, row: 2 },
{ value: [ 'Cersei', '', 38, '' ],
type: 'data',
depth: 1,
row: 5 },
{ value: [ 'Bran', 8, '', '' ], type: 'data', depth: 1, row: 6 },
{ value: [ 'Jaime', '', '', 32 ], type: 'data', depth: 1, row: 7 },
{ value: [ 'f', 11, 38, '' ],
row: 3 },
{ value: [ 'Sansa', 12, '', '' ], type: 'data', depth: 1, row: 4 },
{ value: [ 'm', 11, 18, 44.333333333333336 ],
depth: 0,
type: 'rowHeader',
row: 8 } ]
row: 5 },
{ value: [ '', 11, 28, 44.333333333333336 ],
type: 'aggregated' } ]
*/
```

## Changes
Check out the [change log](/CHANGES.md)
Check out the [change log](/CHANGES.md)
34 changes: 32 additions & 2 deletions src/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,39 @@ export function tableCreator(data, rows = [], cols = [], accCatOrCB,
});
}

function completedTableAccumulator(rows) {
const filteredRows = rows.reduce((acc, { type, value }) => {
if (acc.length === 0) {
acc = Array(value.length).fill([]);
}

if (type === 'data') {
value.forEach((valueElem, i) => {
if (Array.isArray(valueElem)) {
acc[i] = acc[i].concat(valueElem);
}
});
}

return acc;
}, []);

return filteredRows.map((accumulatedRawData) => {
if (accumulatedRawData.length > 0) {
return accumulator(accumulatedRawData, accCatOrCB, accTypeOrInitVal);
}

return '';
});
}

const accumulatedRows = {
value: completedTableAccumulator(rawData),
type: 'aggregated',
};

return {
table: formattedColumnHeaders.concat(dataRows),
table: formattedColumnHeaders.concat(dataRows, accumulatedRows),
rawData: formattedColumnHeaders.concat(rawData),
};

}
12 changes: 12 additions & 0 deletions test/index/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export default () => {
type: 'rowHeader',
row: 5,
},
{
type: 'aggregated',
value: ['', 44, 56, 133],
},
];

const expectedRawData = [
Expand Down Expand Up @@ -149,6 +153,10 @@ export default () => {
type: 'rowHeader',
row: 5,
},
{
type: 'aggregated',
value: ['', 44, 56, 133],
},
];

expect(pivot.data.table).to.deep.equal(expectedTable);
Expand Down Expand Up @@ -182,6 +190,10 @@ export default () => {
type: 'rowHeader',
row: 5,
},
{
type: 'aggregated',
value: ['', 44, 56, 133],
},
];

expect(pivot.data.table).to.deep.equal(expectedTable);
Expand Down
8 changes: 8 additions & 0 deletions test/index/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export default () => {
depth: 0,
type: 'rowHeader',
row: 5 },
{
type: 'aggregated',
value: ['', 44, 56, 133],
},
];

const expectedCollapsedResult = [
Expand Down Expand Up @@ -244,6 +248,10 @@ export default () => {
depth: 0,
row: 10,
},
{
type: 'aggregated',
value: ['', 22, 22, 38, 18, 133],
},
];

expect(pivot.data.table).to.deep.equal(expectedTable);
Expand Down
8 changes: 8 additions & 0 deletions test/index/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default () => {
{ value: [ 'Sansa', 12 ], type: 'data', depth: 2, row: 14 },
{ value: [ 'm', 8 ], depth: 1, type: 'rowHeader', row: 15 },
{ value: [ 'Bran', 8 ], type: 'data', depth: 2, row: 16 },
{ type: 'aggregated', value: ['', 219] },
];

pivot.filter('name', ['Jon'], 'exclude');
Expand Down Expand Up @@ -103,6 +104,7 @@ export default () => {
{ value: [ 'Sansa', 12 ], type: 'data', depth: 2, row: 14 },
{ value: [ 'm', 8 ], depth: 1, type: 'rowHeader', row: 15 },
{ value: [ 'Bran', 8 ], type: 'data', depth: 2, row: 16 },
{type: 'aggregated', value: ['', 219] },
];

pivot.filter('name', ['Jon']);
Expand All @@ -128,6 +130,7 @@ export default () => {
{ value: [ 'm', 22 ], depth: 1, type: 'rowHeader', row: 5 },
{ value: [ 'Bran', 8 ], type: 'data', depth: 2, row: 6 },
{ value: [ 'Jon', 14 ], type: 'data', depth: 2, row: 7 },
{ type: 'aggregated', value: ['', 44] },
];

function filterFunc(dataRow) {
Expand Down Expand Up @@ -166,6 +169,7 @@ export default () => {
row: 6 },
{ value: [ 'm', 133 ], depth: 1, type: 'rowHeader', row: 7 },
{ value: [ 'Stark', 30 ], depth: 0, type: 'rowHeader', row: 11 },
{ type: 'aggregated', value: ['', 219] },
];

pivot.collapse(11).collapse(2).collapse(6);
Expand All @@ -187,6 +191,7 @@ export default () => {
const expectedResult = [
{ value: [ 'sum age', 'sum age' ], depth: 0, type: 'colHeader', row: 0 },
{ value: [ 'Stark', 44 ], depth: 0, type: 'rowHeader', row: 1 },
{ type: 'aggregated', value: ['', 44] },
];

function filterFunc(dataRow) {
Expand Down Expand Up @@ -214,6 +219,7 @@ export default () => {
{ value: [ 'Baratheon', 38 ], depth: 0, type: 'rowHeader', row: 1 },
{ value: [ 'f', 38 ], depth: 1, type: 'rowHeader', row: 2 },
{ value: [ 'Cersei', 38 ], type: 'data', depth: 2, row: 3 },
{ type: 'aggregated', value: ['', 38] },
];

pivot.filter('house', ['Stark'], 'exclude')
Expand Down Expand Up @@ -254,6 +260,7 @@ export default () => {
{ value: [ 'm', 22 ], depth: 1, type: 'rowHeader', row: 11 },
{ value: [ 'Bran', 8 ], type: 'data', depth: 2, row: 12 },
{ value: [ 'Jon', 14 ], type: 'data', depth: 2, row: 13 },
{ type: 'aggregated', value: ['', 96] },
];

pivot.filter('name', ['Cersei'], 'exclude')
Expand Down Expand Up @@ -298,6 +305,7 @@ export default () => {
{ value: [ 'm', 22 ], depth: 1, type: 'rowHeader', row: 11 },
{ value: [ 'Bran', 8 ], type: 'data', depth: 2, row: 12 },
{ value: [ 'Jon', 14 ], type: 'data', depth: 2, row: 13 },
{ type: 'aggregated', value: ['', 96] },
];

pivot.filter('name', ['Cersei'], 'exclude')
Expand Down
1 change: 1 addition & 0 deletions test/index/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default () => {
{ value: [ 'Jaime', '', 32, '' ], type: 'data', depth: 1, row: 5 },
{ value: [ 'Joffrey', '', '', 18 ], type: 'data', depth: 1, row: 6 },
{ value: [ 'Tyrion', '', 34, '' ], type: 'data', depth: 1, row: 7 },
{ type: 'aggregated', value: ['', 20, 66, 18] },
];

const pivot = new Pivot(
Expand Down
Loading

0 comments on commit 06cb287

Please sign in to comment.