Skip to content

Commit

Permalink
fix: Fix relocate logic bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Dec 9, 2020
1 parent 7d31530 commit 3a05659
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/verbs/derive.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function(table, values, options = {}) {
return options.before == null && options.after == null
? dt
: relocate(dt,
Object.keys(values).filter(name => table.columnIndex(name) >= 0),
Object.keys(values).filter(name => table.columnIndex(name) < 0),
options
);
}
4 changes: 2 additions & 2 deletions src/verbs/relocate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export default function(table, columns, { before, after } = {}) {

// at anchor column, insert relocated columns
if (name === anchor) {
if (bef && assign) _cols[name] = name;
if (aft && assign) _cols[name] = name;
Object.assign(_cols, columns);
if (bef) return; // exit if current column has been handled
if (aft) return; // exit if current column has been handled
}

if (assign) _cols[name] = name;
Expand Down
32 changes: 28 additions & 4 deletions test/verbs/derive-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,48 @@ tape('derive can relocate new columns', t => {
b: [2, 4, 6, 8]
};

const t1 = table(data).derive({ z: d => d.a + d.b }, { before: 'a' });

tableEqual(t,
table(data).derive({ z: d => d.a + d.b }, { before: 'a' }),
t1,
{ z: [3, 7, 11, 15], ...data },
'derive data, with before'
);

t.deepEqual(
t1.columnNames(),
['z', 'a', 'b'],
'derive data columns, with before'
);

const t2 = table(data).derive({ z: d => d.a + d.b }, { after: 'a' });

tableEqual(t,
table(data).derive({ z: d => d.a + d.b }, { after: 'a' }),
t2,
{ a: data.a, z: [3, 7, 11, 15], b: data.b },
'derive data, with before'
'derive data, with after'
);

t.deepEqual(
t2.columnNames(),
['a', 'z', 'b'],
'derive data columns, with after'
);

const t3 = table(data).derive({ a: d => -d.a, z: d => d.a + d.b }, { after: 'b' });

tableEqual(t,
table(data).derive({ a: d => -d.a, z: d => d.a + d.b }, { after: 'b' }),
t3,
{ a: [-1, -3, -5, -7], b: data.b, z: [3, 7, 11, 15] },
'derive data, with after and overwrite'
);

t.deepEqual(
t3.columnNames(),
['a', 'b', 'z'],
'derive data columns, with after and overwrite'
);

t.end();
});

Expand Down
55 changes: 30 additions & 25 deletions test/verbs/relocate-test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import tape from 'tape';
import tableEqual from '../table-equal';
import { not, range, table } from '../../src/verbs';

tape('relocate repositions columns', t => {
const a = [1], b = [2], c = [3], d = [4];
const dt = table({ a, b, c, d });

tableEqual(t,
dt.relocate(not('b', 'd'), { before: 'b' }),
{ a, c, b, d },
t.deepEqual(
dt.relocate('a', { before: 'd' }).columnNames(),
['b', 'c', 'a', 'd'],
'relocate data, before'
);

tableEqual(t,
dt.relocate(not('b', 'd'), { after: 'd' }),
{ b, d, a, c },
t.deepEqual(
dt.relocate(not('b', 'd'), { before: 'b' }).columnNames(),
['a', 'c', 'b', 'd'],
'relocate data, before'
);

t.deepEqual(
dt.relocate(not('b', 'd'), { after: 'd' }).columnNames(),
['b', 'd', 'a', 'c'],
'relocate data, after'
);

tableEqual(t,
dt.relocate(not('b', 'd'), { before: 'c' }),
{ b, a, c, d },
t.deepEqual(
dt.relocate(not('b', 'd'), { before: 'c' }).columnNames(),
['b', 'a', 'c', 'd'],
'relocate data, before self'
);

tableEqual(t,
dt.relocate(not('b', 'd'), { after: 'a' }),
{ a, c, b, d },
t.deepEqual(
dt.relocate(not('b', 'd'), { after: 'a' }).columnNames(),
['a', 'c', 'b', 'd'],
'relocate data, after self'
);

Expand All @@ -37,15 +42,15 @@ tape('relocate repositions columns using multi-column anchor', t => {
const a = [1], b = [2], c = [3], d = [4];
const dt = table({ a, b, c, d });

tableEqual(t,
dt.relocate([1, 3], { before: range(2, 3) }),
{ b, a, c, d },
t.deepEqual(
dt.relocate([1, 3], { before: range(2, 3) }).columnNames(),
['a', 'b', 'd', 'c'],
'relocate data, before range'
);

tableEqual(t,
dt.relocate([1, 3], { after: range(2, 3) }),
{ b, d, a, c },
t.deepEqual(
dt.relocate([1, 3], { after: range(2, 3) }).columnNames(),
['a', 'c', 'b', 'd'],
'relocate data, after range'
);

Expand All @@ -56,15 +61,15 @@ tape('relocate repositions and renames columns', t => {
const a = [1], b = [2], c = [3], d = [4];
const dt = table({ a, b, c, d });

tableEqual(t,
dt.relocate({ a: 'e', c: 'f' }, { before: { b: '?' } }),
{ e: a, f: c, b, d },
t.deepEqual(
dt.relocate({ a: 'e', c: 'f' }, { before: { b: '?' } }).columnNames(),
['e', 'f', 'b', 'd'],
'relocate data, before plus rename'
);

tableEqual(t,
dt.relocate({ a: 'e', c: 'f' }, { after: { b: '?' } }),
{ b, d, e: a, f: c },
t.deepEqual(
dt.relocate({ a: 'e', c: 'f' }, { after: { b: '?' } }).columnNames(),
['b', 'e', 'f', 'd'],
'relocate data, after plus rename'
);

Expand Down

0 comments on commit 3a05659

Please sign in to comment.