diff --git a/src/verbs/derive.js b/src/verbs/derive.js index 0596bb0c..cb166fba 100644 --- a/src/verbs/derive.js +++ b/src/verbs/derive.js @@ -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 ); } \ No newline at end of file diff --git a/src/verbs/relocate.js b/src/verbs/relocate.js index 05cb1b1b..4fb90424 100644 --- a/src/verbs/relocate.js +++ b/src/verbs/relocate.js @@ -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; diff --git a/test/verbs/derive-test.js b/test/verbs/derive-test.js index 61f1fc1f..e9fa14fe 100644 --- a/test/verbs/derive-test.js +++ b/test/verbs/derive-test.js @@ -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(); }); diff --git a/test/verbs/relocate-test.js b/test/verbs/relocate-test.js index 486cbf14..210d1a20 100644 --- a/test/verbs/relocate-test.js +++ b/test/verbs/relocate-test.js @@ -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' ); @@ -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' ); @@ -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' );