diff --git a/src/table/Table.js b/src/table/Table.js index a256f7f..21d7449 100644 --- a/src/table/Table.js +++ b/src/table/Table.js @@ -29,7 +29,7 @@ export class Table { */ constructor(columns, names, filter, group, order, params) { const data = Object.freeze({ ...columns }); - names = names ?? Object.keys(data); + names = names?.slice() ?? Object.keys(data); const nrows = names.length ? data[names[0]].length : 0; /** @private */ this._names = Object.freeze(names); diff --git a/test/table/table-test.js b/test/table/table-test.js index 2ed6389..e2146d5 100644 --- a/test/table/table-test.js +++ b/test/table/table-test.js @@ -70,6 +70,25 @@ describe('Table', () => { assert.deepEqual(scanned, ref, 'scanned values match'); }); + it('copies and freezes column object', () => { + const cols = { x: [1, 2, 3 ]}; + const table = new Table(cols); + const data = table.data(); + assert.notStrictEqual(data, cols, 'is copied'); + assert.strictEqual(data, table._data, 'is direct'); + assert.ok(Object.isFrozen(data), 'is frozen'); + assert.throws(() => data.y = [4, 5, 6], 'throws on edit'); + }); + + it('copies and freezes column name list', () => { + const names = ['y', 'x']; + const cols = { x: [1, 2, 3 ], y: [4, 5, 6]}; + const table = new Table(cols, names); + assert.notStrictEqual(table._names, names, 'is copied'); + assert.ok(Object.isFrozen(table._names), 'is frozen'); + assert.throws(() => table._names.push('z'), 'throws on edit'); + }); + it('scan supports filtering and ordering', () => { const table = new Table({ a: ['a', 'a', 'a', 'b', 'b'],