Skip to content

Commit

Permalink
add aggregation types
Browse files Browse the repository at this point in the history
  • Loading branch information
turnerniles committed Mar 9, 2017
1 parent b19de9c commit d8fef19
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 27 deletions.
29 changes: 26 additions & 3 deletions src/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ export function accumulator(arr, accCat, accType, accValue) {
return arr.reduce((acc, curr, index, array) => {
if (typeof accType === 'function') {
return accType(acc, typeof accCat === 'string' ? curr[accCat] :
curr, index, array);
curr, index, array);
}
switch (accType) {
case ('sum'): {
acc += Number(curr[accCat]);
case ('average'): {
acc += Number(curr[accCat]) / arr.length;
return acc;
}

Expand All @@ -120,6 +120,29 @@ export function accumulator(arr, accCat, accType, accValue) {
return acc;
}

case ('min'): {
if (index === 0) {
acc = Number(curr[accCat]);
} else if (curr[accCat] < acc) {
acc = Number(curr[accCat]);
}
return acc;
}

case ('max'): {
if (index === 0) {
acc = Number(curr[accCat]);
} else if (curr[accCat] > acc) {
acc = Number(curr[accCat]);
}
return acc;
}

case ('sum'): {
acc += Number(curr[accCat]);
return acc;
}

default: {
acc += 1;
return acc;
Expand Down
134 changes: 112 additions & 22 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ const dataArray = [
['Jaime', 'm', 'Lannister', 32],
['Sansa', 'f', 'Stark', 12],
];
const rowsToPivot = ['gender', 'name'];
const colsToPivot = ['house'];
const rowsToPivotTestOne = ['gender', 'name'];
const colsToPivotTestOne = ['house'];
const rowsToPivotTestTwo = ['name'];
const colsToPivotTestTwo = ['house', 'gender'];
const aggregationCategory = 'age';
const aggregationType = 'sum';

Expand All @@ -33,8 +35,8 @@ describe('pivot', () => {
it('should create a pivot when passed data', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand All @@ -47,8 +49,8 @@ describe('pivot', () => {
it('should collapse a header row when passed the index', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand Down Expand Up @@ -145,8 +147,8 @@ describe('pivot', () => {
it('should not collapse a data row', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand All @@ -166,8 +168,8 @@ describe('pivot', () => {
it('can chain to collapse multiple rows', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand Down Expand Up @@ -199,8 +201,8 @@ describe('pivot', () => {
() => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand Down Expand Up @@ -233,8 +235,8 @@ describe('pivot', () => {
it('should return data of a collapsed row', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand Down Expand Up @@ -311,8 +313,8 @@ describe('pivot', () => {
it('should not expand a row that is not collapsed', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand All @@ -330,8 +332,8 @@ describe('pivot', () => {
it('should return table to normal state when completely expanded', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand All @@ -351,8 +353,8 @@ describe('pivot', () => {
it('should expand correct rows', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);
Expand Down Expand Up @@ -455,15 +457,103 @@ describe('pivot', () => {
it('should return null if row does not exist', () => {
const pivot = new Pivot(
dataArray,
rowsToPivot,
colsToPivot,
rowsToPivotTestOne,
colsToPivotTestOne,
aggregationCategory,
aggregationType,
);

pivot.collapse(1).collapse(2).expand(1);
expect(pivot.getData(9)).to.be.null;
});

it('should pivot correctly provided multiple columns to pivot', () => {
const pivot = new Pivot(
dataArray,
rowsToPivotTestTwo,
colsToPivotTestTwo,
aggregationCategory,
aggregationType,
);

const expectedTable = [
{
value:
[ 'sum age',
'Stark',
'Stark',
'Baratheon',
'Baratheon',
'Lannister',
],
depth: 0,
type: 'colHeader',
row: 0,
},
{
value: [ 'sum age', 'm', 'f', 'f', 'm', 'm' ],
depth: 1,
type: 'colHeader',
row: 1,
},
{
value: [ 'Jon', 14, '', '', '', '' ],
type: 'data',
depth: 0,
row: 2,
},
{
value: [ 'Arya', '', 10, '', '', '' ],
type: 'data',
depth: 0,
row: 3,
},
{
value: [ 'Cersei', '', '', 38, '', '' ],
type: 'data',
depth: 0,
row: 4,
},
{
value: [ 'Tywin', '', '', '', '', 67 ],
type: 'data',
depth: 0,
row: 5,
},
{
value: [ 'Tyrion', '', '', '', '', 34 ],
type: 'data',
depth: 0,
row: 6,
},
{
value: [ 'Joffrey', '', '', '', 18, '' ],
type: 'data',
depth: 0,
row: 7,
},
{
value: [ 'Bran', 8, '', '', '', '' ],
type: 'data',
depth: 0,
row: 8,
},
{
value: [ 'Jaime', '', '', '', '', 32 ],
type: 'data',
depth: 0,
row: 9,
},
{
value: [ 'Sansa', '', 12, '', '', '' ],
type: 'data',
depth: 0,
row: 10,
},
];

expect(pivot.data.table).to.deep.equal(expectedTable);
});
});

});
24 changes: 22 additions & 2 deletions test/logic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,33 @@ describe('logic functions', () => {
describe('accumulator', () => {
describe('accumulation category check',
() => {
it('average', () => {
const accumulatedResults = accumulator(data, 'age', 'average');

expect(accumulatedResults).to.equal(28.75);
});
it('count', () => {
const accumulatedResults = accumulator(data, 'age', 'count');

expect(accumulatedResults).to.equal(8);
});
it('min', () => {
const accumulatedResults = accumulator(data, 'age', 'min');

expect(accumulatedResults).to.equal(28);
});
it('max', () => {
const accumulatedResults = accumulator(data, 'age', 'max');

expect(accumulatedResults).to.equal(30);
});
it('sum', () => {
const accumulatedResults = accumulator(data, 'age', 'sum');

expect(accumulatedResults).to.equal(230);
});
it('count', () => {
const accumulatedResults = accumulator(data, 'age', 'count');
it('default', () => {
const accumulatedResults = accumulator(data, 'age', 'default');

expect(accumulatedResults).to.equal(8);
});
Expand Down

0 comments on commit d8fef19

Please sign in to comment.