Skip to content

Commit

Permalink
Extend createTable()
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmcintyre committed Jul 16, 2022
1 parent ec30f93 commit 7e3eb53
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/numero.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "numero",
"version": "0.2.3",
"version": "0.2.4",
"description": "A friendly and intuitive math library for p5.js",
"main": "index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion src/table/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,15 @@ Table.prototype.isin = function _isin(values: any[]): Table {
};

// eslint-disable-next-line import/prefer-default-export
export const createTable = (data: TableRow[] | object): Table => {
export const createTable = (data: TableRow[] | string[] | object): Table => {
let output: Table;
if (data instanceof Array && data[0] instanceof TableRow) {
// @ts-ignore
output = new Table(data);
} else if (data instanceof Array && typeof data[0] === 'string') {
output = new Table();
// @ts-ignore
output.columns = data.slice();
} else if (data instanceof Object) {
output = new Table();
output.columns = Object.keys(data);
Expand Down
7 changes: 7 additions & 0 deletions test/unit/table/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ describe('Utilities', function () {
});

describe('createTable()', function () {
it('Should create a p5.Table with empty columns', function () {
const columns = ['a', 'b', 'c'];
const t = pInst.createTable(columns);
expect(t.columns).to.eql(columns);
expect(t.getRowCount()).to.eq(0);
});

it('Should create a p5.Table from an object', function () {
const t = pInst.createTable({
x: [1, 2, 3],
Expand Down

0 comments on commit 7e3eb53

Please sign in to comment.