Skip to content

Commit

Permalink
Merge pull request #13 from pat310/12MaximumCallStack
Browse files Browse the repository at this point in the history
throwing error if pivot category does not exist
  • Loading branch information
pat310 committed Jan 28, 2017
2 parents 714ae1b + 51f0b33 commit dd92522
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
15 changes: 14 additions & 1 deletion logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,20 @@ function accumulator(arr, accCat, accType, accValue){
}, accValue || 0);
}

function checkPivotCategories(actualCats, selectedCats){
var errMessage = [];
selectedCats.forEach(selectedCat =>{
if(actualCats.indexOf(selectedCat) === -1) errMessage.push(selectedCat);
});
if(errMessage.length) throw new Error(`Check that these selected pivot categories exist: ${errMessage.join(',')}`);
}

function tableCreator(data, rows = [], cols = [], accCatOrCB, accTypeOrInitVal, rowHeader){
data = fixDataFormat(data);
if(!data.length) return [];
checkPivotCategories(Object.keys(data[0]), rows);
checkPivotCategories(Object.keys(data[0]), cols);

if(typeof rowHeader === 'undefined') rowHeader = typeof accCatOrCB !== 'function' ? `${accTypeOrInitVal} ${accCatOrCB}` : 'Custom Agg';

const columnData = createColumnHeaders(data, cols, rowHeader);
Expand Down Expand Up @@ -190,5 +202,6 @@ module.exports = {
groupByCategory,
groupByCategories,
createColumnHeaders,
accumulator
accumulator,
checkPivotCategories
};
16 changes: 15 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const {
groupByCategories,
createColumnHeaders,
accumulator,
tableCreator
tableCreator,
checkPivotCategories
} = require('./logic');

const chai = require('chai');
Expand Down Expand Up @@ -253,6 +254,19 @@ describe('accumulator', function(){
});
});

describe('checkPivotCategories', function(){
var actualCategories = ['pig', 'fish', 'dog'];

it('should return undefined if pivot category exists', function(){
expect(checkPivotCategories(actualCategories, ['dog'])).to.be.undefined;
});

it('should throw an error if pivot category does not exist', function(){
var errorFunc = checkPivotCategories.bind(null, actualCategories, ['cat']);
expect(errorFunc).to.throw(Error);
});
});

describe('tableCreator', function(){
it('should take an array of objects, an array of row categories, an array of column categories, an accumulation category, and an accumulation type and return an object containing the table and data comprising the table', function(){
var tableResults = tableCreator(data, ['gender'], ['borough'], 'age', 'count');
Expand Down

0 comments on commit dd92522

Please sign in to comment.