Skip to content

Commit

Permalink
allow for string values in extent calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Oct 18, 2018
1 parent 9c5724d commit 9a50fd9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/lib/calcExtents.js
Expand Up @@ -15,11 +15,11 @@ export default function calcExtents (data, fields) {
let val;
let s;

for (i = 0; i < fl; i++) {
extents[fields[i].field] = [Infinity, -Infinity];
}

if (fl) {
for (i = 0; i < fl; i++) {
const firstRow = fields[i].accessor(data[0]);
extents[fields[i].field] = Array.isArray(firstRow) ? firstRow : [firstRow, firstRow];
}
const dl = data.length;
for (i = 0; i < dl; i++) {
for (j = 0; j < fl; j++) {
Expand Down Expand Up @@ -48,6 +48,8 @@ export default function calcExtents (data, fields) {
}
}
}
} else {
return null;
}
return extents;
};
9 changes: 8 additions & 1 deletion test/calcExtents.js
Expand Up @@ -7,12 +7,19 @@ const name = 'calcExtents';
const tests = [
{ args: [[]], expected: null },
{ args: [{}], expected: null },
{ args: [[0, 1, 2], []], expected: null },
{
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: '2010-01-04'}, {x: '2010-01-02'}, {x: '2010-01-04'}, {x: '2010-01-05'}, {x: '2010-01-06'}
], [{field: 'x', accessor: d => d.x}]],
expected: {x: ['2010-01-02', '2010-01-06']}
},
{
args: [[
{x: 0, y: 1}, {x: 1, y: 2}, {x: 2, y: 3}, {x: 3, y: 4}, {x: 4, y: 5}
Expand All @@ -21,7 +28,7 @@ const tests = [
},
{
args: [[
{x: [-5, 0], y: [1, 6]}, {x: [-4, 1], y: [2, 7]}, {x: [-3, 2], y: [3, 8]}, {x: [-2, 3], y: [4, 9]}, {x: [-1, 4], y: [5, 10]}
{x: [-4, 0], y: [1, 6]}, {x: [-5, 1], y: [2, 7]}, {x: [-3, 2], y: [3, 8]}, {x: [-2, 3], y: [4, 9]}, {x: [-1, 4], y: [5, 10]}
], [{field: 'x', accessor: d => d.x}, {field: 'y', accessor: d => d.y}]],
expected: {x: [-5, 4], y: [1, 10]}
}
Expand Down

0 comments on commit 9a50fd9

Please sign in to comment.