Skip to content

Commit

Permalink
fix: empty domain error for ordinal x scale (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme authored Feb 5, 2020
1 parent cc561a4 commit ce4e84f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/utils/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import {
} from './domain';

describe('utils/domain', () => {
test('should return [0] domain if no data', () => {
const data: any[] = [];
const accessor: AccessorFn = (datum: any) => datum.x;
const isSorted = true;
const removeNull = true;

const ordinalDataDomain = computeOrdinalDataDomain(data, accessor, isSorted, removeNull);

expect(ordinalDataDomain).toEqual([0]);
});

test('should compute ordinal data domain: sort & remove nulls', () => {
const data = [{ x: 'd' }, { x: 'a' }, { x: null }, { x: 'b' }];
const accessor: AccessorFn = (datum: any) => datum.x;
Expand Down
5 changes: 5 additions & 0 deletions src/utils/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export function computeOrdinalDataDomain(
sorted?: boolean,
removeNull?: boolean,
): string[] | number[] {
// TODO: Check for empty data before computing domain
if (data.length === 0) {
return [0];
}

const domain = data.map(accessor).filter((d) => (removeNull ? d !== null : true));
const uniqueValues = [...new Set(domain)];
return sorted
Expand Down

0 comments on commit ce4e84f

Please sign in to comment.