Skip to content

Commit

Permalink
Merge pull request #49 from pat310/44filteringOutAllData
Browse files Browse the repository at this point in the history
44filtering out all data
  • Loading branch information
pat310 committed Apr 17, 2017
2 parents 679c24c + 084c5ea commit 08fe5f0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Returns all the unique values for a particular field as an array

#### `.filter([fieldName or CBfunction], filterValues, [filterType])`
Filters out values based on either:
- string `fieldName` field to filter on, array `filterValues` values to filter, string `filterType` option enumerated string either `'include'` or `'exclude'` (defaults to exclude if not provided)
- string `fieldName` field to filter on, array `filterValues` values to filter, string `filterType` optional enumerated string either `'include'` or `'exclude'` (defaults to exclude if not provided)
- function `CBfunction(element, index, array)` which iterates over each element in array (similar to Javascript array `.filter` method)


Expand Down
7 changes: 6 additions & 1 deletion src/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ export function tableCreator(data, rows = [], cols = [], accCatOrCB,
accTypeOrInitVal, rowHeader) {

/** if data is empty, return empty array */
if (data.length === 0) return [];
if (data.length === 0) {
return {
rawData: [],
table: [],
};
};

/** if rows/cols are not arrays, return throw an error */
if (!Array.isArray(rows) || !Array.isArray(cols)) {
Expand Down
34 changes: 34 additions & 0 deletions test/filtering/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,39 @@ export default () => {

expect(filteredResults).to.deep.equal(expectedResults);
});

it('should work if pivot filters out all the results', () => {
const results = filter(data, 'gender', ['m', 'f'], 'exclude');

expect(results).to.deep.equal([]);
});

it('should return an empty array if including a value that does not ' +
'exist', () => {
const results = filter(data, 'gender', ['dogman'], 'include');

expect(results).to.deep.equal([]);
});

it('should return all values if excluding a value that does not ' +
'exist', () => {
const results = filter(data, 'gender', ['dogman'], 'exclude');

expect(results).to.deep.equal(data);
});

it('should return an empty array if including on a dimension that does not ' +
'exist', () => {
const results = filter(data, 'dogman', ['dogman'], 'include');

expect(results).to.deep.equal([]);
});

it('should return all values if excluding on a dimension that does not ' +
'exist', () => {
const results = filter(data, 'dogman', ['dogman'], 'exclude');

expect(results).to.deep.equal(data);
});
};

6 changes: 6 additions & 0 deletions test/logic/tableCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,10 @@ export default () => {
expect(colError).to.throw(Error);
});

it('should return empty rawData and empty table if data is empty', () => {
const results = tableCreator([], ['house'], ['gender'], 'age', 'count');

expect(results).to.deep.equal({table: [], rawData: []});
});

};

0 comments on commit 08fe5f0

Please sign in to comment.