Skip to content

Commit

Permalink
ignore null, undefined and NaN in extent checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Jun 17, 2020
1 parent 6c254a7 commit 8cb8f34
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/lib/calcExtents.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import canBeZero from '../utils/canBeZero.js';

/* --------------------------------------------
*
* Calculate the extents of desired fields
Expand All @@ -20,7 +22,11 @@ export default function calcExtents (data, fields) {
if (fl) {
for (i = 0; i < fl; i += 1) {
const firstRow = fields[i].accessor(data[0]);
extents[fields[i].field] = Array.isArray(firstRow) ? firstRow : [firstRow, firstRow];
if (firstRow === undefined || firstRow === null || Number.isNaN(firstRow) === true) {
extents[fields[i].field] = [Infinity, -Infinity];
} else {
extents[fields[i].field] = Array.isArray(firstRow) ? firstRow : [firstRow, firstRow];
}
}
const dl = data.length;
for (i = 0; i < dl; i += 1) {
Expand All @@ -31,7 +37,7 @@ export default function calcExtents (data, fields) {
if (Array.isArray(val)) {
const vl = val.length;
for (let k = 0; k < vl; k += 1) {
if (val[k] !== undefined) {
if (val[k] !== undefined && val[k] !== null && Number.isNaN(val[k]) === false) {
if (val[k] < extents[s][0]) {
extents[s][0] = val[k];
}
Expand All @@ -40,7 +46,7 @@ export default function calcExtents (data, fields) {
}
}
}
} else if (val !== undefined) {
} else if (val !== undefined && val !== null && Number.isNaN(val) === false) {
if (val < extents[s][0]) {
extents[s][0] = val;
}
Expand Down
30 changes: 30 additions & 0 deletions test/calcExtents.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@ const tests = [
], [{ field: 'x', accessor: d => d.x }]],
expected: { x: [0, 4] }
},
{
args: [[
{}, { x: 0, y: 1 }, { x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }, { x: 4, y: 5 }
], [{ field: 'x', accessor: d => d.x }]],
expected: { x: [0, 4] }
},
{
args: [[
{ x: null }, { x: 0, y: 1 }, { x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }, { x: 4, y: 5 }
], [{ field: 'x', accessor: d => d.x }]],
expected: { x: [0, 4] }
},
{
args: [[
{ x: 'd' / 1 }, { x: 0, y: 1 }, { x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }, { x: 4, y: 5 }
], [{ field: 'x', accessor: d => d.x }]],
expected: { x: [0, 4] }
},
{
args: [[
{ x: NaN }, { x: 0, y: 1 }, { x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }, { x: 4, y: 5 }
], [{ field: 'x', accessor: d => d.x }]],
expected: { x: [0, 4] }
},
{
args: [[
{ x: Number.NaN }, { x: 0, y: 1 }, { x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }, { x: 4, y: 5 }
], [{ field: 'x', accessor: d => d.x }]],
expected: { x: [0, 4] }
},
{
args: [[
{ x: '2010-01-04' }, { x: '2010-01-02' }, { x: '2010-01-04' }, { x: '2010-01-05' }, { x: '2010-01-06' }
Expand Down

0 comments on commit 8cb8f34

Please sign in to comment.