Skip to content

Commit

Permalink
fix: Fix spread column generation. (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Jun 11, 2021
1 parent f79ca3f commit f6609d8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/engine/spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ export default function(table, { names, exprs, ops = [] }, options = {}) {
function spread(table, get, limit) {
const nrows = table.totalRows();
const columns = [];
let j = -1;

table.scan((row, data) => {
const values = toArray(get(row, data));
const n = Math.min(values.length, limit);
while (++j < n) {
while (columns.length < n) {
columns.push(Array(nrows).fill(NULL));
}
for (let i = 0; i < n; ++i) {
Expand Down
32 changes: 31 additions & 1 deletion test/verbs/spread-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,34 @@ tape('spread ignores as option with multi column input', t => {
b_2: [ 'bop', 'baz', undefined, 'bar' ]
}, 'spread data with as');
t.end();
});
});

tape('spread handles arrays of varying length', t => {
const data1 = {
u: [
['A', 'B', 'C'],
['D', 'E']
]
};
const data2 = {
u: data1.u.slice().reverse()
};
const obj = [
{ u_1: 'A', u_2: 'B', u_3: 'C' },
{ u_1: 'D', u_2: 'E', u_3: undefined }
];

t.deepEqual(
table(data1).spread('u').objects(),
obj,
'spread data, larger first'
);

t.deepEqual(
table(data2).spread('u').objects(),
obj.slice().reverse(),
'spread data, smaller first'
);

t.end();
});

0 comments on commit f6609d8

Please sign in to comment.