From f6609d8318490b26527bade89ed002d6774047d8 Mon Sep 17 00:00:00 2001 From: jheer Date: Fri, 11 Jun 2021 13:41:44 +0200 Subject: [PATCH] fix: Fix spread column generation. (#209) --- src/engine/spread.js | 3 +-- test/verbs/spread-test.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/engine/spread.js b/src/engine/spread.js index dc869c35..f12d1e78 100644 --- a/src/engine/spread.js +++ b/src/engine/spread.js @@ -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) { diff --git a/test/verbs/spread-test.js b/test/verbs/spread-test.js index b3a81ef4..a1a3e5e8 100644 --- a/test/verbs/spread-test.js +++ b/test/verbs/spread-test.js @@ -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(); -}); \ No newline at end of file +}); + +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(); +});